继续畅通工程
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1006&cid=12467&hide=0
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
当N为0时输入结束。
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
Author
Source
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int parent[105];
struct Edge{
int from,to,price,flag;
}edge[5100];
bool compare(Edge a,Edge b){
return a.price<b.price;
}
void init(int n){
for(int i=1;i<=n;i++)
parent[i]=i;
}
int find(int x){
while(x!=parent[x])
x=parent[x];
return x;
}
void merge(int x,int y){
x=find(x);
y=find(y);
parent[x]=y;
}
int main(){
int sum ,n;
while(scanf("%d",&n)&&n){
int m=(n*(n-1))/2;
init(n);
sum=0;
for(int i=1;i<=m;i++)
scanf("%d%d%d%d", &edge[i].from ,&edge[i].to,&edge[i].price,&edge[i].flag);
for(int i=1;i<=m;i++){
if(edge[i].flag==1){
int x=find(edge[i].from),y=find(edge[i].to);
if(x!=y){
merge(x,y);
n--;
}
}
}
sort(edge+1,edge+m+1,compare);
for(int i=1;i<=m&&n>1;i++){
int x=find(edge[i].from),y=find(edge[i].to);
if(x!=y){
merge(x,y);
n--;
if(edge[i].flag==0)
sum+=edge[i].price;
}
}
printf("%d\n",sum);
}
return 0;
}
304

被折叠的 条评论
为什么被折叠?



