POJ 1258 Agri-Net
来源:互联网 发布:淘宝中评对卖家的影响 编辑:程序博客网 时间:2024/06/16 12:59
Agri-Net
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 47406 Accepted: 19615
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
USACO 102
思路:简单的最小生成树,prim搞定,不过要注意输入有多组数据
AC代码:
#include <iostream>#include <cstring>#include <cstdio>using namespace std;#define INF 0x3f3f3f3fconst int maxn=100+5;int mp[maxn][maxn],dis[maxn],vis[maxn];void prim(int n){ for(int i=0;i<n;i++) dis[i]=mp[0][i]; dis[0]=0,vis[0]=1; int ans=0; for(int i=1;i<n;i++){ int minn=INF,p=0; for(int j=0;j<n;j++){ if(!vis[j] && dis[j]<minn){ minn=dis[j]; p=j; } } ans+=minn;vis[p]=1; for(int j=0;j<n;j++){ if(!vis[j]&& dis[j]>mp[p][j]) dis[j]=mp[p][j]; } } printf("%d\n",ans);}int main(){ int t; int n; while(~scanf("%d",&n)){ memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) for(int j=0;j<n;j++) scanf("%d",&mp[i][j]); prim(n); } return 0;}
0 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
- MSSQL常用语法
- js将long日期格式转换为标准日期格式
- 协方差的意义和计算公式
- 使用 C++11 编写 Linux 多线程程序
- StringBuilder、StringBuffer和String:
- POJ 1258 Agri-Net
- 2015年你必须学习的编程语言和前端框架
- ubuntu 14.04中打开squid服务并将本机作为代理服务器
- 【百金轻】:油价跌势愈猛 投资者更不应做急单
- TML5来了,推荐7个混合式Hybrid移动开发框架
- c#异常处理机制
- React Native For Android初探
- Android 讲解:广播
- 【iOS开发】cocoaPods安装和使用(2)