【最小生成树】poj1258
Agri-Net
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
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();
}
}