poj 2421 Constructing Roads
2012-05-09 16:45
211 查看
Constructing Roads
Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
Sample Output
300题了
不知不觉刷了300个水题了 我真够水的。。。。
留个纪念
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15578 | Accepted: 6302 |
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
#include<iostream> #include<algorithm> using namespace std; int n, m; int mmax; int num; struct node { int x, y; int w; }; node edge[10005]; int map[101][101]; int father[101]; int comp(node a, node b) { return a.w < b.w; } int find_set(int a) { if(father[a] != a) { father[a] = find_set(father[a]); } return father[a]; } void union_set(int a, int b) { father[a] = b; } int main() { int i, j; freopen("e:\\data.txt", "r", stdin); freopen("e:\\out.txt", "w", stdout); cin>>n; num = 0; for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { cin>>map[i][j]; } } int q; cin>>q; while(q--) { int a, b; cin>>a>>b; map[a][b] = 0; map[b][a] = 0; } for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if(i < j) { edge[num].x = i; edge[num].y = j; edge[num].w = map[i][j]; num++; } } } sort(edge, edge + num, comp); mmax = 0; for(i = 1; i <= n; i++) { father[i] = i; } for(i = 0; i < num; i++) { int p = find_set(edge[i].x); int q = find_set(edge[i].y); if(p != q) { union_set(p, q); mmax += edge[i].w; } } cout<<mmax<<endl; return 0; }
300题了
不知不觉刷了300个水题了 我真够水的。。。。
留个纪念
相关文章推荐
- POJ 2421 图的最小生成树 prim算法 模板代码的利用
- POJ 2421 Constructing Roads
- poj 2421 (最小生成树 Prime+kruskal)
- {POJ}{2421}{Constructing Roads}{Prim}
- POJ 2421 Constructing Roads (最小生成树)
- POJ 2421 Constructing Roads
- poj2421
- poj 2421 最小生成树(prime) 模版题
- POJ 2421
- Poj 2421 Constructing Roads(Prim 最小生成树)
- poj 2421 Constructing Roads(prim)
- POJ 2421 Constructing Roads(Kruskal算法)
- POJ-2421-Constructing Roads(最小生成树 普利姆)
- poj 2421 Constructing Roads
- POJ 2421 Constructing Roads(最小生成树,边已经存在的处理方法)
- POJ 2421 Constructing Roads
- poj 2421 Constructing Roads(最小生成树prim)
- POJ 2421 Constructing Roads 克鲁斯卡尔(Kruskal)算法
- Constructing Roads POJ - 2421 prim
- POJ 2421 解题报告