Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
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 3 3 1 2 3 1 3 4 2 3 5
Scenario #1: 4
题意:求1到n的最优值(1到n的路径中,边权最小的定义是min,求出min的最大值)
思路:其实就是求最大生成树的最小边权;
//Kruskal 解法;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pre[600000];//此题易RE;
struct node
{
int a;
int b;
int c;
}s[600000];
bool cmp(node x,node y)//根据题意,降序排列
{
return x.c>y.c;
}
int find(int x)
{
if(pre[x]==-1)
return x;
return pre[x]=find(pre[x]);
}
int main()
{
int m,n,i,t,k=0;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
memset(pre,-1,sizeof(pre));
for(i=0;i<m;i++)
scanf("%d %d %d",&s[i].a,&s[i].b,&s[i].c);
sort(s,s+m,cmp);
int ans;
for(i=0;i<m;i++)
{
int fx=find(s[i].a);
int fy=find(s[i].b);
if(fx!=fy)
{
pre[fy]=fx;
if(find(1)==find(n))
{
ans=s[i].c;
break;
}
}
}
printf("Scenario #%d:\n",++k);//zz博主因为没加冒号,wa了数次;
printf("%d\n\n",ans);//注意空行;
}
return 0;}
//Dijkstra 解法;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int graph[1010][1010];
int dij(int n)
{
int mst[1010],lowcost[1010];
int i,j,minid;
memset(mst,0,sizeof(mst));
for(i=1;i<=n;i++)//这个时候lowcost代表最大生成树的最小边权;
lowcost[i]=graph[1][i];
mst[1]=1;
for(i=1;i<=n;i++)
{
int smin=0;
for(j=1;j<=n;j++)
{
if(lowcost[j]>smin && !mst[j])
{
smin=lowcost[j];
minid=j;
}
}
mst[minid]=1;
for(j=1;j<=n;j++)
{
if(!mst[j] && lowcost[j]<min(graph[minid][j],lowcost[minid]))
lowcost[j]=min(graph[minid][j],lowcost[minid]);
}
}
return lowcost[n];
}
int main()
{
int t,m,n,i,j,cost,k=0;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
memset(graph,0,sizeof(graph));
while(m--)
{
scanf("%d %d %d",&i,&j,&cost);
graph[i][j]=graph[j][i]=cost;//题目数据应该把i,j之间的权值就给了一次;
}
printf("Scenario #%d:\n",++k);
printf("%d\n\n",dij(n));
}
return 0;
}
//prim 解法;
#include<stdio.h>
#include<string.h>
int graph[1010][1010];
int prim(int n)
{
int lowcost[1010],mst[1010];
int i,j,min,minid,ans=0x3f3f3f3f;
memset(mst,0,sizeof(mst));
mst[1]=1;
for(i=2;i<=n;i++)
lowcost[i]=graph[1][i];
for(i=1;i<n;i++)
{
min=0;
for(j=1;j<=n;j++)
{
if(!mst[j] && lowcost[j]>min)
{
min=lowcost[j];
minid=j;
}
}
if(ans>min)
ans=min;
if(minid==n)//注意1和n连接后,结束;
return ans;
mst[minid]=1;
for(j=1;j<=n;j++)
{
if(!mst[j] && graph[minid][j]>lowcost[j])
lowcost[j]=graph[minid][j];
}
}
}
int main()
{
int m,n,i,t,j,cost,k=0;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
memset(graph,0,sizeof(graph));
while(m--)
{
scanf("%d %d %d",&i,&j,&cost);
graph[i][j]=graph[j][i]=cost;
}
printf("Scenario #%d:\n",++k);
printf("%d\n\n",prim(n));
}
return 0;
}