标签:ted 畅通工程 panel The 简单 limit ble span 初始化
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27972 Accepted Submission(s): 12279
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> using namespace std; struct node { int u; int v; int w; }edge[120]; int cmp(node A, node B) { if(A.w < B.w) return 1; else return 0; } //定义父节点 int fa[120]; int find(int x) { int p =fa[x]; while(p != fa[p]) p =fa[p]; return p; } int main() { int n, m;//道路条数n 村庄个数m //输入 while(cin >> n >> m && n != 0 && m != 0) { //初始化,每个节点的父节点是其本身 for(int i = 1; i <= m; i++) { fa[i] = i; } for(int i = 0; i < n; i++) { cout<<edge[i].u<<edge[i].v<<edge[i].w<<endl; } sort(edge,edge + n,cmp); int sum = 0;//总路程 int count = 0;//计数 for(int i = 0; i < n; i++) { int fx = find(edge[i].u); int fy = find(edge[i].v); if(fx != fy) { fa[fx] = fy; sum += edge[i].w; count++; } } if(count == M-1) cout<<sum<<endl; else cout<<"?"<<endl; } return 0; }
标签:ted 畅通工程 panel The 简单 limit ble span 初始化
原文地址:https://www.cnblogs.com/sakuraXiYue/p/10011542.html