ZOJ 2966 最小生成树(B)
2015-11-07 23:18
337 查看
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
5
题意:大概是说将所有村庄连接起来最小的花费
题解:最小生成树裸题。
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<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define LL long long #define N 10000 int fa ; #define inf 0x3f3f3f3f struct point { int x,y,v;; point (int _x,int _y,int _v):x(_x),y(_y),v(_v){} point(){} bool operator< (const point &x1) const { return this->v<x1.v; } }; vector<point>eg; int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } void unio(int x,int y) { x=find(x); y=find(y); if(fa[x]!=y) fa[x]=y; } int kruskal() { int ans=0; for(int i=0;i<eg.size();i++) { if(find(eg[i].x)!=find(eg[i].y)) { unio(eg[i].x,eg[i].y); ans+=eg[i].v; } } return ans; } int main(){ #ifdef CDZSC freopen("i.txt","r",stdin); #endif char s ; int n,t,m,x,y,va; scanf("%d",&t); while(t--) { eg.clear(); scanf("%d%d",&n,&m); for(int i=0;i<n;i++) fa[i]=i; for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&va); eg.push_back(point(x,y,va)); } sort(eg.begin(),eg.end()); printf("%d\n",kruskal()); } return 0; }
相关文章推荐
- line-hight-(行高)解析
- 交换某个类里面的方法
- c++调用ado执行带参数的sql(非存储过程)
- CSS 属性 overflow 深入理解学习笔记
- 在动作栏(Action Bar)中添加和删除选项卡
- Linux(centOS)上的mysql 1130连接错误
- HQL语句大全(转载)
- 如何操作笔记本显得逼格很高?
- iftop、ifstat详解
- ubuntu14.04忽然不能登录,输入密码一直返回登录界面
- python之函数用法fromkeys()
- 黑马程序员——java基础---IO(上)
- 强制加载库
- C++ sstream 中处理字符串
- CSS盒子模型
- hdu 4467 Graph 阀值
- ZOJ 2965 模拟 (A)
- android 动画 amintorset 的使用
- Day 5(11.7):(2)实训--数据的完整性
- 典型的指针笔试题