题目大意:
在所有从1到n的路线最小边中找到最大的那个,即maxmin
Code(spfa):
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
int n,m;
int e[MAXN][MAXN];
bool vis[MAXN];
int q[MAXN*MAXN/2];
int d[MAXN];
int spfa(int s){
for(int i = 0; i < n; ++i) d[i] = (i == s ? INF : 0);
vis[s] = true;
int h = 0,t = 0;
q[t++] = s;
while(h < t){
int u = q[h++];
for(int v = 0; v < n; ++v){
if(e[u][v]){
if(min(d[u],e[u][v]) > d[v]){
d[v] = min(d[u],e[u][v]);
if(!vis[v]){
q[t++] = v;
}
}
}
}
}
}
int main()
{
int a,b,c,t,kcase = 0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
memset(e,0,sizeof(e));
for(int i = 0; i < m; ++i){
scanf("%d%d%d",&a,&b,&c);
e[a-1][b-1] = e[b-1][a-1] = c;
}
spfa(0);
printf("Scenario #%d:\n",++kcase);
printf("%d\n",d[n-1]);
printf("\n");
}
return 0;
}
Code(最大生成树思想):
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
int u[MAXN*MAXN],v[MAXN*MAXN],w[MAXN*MAXN],r[MAXN*MAXN],p[MAXN*MAXN];
int n,m;
bool cmp(const int i, const int j){
return w[i] > w[j];
}
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
int kruskal(){
int ans = INF;
for(int i = 1; i <= n; ++i) p[i] = i;
for(int i = 0; i < m; ++i) r[i] = i;
sort(r,r+m,cmp); //注意这里是 m
for(int i = 0; i < m; ++i){
int e = r[i];
int x = find(u[e]);
int y = find(v[e]);
if(x != y){
ans = min(ans,w[e]);
p[x] = y;
if(find(1) == find(n)) //不需要是完整的树,此时最大,完整时候不一定最大
break;
}
}
return ans;
}
int main()
{
int a,b,c,t,kcase = 0;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i = 0; i < m; ++i){
scanf("%d%d%d",&a,&b,&c);
u[i] = a; v[i] = b; w[i] = c;
}
int ans = kruskal();
printf("Scenario #%d:\n",++kcase);
printf("%d\n",ans);
printf("\n");
}
return 0;
}