标签:
时间限制: 1 Sec 内存限制: 128 MB
提交: 177 解决: 19
[提交][状态]
2 1
1 2 3 4
3 4
最小生成树,在保证路最短的情况下,找话费最小的~Kurscal,prim不知道为啥一直runtime error
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; #define N 1100 #define INF 0xfffffff long long len, cost, f[N], n, m; struct node { long long x, y, l, c; }P[N*N]; // 如果 你写P[N]运行错误?我暂时也不知道为啥用N*N才行~ bool cmp(node a, node b) { if(a.l != b.l) return a.l < b.l; return a.c < b.c; } int found(int x) { if(f[x] != x) f[x] = found(f[x]); return f[x]; } void kurscal() { for(int i = 0; i < m; i++) { int nx = found(P[i].x), ny = found(P[i].y); if(nx != ny) { f[nx] = ny; len += P[i].l; cost += P[i].c; } } } int main() { //long long a, b, l, c; while(cin >> n >> m) { len = cost = 0; for(int i = 1; i <= n; i++) f[i] = i; for(int i = 0; i < m; i++) cin >> P[i].x >> P[i].y >> P[i].l >> P[i].c; sort(P, P+m, cmp); kurscal(); int cou = 0; for(int i = 1; i <= n; i++) { if(f[i] == i) cou++; } if(cou > 1) printf("村长你家铺子烧啦\n"); else cout << len << ‘ ‘ << cost << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4689827.html