POJ 1258 Agri-Net(kruskal或prim+heap)
Time Limit: 1000MS | Memory Limit: 10000K | |
---|---|---|
Total Submissions: 68496 | Accepted: 28392 |
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.
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
题意
求给出的n*n图中的mst。
解题思路
听说prim+heap在稀疏图里会快很多,就来学了学。没什么注意的,直接上代码了。
代码
prim+heap算法
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int maxn = 1e2+50;
const int inf = 0x3f3f3f3f;
struct node
{
int to,val;
node() {}
node(int a,int b):to(a),val(b) {}
friend bool operator < (node a,node b)
{
return a.val>b.val;
}
};
vector<node> v[maxn];
int flag[maxn],minn[maxn],n;
void prim_heap()
{
memset(flag,0,sizeof flag);
memset(minn,inf,sizeof minn);
int num=0,ans=0;
priority_queue<node> q;
q.push(node(0,0));
while(!q.empty())
{
node now=q.top();
q.pop();
if(flag[now.to]) continue;
flag[now.to]=1;
num++,ans+=now.val;
if(num==n)
{
printf("%d\n",ans);
return;
}
int len=v[now.to].size();
for(int i=0; i<len; i++)
{
node next=v[now.to][i];
if(flag[next.to]) continue;
if(next.val<minn[next.to])
{
minn[next.to]=next.val;
q.push(next);
}
}
}
printf("-1\n");
return;
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=0; i<=n; i++) v[i].clear();
int tmp;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
scanf("%d",&tmp);
v[i].push_back(node(j,tmp));
}
prim_heap();
}
return 0;
}
kruskal算法
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
const int maxn = 1e2+50;
const int inf = 0x3f3f3f3f;
struct node
{
int x,y,val;
node() {}
node(int a,int b,int c):x(a),y(b),val(c) {}
} arr[maxn*maxn];
int p[maxn],n;
int cmp(node a,node b)
{
return a.val<b.val;
}
int finds(int x)
{
return p[x]==x?x:p[x]=finds(p[x]);
}
void kruskal()
{
for(int i=0; i<n; i++) p[i]=i;
int cnt=0,ans=0;
for(int i=0; i<n*n; i++)
{
int xx=finds(arr[i].x);
int yy=finds(arr[i].y);
if(xx!=yy)
{
p[xx]=yy;
cnt++;
ans+=arr[i].val;
}
if(cnt==n-1)
{
printf("%d\n",ans);
return;
}
}
printf("-1\n");
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
int tmp,cnt=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
scanf("%d",&tmp);
arr[cnt++]=node(i,j,tmp);
}
sort(arr,arr+cnt,cmp);
kruskal();
}
return 0;
}