课堂练习
-P1396 营救-
思路
- 使得经过道路的拥挤度最大值最小,看到这句话,你有没有想到一位故人——二 分答案
- 单调性:如果存在一条路径,其最大拥挤度不超过 ,那么对于所有大于 的数,必然也存在这样的路径,反之亦然
- 所以这里用并查集判断拥挤度可行性,然后二分查找即可
好吧其实还有 做法
- 按边权从小到大加边合并连通块,当 和 第一次连通时,最后加入的这条边的权值,就是 的最小瓶颈(即路径上的最大边权的最小值)
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
int u,v,w;
}a[N];
int n,m,s,t,p[N];
int find(int x)
{
return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
int t1=find(x),t2=find(y);
if(t1!=t2) p[t1]=t2;
}
bool check(int x)
{
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1;i<=m;i++)
{
if(a[i].w<=x) merge(a[i].u,a[i].v);//只保留拥挤度<=x的边
}
return (find(s)==find(t));
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m>>s>>t;
for(int i=1;i<=m;i++) cin>>a[i].u>>a[i].v>>a[i].w;
int l=-1,r=N+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}
cout<<r;
return 0;
}
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
int u,v,w;
}a[N];
int n,m,s,t,k,p[N],cnt,ans;
int find(int x)
{
return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
int t1=find(x),t2=find(y);
if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
return x.w<y.w;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>s>>t;
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1;i<=m;i++) cin>>a[i].u>>a[i].v>>a[i].w;
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++)
{
int u=a[i].u,v=a[i].v,w=a[i].w;
if(find(u)!=find(v))
{
merge(u,v);
if(find(s)==find(t)) return cout<<w,0;
}
}
return 0;
}-P1194 买礼物-
思路
- 这题其实就是考你点权转边权,这里有两种方法:
- 直接点权转边权:对每对物品 ,可添加两种边:
- 优惠边:权值为 (仅当 时添加, 无意义)
- 原价边:权值为 ,表示不通过 的优惠,直接按原价买
C++ for(int i=1;i<=m;i++) { for(int j=1,x;j<=m;j++) { cin>>x; if(x!=0) s[++k]={i,j,x};//优惠边 s[++k]={i,j,n}; //原价购买 } } - 超级源点:
新增一个虚拟节点
虚拟节点 到每个物品 连一条权值为 的边,表示原价购买第 个物品
物品 和物品 之间连一条权值为 的边,表示买了 之后用优惠价买
C++ for(int i=1;i<=m;i++) s[++k]={0,i,n}; for(int i=1;i<=m;i++) { for(int j=1,x;j<=m;j++) { cin>>x; if(x!=0) s[++k]={i,j,x}; } }
- 直接点权转边权:对每对物品 ,可添加两种边:
- 剩下部分打板子即可
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
int u,v,w;
}s[N];
int n,m,k,p[N],cnt,ans;
int find(int x)
{
return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
int t1=find(x),t2=find(y);
if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
return x.w<y.w;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m;
for(int i=1;i<=m;i++) p[i]=i;
for(int i=1;i<=m;i++)
{
for(int j=1,x;j<=m;j++)
{
cin>>x;
if(x!=0) s[++k]={i,j,x};
s[++k]={i,j,n};
}
}
sort(s+1,s+k+1,cmp);
for(int i=1;i<=k;i++)
{
int u=s[i].u,v=s[i].v,w=s[i].w;
if(find(u)!=find(v))
{
merge(u,v);
ans+=w;
cnt++;
}
}
if(cnt==m-1) cout<<ans+n;//必须额外加一个原价n,因为所有优惠都需要基于已买一个物品的条件上,需要一个初始的原价购买
return 0;
}超级源点
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
int u,v,w;
}s[N];
int n,m,k,p[N],ans;
int find(int x)
{
return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
int t1=find(x),t2=find(y);
if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
return x.w<y.w;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m;
for(int i=1;i<=m;i++) p[i]=i;
for(int i=1;i<=m;i++) s[++k]={0,i,n};
for(int i=1;i<=m;i++)
{
for(int j=1,x;j<=m;j++)
{
cin>>x;
if(x!=0) s[++k]={i,j,x};
}
}
sort(s+1,s+k+1,cmp);
for(int i=1;i<=k;i++)
{
int u=s[i].u,v=s[i].v,w=s[i].w;
if(find(u)!=find(v))
{
merge(u,v);
ans+=w;
}
}
cout<<ans;
return 0;
}-P2916 [USACO08NOV] Cheering up the Cow G-
思路
- 要去掉尽量多的道路,同时保持所有牧场连通,因此保留的道路必然是一棵生成树
- 时间构成:
- 道路时间:每条道路走一次需要 ,回路中每条边会被恰好走两次,因此每条边贡献
- 牧场时间:每次路过牧场 都要花 ,包括出发和返回时
- 总时间:
我们可以把总时间拆分为两部分,最终转化为生成树的总权值:
- 对于生成树中的每条边 ,除了道路时间,还会让 和 各被经过一次,因此总贡献为
- 额外贡献:起点会比其他节点多被路过一次(出发时的第一次),因此需要额外加一个起点的 值
- 因此,总时间 = 生成树所有边的总贡献 + 起点的 值
有了边权,剩下也是打板子即可
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
int u,v,w;
}s[N];
int n,m,a[N],p[N],cnt,ans,minn=inf;
int find(int x)
{
return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
int t1=find(x),t2=find(y);
if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
return x.w<y.w;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m;
for(int i=1;i<=n;i++) p[i]=i;
for(int i=1;i<=n;i++)
{
cin>>a[i];
minn=min(minn,a[i]);//找最优起点
}
for(int i=1;i<=m;i++)
{
cin>>s[i].u>>s[i].v>>s[i].w;
s[i].w=a[s[i].u]+a[s[i].v]+2*s[i].w;
}
sort(s+1,s+m+1,cmp);
for(int i=1;i<=m;i++)
{
int u=s[i].u,v=s[i].v,w=s[i].w;
if(find(u)!=find(v))
{
merge(u,v);
ans+=w;
cnt++;
if(cnt==n-1) break;
}
}
if(cnt==n-1) cout<<ans+minn;
return 0;
}