poj 1258 Agri-Net
2015-08-13 20:04
204 查看
Agri-Net
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Total Submission(s) : 10 Accepted Submission(s) : 6
Problem 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模板题:代码:#include<stdio.h> #include<algorithm> using namespace std; #include<string.h> #define INF 0xfffffff #define N 105 int n,v ,map ,cost ,outcome; void prim() { int mincost,next; int i,j; outcome=0; memset(v,0,sizeof(v)); for(i=0;i<n;i++) cost[i]=map[0][i]; v[0]=1; for(j=1;j<n;j++) { mincost=INF; for(i=0;i<n;i++)//寻找最短距离 { if(v[i]==0&&mincost>cost[i]) { mincost=cost[i]; next=i; } } v[next]=1; outcome+=mincost; for(i=0;i<n;i++)//更新路径 { if(v[i]==0&&cost[i]>map[next][i]) cost[i]=map[next][i]; } } } int main() { while(scanf("%d",&n)!=EOF) { int i,j; for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&map[i][j]); prim(); printf("%d\n",outcome); } }
相关文章推荐
- OC 找出在只有空格字母的字符串中出现次数最多的单词
- POJ3213(矩阵乘法)
- POJ3213(矩阵乘法)
- Asp.Net+Easyui实现重大CRUD
- AngularJS实践(介绍)
- spring @value
- oc 关于头文件声明 与前向声明
- UIControl和它的子类
- int型数据在内存中存储时1的个数
- 发送验证码倒计时
- php 处理上百万条的数据库如何提高处理查询速度
- 递推DP HDOJ 5389 Zero Escape
- HDU 5381 The sum of gcd
- 在字符串中找出连续最长的数字串 .
- 决策树之ID3、C4.5、C5.0等五大算法
- 又见01背包
- android 事件处理机制之requestDisallowInterceptTouchEvent
- Linux2.6.38内核启动流程分析
- hdu 1198Farm Irrigation
- 【华为机试题】亮着电灯的盏数