# 繼續暢通工程

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14089    Accepted Submission(s): 6143

Problem Description

Input

Output

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

#最小生成樹
``````#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define N 5100
struct node
{
int u,v,w;
int flag;
} q[N];
int cmp(node x,node y)
{
return x.w<y.w;
}
int p[110];
int m,i;
void init ()
{
for(i=0; i<=m; i  )
{
p[i] = i;
}
memset(q,0,sizeof(q));
}
int find(int x)//並查集的find
{
return p[x] == x ? x : find(p[x]);
}
void bin(int x,int y)
{
int a = find(x);
int b = find(y);
p[b] = a;
}
int main()
{
while(scanf("%d",&m),m)
{
init();
int ans = 0;
int A = m*(m-1)/2;
for (i = 0; i < A; i  )
{
scanf("%d%d%d%d",&q[i].u,&q[i].v,&q[i].w,&q[i].flag);
if(q[i].flag)
q[i].w = 0;
}
sort(q,q A,cmp);
for(i = 0; i < A; i   )
{
int x = find(q[i].u);//兩個端點
int y = find(q[i].v);//找出當前端點所在集合編號
if(x != y)//如果再不同集合，合併
{
bin(x,y);
ans  = q[i].w;
}
}
printf ("%d\n",ans);
}
return 0;
}``````

﻿﻿