POJ-1258-Agri-Net
来源:互联网 发布:淘宝官方电话人工服务 编辑:程序博客网 时间:2024/06/06 05:29
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 59930 Accepted: 24821
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
40 4 9 214 0 8 179 8 0 1621 17 16 0
Sample Output
28
Source
个人理解:图-最小生成树
代码AC情况:
C语言:
#include<stdio.h># define N 101# define MAX 100000int tu[N][N];//储存图char vis[N];//标记数组 标记当前村庄是否走过int Min_Room(int n)//找到离已经连好了的村庄最短距离的村庄的标号{ int i,m=MAX,k=-1; for(i=0;i<n;i++) if(!vis[i]&&tu[0][i]<m)//如果当前的村庄没有走 并且其距离<已知的其他村庄的距离 { m=tu[0][i];//记录当前的距离 k=i;//记录村庄号 } return k;}int main(){ int i,j,k,n,sum; //freopen("AAA.txt","r",stdin); while(scanf("%d",&n)!=EOF) { for(sum=i=0;i<n;i++) for(vis[i]=j=0;j<n;j++) scanf("%d",&tu[i][j]);//录入地图路费 vis[0]=1;//从第1个村庄开始 for(i=0;i<n-1;i++)//找出n-1条最短边 { k=Min_Room(n);//找到离已经连好了的村庄最短距离的村庄的标号 vis[k]=1;//标记当前村庄已走过 sum+=tu[0][k];//加上权值 for(j=0;j<n;j++)//找到一个新的点,马上更新最小距离。 if(tu[k][j]<tu[0][j]) tu[0][j]=tu[k][j]; } printf("%d\n",sum); } return 0;}
阅读全文
1 0
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj 1258-Agri-net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ+1258++Agri-Net
- Poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- poj 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- POJ 1258 Agri-Net
- poj-1258-Agri-Net
- poj-1258 Agri-Net
- Poj 1258 Agri-Net
- POJ 1258 - Agri-Net
- 【复习笔记】听说找工作一定要会的三种排序算法
- FFMPEG学习【libpostproc】
- 博弈算法
- 猜数字
- 【git】常用命令汇总
- POJ-1258-Agri-Net
- 209. Minimum Size Subarray Sum
- object-fit
- HDU6053 前缀和+容斥
- 数据库笔记
- ACSII码排序
- java+discuz部署和通信问题总结
- Case:接口实现类的方法上使用AspectJ方式实现aop的异常问题
- 回形取数