Agri-Net
2015-08-13 21:37
239 查看
Agri-Net
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Total Submission(s) : 8 Accepted Submission(s) : 4
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
[align=left]Sample Output[/align]
28
题解:最小生成树的全脂和;
代码:
#include<stdio.h> #include<string.h> const int INF=0x3f3f3f3f; const int MAXN=110; //#define MAX(x,y) (x>y?x:y) int vis[MAXN],map[MAXN][MAXN],low[MAXN]; int N,answer; void prime(){ memset(vis,0,sizeof(vis)); int flot=1,temp,k; answer=0; vis[1]=1; for(int i=1;i<=N;i++)low[i]=map[1][i]; for(int i=1;i<=N;i++){ temp=INF; for(int j=1;j<=N;j++) if(!vis[j]&&temp>low[j])temp=low[k=j]; if(temp==INF){ // if(flot!=N)ans=0; // printf("flot=%d,N=%d\n",flot,N); break; } answer+=temp; vis[k]=1; flot++; for(int j=1;j<=N;j++){ if(!vis[j]&&low[j]>map[k][j])low[j]=map[k][j]; } } } int main(){ int T,a; while(~scanf("%d",&N)){ memset(map,INF,sizeof(map)); for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ scanf("%d",&a); if(j>i){ if(a<map[i][j])map[i][j]=map[j][i]=a; } } } prime(); printf("%d\n",answer); } return 0; }
相关文章推荐
- **HDU 5378 - Leader in Tree Land(概率DP)
- Excel列名 字母和数字的转换
- CodeForces-554A Kyoya and Photobooks
- 【POJ 1260】Pearls
- Linux C编程的DEBUG宏
- 可修改主席树
- linux 之静默安装oracle
- HDU5389.Zero Escape
- 黑马程序员-----------C语言基础-----------数组
- C++ STL::bitset
- ZOJ 3469 Food Delivery
- 自动装箱拆箱
- 通过应用程序域AppDomain加载和卸载程序集
- Hibernate 基本配置和应用
- sql中的exists
- hdu 2515 Yanghee 的算术
- poj 3345 Bribing FIPA 【树形dp + 01背包】
- 让 Terminal/vim 使用 Solarized 配色
- 深入理解TCP(二)
- J-Link clone问题