poj 1797Heavy Transportation(最大生成樹)
Heavy Transportation
Description Background Input The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings. Output The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line. Sample Input Sample Output Source TUD Programming Contest 2004, Darmstadt, Germany |
[Submit] [Go Back] [Status] [Discuss]
求最大的最小權值
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1005
#define maxm 1005*1005+5
using namespace std;
int n,m,cnt,t;
struct edge{
int u,v,dis;
edge()
{
}
edge(int u,int v,int dis ):u(u),v(v),dis(dis)
{
}
bool operator<(const edge&rhs)
{
return dis>rhs.dis;
}
}edges[maxm];
int father[maxn];
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
int kru()
{int sum=0;
int ans=0;
sort(edges,edges+cnt);
for(int i=0;i<cnt;i++)
{
int u=edges[i].u;
int v=edges[i].v;
if(find(u)!=find(v))
{
father[find(u)]=find(v);
if(find(1)==find(n))
return edges[i].dis;
}
}
return -1;
}
int main()
{
scanf("%d",&t);
for(int i=1;i<=t;i++)
{cnt=0;
scanf("%d%d",&n,&m);
for(int j=0;j<=n;j++)
father[j]=j;
int u,v,w;
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
edges[cnt++]=edge(u,v,w);
}
printf("Scenario #%d:\n",i);
printf("%d\n\n",kru());
}
}