标签:des style color java 使用 os io strong
/*
题目大意:他的路还没修好,现在要修路,求出最省钱的修路方法,即求出最短距离即可。
解题思路:运用并查集,努力代换即可。看到求最少,就是贪心算法!不要害怕贪心,贪心是要排个序,序排好了,你也就成功了
难点详解:数组要开到5000才会过,不然在存数的时候会出现越界现象
关键点:运用并查集和对并查集的灵活使用
解题人:lingnichong
解题时间:2014-08-12 23:56:43
解题体会:数组开小了,一直WA,数组开那么大的原因:一开始有100的村子,但是随后是的N(N-1)/2行对应村庄间的距离, 所以数组要开的很大。总结错很多次的原因主要是对并查集的运用还不是很熟,还有就是对题意理解不够深刻。
*/
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
3 5Huge input, scanf is recommended.HintHint
#include<stdio.h>
#include<algorithm>
using namespace std;
int father[110];
struct place
{
int start;
int end;
int d;
}a[5000];
int cmp(place x,place y)
{
return x.d<y.d;
}
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}
int mergr(int s1,int s2)
{
int fa=find(s1),fb=find(s2);
if(fa!=fb)
{
father[fa]=fb;
return 1;
}
return 0;
}
int main()
{
int n,i;
int sum,m;
while(scanf("%d",&n),n)
{
sum=0;
m=n*(n-1)/2;
for(i=1;i<=m;i++)
scanf("%d%d%d",&a[i].start,&a[i].end,&a[i].d);
sort(a+1,a+1+m,cmp);//少了一个加一
for(i=1;i<=n;i++)
father[i]=i;
for(sum=0,i=1;i<=m;i++)//此处是 m ,因为有m组数据
if(mergr(a[i].start,a[i].end))
sum+=a[i].d;
printf("%d\n",sum);
}
return 0;
}
HDU 1233 还是畅通工程,布布扣,bubuko.com
标签:des style color java 使用 os io strong
原文地址:http://blog.csdn.net/qq_16767427/article/details/38524833