标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20889 | Accepted: 8817 |
Description
Input
Output
Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> using namespace std; int per[110]; int map[110][110]; int N, Q; struct node{ int u, v, w; }; node edge[20000]; int cmp(node a, node b){ return a.w < b.w; } void init(){ for(int i = 1; i <= N; ++i) per[i] = i; } int find(int x){ if(x == per[x]) return x; return per[x] = find(per[x]); } bool join (int x, int y){ int fx = find(x); int fy = find(y); if(fx != fy){ per[fx] = fy; return true; } return false; } int main (){ while(scanf("%d", &N) != EOF){ int k = 0; for(int i = 1; i <= N; ++i) for(int j = 1; j <= N; ++j) scanf("%d", &map[i][j]); scanf("%d", &Q); while(Q--){ int u, v; scanf("%d%d", &u, &v); map[u][v] = 0; } for(int i = 1; i <= N; ++i) for(int j = 1; j <= N; ++j){ edge[k].u = i; edge[k].v = j; edge[k].w = map[i][j]; k++; } sort(edge, edge + k, cmp); int sum = 0; init(); for(int i = 0; i < k; ++i){ //printf("---%d %d %d\n", edge[i].u, edge[i].v, edge[i].w); if(join(edge[i].u, edge[i].v)) sum += edge[i].w; } printf("%d\n", sum); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2421--Constructing Roads【水题 && 最小生成树 && kruskal】
标签:
原文地址:http://blog.csdn.net/hpuhjh/article/details/47402855