POJ 2421 有一条连通下的最小生成树
2016-08-02 17:10
253 查看
[align=center]Constructing Roads[/align]
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
坑爹,开始在并查集中找到共同根后应该返回false,自己写成return -1,返回的还是true,找错找了半天
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22440 | Accepted: 9588 |
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 <cstdio> #include <algorithm> #include <string.h> #include <climits> using namespace std; const int N = 110; int father ; struct edge{ int lp,rp,value; }ee[N*N]; int map ,flag ,numedge,n; bool cmp(edge a,edge b){ return a.value < b.value; } //并查集模板 int find(int x){ if(father[x] == x) return father[x]; else father[x]=find(father[x]); return father[x]; } int merge(int x,int y){ int fx = find(x); int fy = find(y); if(fx!=fy) { father[fy] = fx; return 1; } else return 0; //要么bool类型,返回-1也是ture,只有return 0 才是false } //克鲁斯卡尔算法 int kruskal(){ sort(ee,ee+numedge,cmp); for(int i = 1; i <= n; ++i) father[i] = i; int sum = 0; for(int i = 0; i < numedge; ++i){ int lx = ee[i].lp; int rx = ee[i].rp; if(merge(lx,rx)){ sum += ee[i].value; } } return sum; } int main(){ //freopen("1.txt","r",stdin); while(scanf("%d",&n) != EOF){ memset(flag,0,sizeof(flag)); for(int i = 1; i <= n; ++i){ for(int j = 1; j <= n; ++j) scanf("%d",&map[i][j]); } int m,x,y; scanf("%d",&m); while(m--){ scanf("%d%d",&x,&y); flag[x][y] = 1; } numedge = 0; //无向图,只用放1-2,1-3,2-3 for(int i = 1; i < n; ++i){ for(int j = i + 1; j <= n; j++){ if(flag[i][j]){ ee[numedge].lp = i; ee[numedge].rp = j; ee[numedge].value = 0; numedge++; } else{ ee[numedge].lp = i; ee[numedge].rp = j; ee[numedge].value = map[i][j]; numedge++; } } } int ans = kruskal(); printf("%d\n",ans); } return 0; }
坑爹,开始在并查集中找到共同根后应该返回false,自己写成return -1,返回的还是true,找错找了半天
相关文章推荐
- POJ 2421 Constructing Roads 【已知道路连通求最小生成树】
- poj 2421 最小生成树(prime) 模版题
- poj2421 Constructing Roads ——最小生成树入门题_Kruscal算法
- POJ 2421 -Constructing Roads -最小生成树
- POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法
- poj 1251 1258 2421 2485(最小生成树prime)
- POJ2421_Constructing Roads(最小生成树)
- poj 2421 Constructing Roads(最小生成树 kruskal算法)
- POJ-2421 最小生成树 算法复习计划
- poj 3522 二分+判图连通(最小Slim生成树)
- POJ 2421 Constructing Roads 最小生成树
- (kruscal12.1.1)POJ 2421 Constructing Roads(使用kruscal算法来生成最小生成树&&计算最小带权路径和)
- [NWPU][2014][TRN][17]最小生成树 B - 基础 POJ 2421
- poj 2421 Constructing Roads 最小生成树
- POJ2421 prim算法求最小生成树
- POJ 2421 Constructing Roads(最小生成树)
- Poj 2421 Constructing Roads(Prim 最小生成树)
- POJ 2421 Constructing Roads(最小生成树)
- poj 2421 Constructing Roads(最小生成树))
- poj-2421 Constructing Roads(最小生成树 Kruskal算法)