poj1258
来源:互联网 发布:梦想小镇mac无限绿钞 编辑:程序博客网 时间:2024/04/28 23:44
Description
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
Output
Sample Input
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
#include<stdio.h>
#include<stdlib.h>
#define M 109
struct node{
int a,b,dist;
}road[M*(M-1)/2+1];
int f[109];
int find(int x)
{
if(x==f[x])
return x;
return find(f[x]);
}
int cmp(const void *a,const void *b)
{
struct node *c,*d;
c=(struct node*)a;
d=(struct node*)b;
return c->dist-d->dist;
}
int main()
{
int i,j,k,n,map[109][109],fa,fb;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
for(i=1;i<=n;i++)
f[i]=i;
k=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(map[i][j]!=0&&i>j)
{
road[k].a=i;
road[k].b=j;
road[k].dist=map[i][j];
k++;
}
qsort(road,k,sizeof(road[0]),cmp);
int sum=0;
for(i=0;i<k;i++)
{
fa=find(road[i].a);
fb=find(road[i].b);
if(fa!=fb)
{
sum+=road[i].dist;
f[fa]=fb;
}
}
printf("%d\n",sum);
}
return 0;
}
- poj1258
- poj1258
- POJ1258
- poj1258
- poj1258
- poj1258
- poj1258
- poj1258
- POJ1258
- poj1258
- POJ1258
- poj1258
- poj1258
- poj1258
- poj1258
- poj1258
- POJ1258
- poj1258
- socket
- 烟雨帘,等一个睛天
- 感冒了
- 强连通图的算法
- 设计模式C++实现(13)——中介者模式
- poj1258
- 简单的dp@poj(2)2479最大子段和
- jQuery:将文本转化成JSON对象应注意的问题
- Android入门第十四篇之画图
- Your content must have a ListView whose id attribute is android.R.id.list和ListView注意
- 程序员 未来 规划
- 求砖拍:绝对深入剖析各种方法实现两个变量的交换
- Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
- 没想到还真的是你啊 umask