ZOJ 2966 Build The Electric System【最小生成树】
来源:互联网 发布:品索网络机顶盒 编辑:程序博客网 时间:2024/05/07 19:13
In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there's at least one way between every two villages.
Input
Standard input will contain multiple test cases. The first line of the input is a single integerT (1 <=T <= 50) which is the number of test cases. And it will be followed byT consecutive test cases.
In each test case, the first line contains two positive integers N andE (2 <=N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There followE lines, and each of them contains three integers,A, B,K (0 <= A, B < N, 0 <=K < 1000). A and B respectively means the index of the starting village and ending village of the power line. IfK is 0, it means this line still works fine after the snow storm. IfK is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.
Output
For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.
Sample Input
13 30 1 50 2 01 2 9
Sample Output
5
最小生成树,题意是,给出的每两个村庄之间的距离,如果距离为0,那么之间还能通行,求还需要建多长的路才能使所有的城市连接起来......
简单题,直接水过.....
#include<stdio.h>#include<algorithm>using namespace std;int per[55],n,cnt,kase;struct lu{int a,b,len; }x[2505];void init(){for(int i=0;i<n;++i){per[i]=i;}}int find(int x){int r=x;while(r!=per[r]){r=per[r];}int i=x,j;while(i!=r){j=per[i];per[i]=r;i=j;}return r;}void join(int x,int y){int fx=find(x),fy=find(y);if(fx!=fy){per[fx]=fy;++cnt;kase=1;}}int cmp(lu a,lu b){return a.len<b.len;}int main(){int t,i,j,m;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);init();cnt=0;int a,b,c,k=0;for(i=0;i<m;++i){scanf("%d%d%d",&a,&b,&c);if(c==0){join(a,b);}else{x[k].a=a;x[k].b=b;x[k++].len=c;}}sort(x,x+k,cmp);int sum=0;for(i=0;cnt<n-1;++i){kase=0;join(x[i].a,x[i].b);if(kase){sum+=x[i].len;}}printf("%d\n",sum);}return 0; }
- ZOJ 2966 Build The Electric System【最小生成树】
- 【最小生成树】ZOJ 2966 Build The Electric System
- ZOJ 2966 Build The Electric System 最小生成树
- ZOJ 2966 Build The Electric System【最小生成树】
- ZOJ 2966 Build The Electric System(最小生成树)
- ZOJ 2966 Build The Electric System 最小生成树 Kruscal && Prim
- zoj 2966 Build The Electric System【最小生成树 Kruskal && prim】
- ZOJ-2966-Build The Electric System【5th浙江省赛】【最小生成树】
- ZOJ 2966 Build The Electric System (kruskal)
- ZOJ 2966 Build The Electric System MST
- ZOJ 2966 Build The Electric System
- zoj 2966 Build The Electric System
- ZOJ 2966Build The Electric System
- zoj 2966 Build The Electric System ( prim || kruskal~ )
- zoj 2966 Build The Electric System(MST)
- zoj 2966 Build The Electric System(并查集)
- ZOJ 1965 Build The Electric System
- Build The Electric System
- 从安卓客户端向服务器端发送JSON格式的数据
- TextView省略号显示
- 集合遍历
- 黑马程序员--数组
- 关于Handlebars.java的模板引擎的使用
- ZOJ 2966 Build The Electric System【最小生成树】
- leetcode Balanced Binary Tree 平衡二叉树判定
- ZOJ 1586 QS Network(最小生成树--prime)
- 重构iOS应用
- 基于jQuery、Bootstrap实现的仿知乎前端动态列表效果
- 可视化编程storyBoard
- [转]tornado ioloop start 的过程
- 【HDU 5384】Danganronpa(AC自动机)
- QT中创建透明或者异性的Window