poj1285 Agri-Net(最短路)
2016-07-29 17:54
211 查看
<div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Agri-Net</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 10000K</td></tr><tr><td><strong>Total Submissions:</strong> 51426</td><td width="10px"> </td><td><strong>Accepted:</strong> 21441</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">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. </div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">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.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">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.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: "Courier New", Courier, monospace;font-size:14px;">4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0Sample Output
28Source
#include<stdio.h>#include<string.h>int INF = 0x3f3f3f3f;int main(){int t;int n,m;int i,j,k;int map[550][550];int book[550];int dis[550];int sum;//scanf("%d",&t);while(~scanf("%d",&n)){sum=0;//memset(map,0,sizeof(map));memset(book,0,sizeof(book));memset(dis,0,sizeof(dis));for(i = 1; i <=n ;i++){for( j = 1; j <=n ;j++)scanf("%d",&map[i][j]);}book[1]=1;for( i = 1; i <= n; i++)dis[i] = map[1][i];int step = 1;int max = -1;int min;while(step < n){min = INF;for( i=1; i <=n; i++){if(!book[i]&&dis[i]<min){min = dis[i];j = i;}}book[j] = 1;step++;//if(max < min)// max=min;sum+=min;for( k = 1; k <= n; k++){if(!book[k] && dis[k] > map[j][k])dis[k]=map[j][k];}}printf("%d\n",sum);}return 0;}
相关文章推荐
- Android 四种启动模式,代码实验
- Android AppOpsService服务实践贴
- Randomization Services Reference
- HTTP之referer
- 分页工具
- Oracle逐行累加求和
- 内置函数:abs_all_any
- NDK学习笔记
- window 计划任务 备份数据库至备份服务器
- 雅虎崩塌,一个时代的结束
- 使用unsion完成机器文件夹容灾备份
- Android Studio 包冲突的解决方法
- POJ 2031 Building a Space Station (最小生成树)
- Android消息机制
- 面试题
- 内置函数:sort_sorted&reverse_reversed
- 字体属性设置(一):谷歌浏览器12px以下字体的显示;方法和原理
- 百度地图多个点显示在一个页面上
- Java数据类型转换
- MFC和GDI+开发电子时钟小程序