描述
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.
从1到n的路径中最小的边最大
解法
(最小的边最大很容易想到二分)
我们选取一个边权尽量大的路径即可
-》最大生成树
如果用kruskal算法的话,最后一条加入的边(使得1和n成为一个集合)的边权就是答案
代码
#include<cstdio>
#include<algorithm>
typedef long long LL;
const int N=1e3+10;
const int oo=0x3f3f3f3f;
using namespace std;
bool flag;
int fa[N],x[N],y[N];
int read(){
int f=1,s=0;char c=getchar();
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(;c>='0'&&c<='9';c=getchar())s=s*10+c-48;
return f*s;
}
struct qq{
int u,v,w;
void in(){
u=read();v=read();w=read();
}
}a[N*N];
bool operator<(qq a,qq b){
return a.w>b.w;
}
int find(int x){
if(fa[x]!=x)fa[x]=find(fa[x]);
return fa[x];
}
bool check(int x,int y){
return find(x)==find(y);
}
void join(int x,int y){
x=find(x);y=find(y);
if(x==y)return;
fa[x]=y;
}
int work(){
int n=read(),m=read();
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=0;i<m;i++)a[i].in();
sort(a,a+m);
for(int i=0;i<m;i++){
int u=find(a[i].u),v=find(a[i].v);
if(u==v)continue;
join(u,v);
if(check(1,n)==true)return a[i].w;
}
}
int main(){
int T=read();
for(int i=1;i<=T;i++)
printf("Scenario #%d:\n%d\n\n",i,work());
return 0;
}