Agri-Net
来源:互联网 发布:和外国人谈恋爱知乎 编辑:程序博客网 时间:2024/06/08 10:41
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.
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.
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.
40 4 9 214 0 8 179 8 0 1621 17 16 0
28
#include<stdio.h>#define inf 0x3f3f3f3fint main(){ int t, n, i, j, temp, e[505][505], book[505], dis[505]; while(scanf("%d", &n)!=EOF) { int a, minn, ans=0, sum=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) e[i][j]=inf; for(i=1;i<=n;i++) book[i]=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { scanf("%d", &a); if(a<e[i][j]) e[i][j]=e[j][i]=a; } for(i=1;i<=n;i++) dis[i]=e[1][i]; book[1]=1; for(i=1;i<n;i++) { minn=inf; for(j=1;j<=n;j++) if(book[j]==0&&dis[j]<minn) { minn=dis[j]; temp=j; } book[temp]=1; ans+=dis[temp]; for(j=1;j<=n;j++) if(book[j]==0&&dis[j]>e[temp][j]) dis[j]=e[temp][j]; } printf("%d\n", ans); } return 0;}
阅读全文
0 0
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Agri-Net
- Highways
- [面试]简介nginx的fcgi
- 宿主机与docker容器之间免密码ssh链接,容器与容器之间ssh免密码连接
- Oracle加解密_解决编码问题_解决中文截断问题
- LeetCode-62. Unique Paths
- Agri-Net
- java.nio.channels.CancelledKeyException
- Data Analytics for Beginners:第二节
- oracle中临时表的创建【会话级别】
- 3.2 Molar Mass UVa1586
- PTA-愿天下有情人都是失散多年的兄妹 C语言实现
- MD5编码
- excel中的条件匹配
- 媒体查询