HDU 1879:引き続き開通工事【kruskal】
工事を続ける
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17839 Accepted Submission(s): 7681
Problem Description
省政府の「円滑な工事」の目標は、全省のどの2つの村の間でも道路交通を実現させることである(しかし、直接的な道路がつながっているとは限らず、間接的に道路を通過すればよい).現在、都市道路統計表が得られ、任意の2つの都市間の道路建設費用と、その道路がすでに開通しているかどうかがリストされている.プログラムを作成して、全省の円滑化に必要な最低コストを計算してください.
Input
テスト入力には、いくつかのテスト例が含まれます.各試験例の1行目は、村の数N(1Nが0のとき入力は終了します.
Output
各テスト例の出力は1行を占め、全省の円滑化に必要な最低コストを出力する.
Sample Input
Sample Output
Author
ZJU
AC-code:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17839 Accepted Submission(s): 7681
Problem Description
省政府の「円滑な工事」の目標は、全省のどの2つの村の間でも道路交通を実現させることである(しかし、直接的な道路がつながっているとは限らず、間接的に道路を通過すればよい).現在、都市道路統計表が得られ、任意の2つの都市間の道路建設費用と、その道路がすでに開通しているかどうかがリストされている.プログラムを作成して、全省の円滑化に必要な最低コストを計算してください.
Input
テスト入力には、いくつかのテスト例が含まれます.各試験例の1行目は、村の数N(1
Output
各テスト例の出力は1行を占め、全省の円滑化に必要な最低コストを出力する.
Sample Input
3
1 2 1 0
1 3 2 0
2 3 4 0
3
1 2 1 0
1 3 2 0
2 3 4 1
3
1 2 1 0
1 3 2 1
2 3 4 1
0
Sample Output
3
1
0
Author
ZJU
AC-code:
#include<cstdio>
#include<algorithm>
using namespace std;
int per[105];
struct node
{
int b,e,v,jud;
}s[10005];
void pre()
{
for(int i=0;i<105;i++)
per[i]=i;
}
bool cmp(node a,node c)
{
return a.v<c.v;
}
int find(int a)
{
return a==per[a]?a:find(per[a]);
}
int join(int a,int c)
{
int fx=find(a);
int fy=find(c);
if(fx!=fy)
{
per[fx]=fy;
return true;
}
return false;
}
int main()
{
int n,m,i,sum;
while(scanf("%d",&n),n)
{
pre();
m=n*(n-1)/2;
for(i=0;i<m;i++)
scanf("%d%d%d%d",&s[i].b,&s[i].e,&s[i].v,&s[i].jud);
for(i=0;i<m;i++)
if(s[i].jud)
join(s[i].b,s[i].e);
sort(s,s+m,cmp);
sum=0;
for(i=0;i<m;i++)
if(join(s[i].b,s[i].e))
if(!s[i].jud)
sum+=s[i].v;
printf("%d
",sum);
}
return 0;
}