Description
Input
Output
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
裸的最小生成树,分别用prim, kruskal, prim + priority_queue做了,那天有心情可以再写个prim + 手工堆
kruskal
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct ty { long x, y, l; }; long n; long fa[110]; ty a[10010]; long const INF = 1000000; bool comp (ty a, ty b) { return (a.l < b.l); } long findfa(long x) { if (fa[x] == x) return x; return fa[x] = findfa(fa[x]); } int main() { freopen("poj1258.in", "r", stdin); while (scanf("%d\n", &n) != EOF) { long m = 0; memset(a, 0 ,sizeof(a)); for (long i = 1; i <= n; i++) for (long j = 1; j <= n; j++) { long tmp; scanf("%d ", &tmp); if ((tmp != 0) && (i < j)) { a[m].x = i; a[m].y = j; a[m].l = tmp; m++; } } m++; sort (a, a + m, comp); for (long i = 1; i <= n; i++) fa[i] = i; long cnt = 0; long sum = 0; for (long i = 0; i < m; i++) { long fx = findfa(a[i].x); long fy = findfa(a[i].y); if (fx != fy) { fa[fx] = fy; cnt++; sum += a[i].l; if (cnt >= n - 1) break; } } printf("%d\n", sum); } return 0; }
prim:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; long n; long a[110][110]; long dist[110]; bool v[110]; long const INF = 1000000; void prim() { memset(v, 0, sizeof(v)); v[1] = 1; for (long i = 1; i <= n; i++) { dist[i] = a[1][i]; // cout << dist [i]<<' '; } //cout << endl; long sum = 0; for (long k = 1; k < n; k++) { long minn = INF; long point = 0; for(long i = 1; i <= n; i++) { if((!v[i]) && (dist[i] < minn)) { minn = dist[i]; point = i; } } v[point] = true; sum += dist[point]; //cout << sum <<endl; dist[point] = INF; for (long i = 1; i <= n; i++) { if (!v[i]&&(a[point][i] < dist[i])) dist[i] = a[point][i]; // cout << dist [i]<<' '; } // cout << endl; } printf("%d\n", sum); } int main() { freopen("poj1258.in", "r", stdin); while (scanf("%d\n", &n) != EOF) { for (long i = 1; i <= n; i++) { for (long j = 1; j <= n; j++) { scanf("%d ", &a[i][j]); } } prim(); } return 0; }
prim + priority_queue:
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; const long N = 110, M=10100; struct ty { long t, w, next; bool operator < (const ty &a) const { if (w > a.w) return true; else return false; } }edge[M*2]; priority_queue <ty> q; long n, dist[N], head[N], cnt; bool v[N]; void insertedge(long x, long y, long z, long k) { edge[k].t = y; edge[k].next = head[x]; edge[k].w = z; head[x] = k; } void init() { long m = 0; for(long i = 1; i <= n; i++) { for(long j = 1; j <= n; j++) { long tmp; scanf("%d", &tmp); insertedge(i, j, tmp, ++m); insertedge(j, i, tmp, ++m); } } } void prim() { long result=0; ty t1, t2; memset(dist, 127, sizeof(dist)); dist[1] = 0; for (long i = head[1]; i != 0; i = edge[i].next) { if (edge[i].w < dist[edge[i].t]) { dist[edge[i].t] = edge[i].w; q.push(edge[i]); } } while( !q.empty() ) { t1 = q.top(); q.pop(); if (dist[t1.t] == 0) continue; result += t1.w; dist[t1.t] = 0; for (long j = head[t1.t]; j != 0; j = edge[j].next) { long u = edge[j].t; if ((dist[u] != 0) && (dist[u] > edge[j].w)) { dist[u] = edge[j].w; t2.t = u; t2.w = dist[u]; q.push(t2); } } } cout << result << endl; } int main() { freopen("poj1258.in", "r", stdin); while (scanf("%d", &n) != EOF) { memset(edge, 0, sizeof(edge)); memset(head, 0, sizeof(head)); init(); prim(); } }