ZOJ 2966 Build The Electric System
2012-09-15 23:48
405 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2966
Build The Electric System
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 integer
T (1 <= T <= 50) which is the number of test cases. And it will be followed by
T consecutive test cases.
In each test case, the first line contains two positive integers N and
E (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 follow
E 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. If
K is 0, it means this line still works fine after the snow storm. If
K 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
Sample Output
连通图问题。
Build The Electric System
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 integer
T (1 <= T <= 50) which is the number of test cases. And it will be followed by
T consecutive test cases.
In each test case, the first line contains two positive integers N and
E (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 follow
E 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. If
K is 0, it means this line still works fine after the snow storm. If
K 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
1 3 3 0 1 5 0 2 0 1 2 9
Sample Output
5
连通图问题。
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int maxn=125010; int n,m; int u[maxn],v[maxn],w[maxn]; int r[maxn],p[maxn]; int cmp(const int i,const int j) { return w[i]<w[j]; } int find(int x) { return p[x]==x? x:p[x]=find(p[x]); } int Kru() { int ans=0; for(int i=0;i<n;i++) p[i]=i; for(int i=0;i<m;i++) r[i]=i; sort(r,r+m,cmp); for(int i=0;i<m;i++) { int e=r[i]; int x=find(u[e]); int y=find(v[e]); if(x!=y) {ans+=w[e];p[x]=y;} } return ans; } int main() { int t,i,min; int x,y,l; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<m;i++) {scanf("%d%d%d",&x,&y,&l); u[i]=x; v[i]=y; w[i]=l; } min=Kru(); printf("%d\n",min); } return 0; }
相关文章推荐
- ZOJ 2966 Build The Electric System【最小生成树】
- ZOJ-2966-Build The Electric System【5th浙江省赛】【最小生成树】
- ZOJ 2966 Build The Electric System
- zoj 2966 Build The Electric System
- zoj_2966 Build TheElectric 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(最小生成树)
- zoj 2966 Build The Electric System(MST)
- zoj 2966 Build The Electric System
- 【最小生成树】ZOJ 2966 Build The Electric System
- ZOJ 2966 Build The Electric System (kruskal)
- zoj 2966 Build The Electric System【最小生成树 Kruskal && prim】
- zoj 2296(Build The Electric System)Kruskal || Prim
- zoj2966 build the electric system
- BNU 16485 Build The Electric System
- Build The Electric System
- ZOJ2966-Build The Electric System