zoj 2966 Build The Electric System【最小生成ツリーKruskal&&prim】

4144 ワード

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 Tconsecutive 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 costK 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

kruskal :
#include<cstdio>
#include<cstring>
#include<cmath>
#define mem(a, b) memset(a, (b), sizeof(a))
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d
", (a)) #define INF 0x3f3f3f #include<algorithm> using namespace std; const int mx = 500*500/2; int per[mx]; int n, e; void init() { for(int i = 0; i < mx; i++) per[i] = i; } struct node{ int start, end, val; }; node p[100010]; bool operator < (node a, node b) { return a.val < b.val; } int find(int x) { return x==per[x] ? x : (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(){ int t; Si(t); Wi(t){ init(); scanf("%d%d", &n, &e); int i, j, k; for(i = 0; i < e; i++) { scanf("%d%d%d", &p[i].start, &p[i].end, &p[i].val); } sort(p, p+e); int ans = 0; for(i = 0; i < e; i++) { if(join(p[i].start , p[i].end)) ans += p[i].val; } Pi(ans); } return 0; }

prim:
#include<cstdio>
#include<cstring>
#include<cmath>
#define mem(a, b) memset(a, (b), sizeof(a))
#define Wi(a) while(a--)
#define Si(a) scanf("%d", &a)
#define Pi(a) printf("%d
", (a)) #define INF 0x3f3f3f int map[550][550]; int n,e; int vis[550], val[550]; void prim() { mem(vis, 0); int i, j, k; int ans = 0, minn; for(i = 0; i < n; i++) val[i] = map[1][i]; vis[1] = 1; for(i = 1; i < n; i++) { k = 1; minn = INF; for(j = 0; j < n; j++) { if(!vis[j] && val[j] < minn) { minn = val[j]; k = j; } } ans += minn; vis[k] = 1; for(j = 0; j < n; j++) { if(!vis[j] && val[j] > map[j][k]) val[j] = map[j][k]; } } Pi(ans); } int main() { int t; Si(t); Wi(t){ scanf("%d%d", &n, &e); int i, j, k; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { map[i][j] = INF; } map[i][i] = 0; } int a, b, c; for(i = 0; i < e; i++) { scanf("%d%d%d", &a, &b, &c); if(map[a][b] > c) map[a][b] = map[b][a] = c; } prim(); } return 0; }