POJ--1258--
2016-05-26 13:44
357 查看
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
Sample Output
28
#include <stdio.h>
#include <string.h>
#define MaxInt 0x3f3f3f3f
#define N 110
int map
,low
,visited
;
int n;
int prim()
{
int i,j,pos,min,result=0;
memset(visited,0,sizeof(visited));
visited[1]=1;
pos=1;
for(i=1;i<=n;i++)
{
if(i!=pos)
{
low[i]=map[pos][i];
}
}
for(i=1;i<n;i++)
{
min=MaxInt;
for(j=1;j<=n;j++)
{
if(visited[j]==0 && min>low[j])
{
min=low[j];
pos=j;
}
}
result+=min;
visited[pos]=1;
for(j=1;j<=n;j++)
{
if(visited[j]==0 && low[j]>map[pos][j] && pos!=j)
{
low[j]=map[pos][j];
}
}
}
return result;
}
int main()
{
int i,v,j,ans;
while(scanf("%d",&n)!=EOF)
{
memset(map,MaxInt,sizeof(map));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&v);
map[i][j]=map[j][i]=v;
}
ans=prim();
printf("%d\n",ans);
}
return 0;
}
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
#include <stdio.h>
#include <string.h>
#define MaxInt 0x3f3f3f3f
#define N 110
int map
,low
,visited
;
int n;
int prim()
{
int i,j,pos,min,result=0;
memset(visited,0,sizeof(visited));
visited[1]=1;
pos=1;
for(i=1;i<=n;i++)
{
if(i!=pos)
{
low[i]=map[pos][i];
}
}
for(i=1;i<n;i++)
{
min=MaxInt;
for(j=1;j<=n;j++)
{
if(visited[j]==0 && min>low[j])
{
min=low[j];
pos=j;
}
}
result+=min;
visited[pos]=1;
for(j=1;j<=n;j++)
{
if(visited[j]==0 && low[j]>map[pos][j] && pos!=j)
{
low[j]=map[pos][j];
}
}
}
return result;
}
int main()
{
int i,v,j,ans;
while(scanf("%d",&n)!=EOF)
{
memset(map,MaxInt,sizeof(map));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&v);
map[i][j]=map[j][i]=v;
}
ans=prim();
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- TCP沾包解包(3)
- iOS ImageView 的填充模式
- java.text.MessageFormat 专题
- python基础(1)
- JVM虚拟机基础
- Linux下用python给某个目录下的wav文件创建相应的文本文件(标注文件)
- centos import MySQLdb 报错!
- SharedPreferences优化总结
- [翻译]Event Handler Description 事件处理描述
- hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)
- 思维题目(发现最近我的思维是越来越差了)
- android开发命名规范
- OBS外放声音重采样,用sdl播放
- 一个由于撩妹而设计出来的C/C++程序
- java通过URL读取json数据
- 配置DruidDataSource参考(com.alibaba.druid.pool.DruidDataSource)
- javascript 返回数组中 最大 最小 平均值多种方法分析比较 by FungLeo
- iOS推送的Java后台代码
- js常用事件总结及案例-onMouseOver、onMouseOut
- 10分钟搞定支付宝和微信支付的各种填坑