POJ 1258 ——Agri-Net 克鲁斯卡尔算法
来源:互联网 发布:做账软件培训 编辑:程序博客网 时间:2024/06/05 07:29
Agri-Net
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 41840
Accepted: 17056
Memory Limit: 10000KTotal Submissions: 41840
Accepted: 17056
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
最小生成树,又是TLE。。。。。。。手残,手残------------>脑残。。。。。。忘了加!=EOF。。。。。
后来加上才AC。。。。
#include <stdio.h>#include <string.h>#include <algorithm>const int inf = 0x3f3f3f3f;struct node{ int x; int y; int w;}ls[100100];int s[1100];bool cmp(node a,node b){ return a.w < b.w;}int nfind(int x){ int r = x,q; while(x != s[x]) x = s[x]; while(s[r] != x) { q = s[r]; s[r] = x; r = q; } return x;}bool lj(int x,int y){ int fx = nfind(x); int fy = nfind(y); if(fx != fy) { s[fx] = fy; return false; } return true;}void klskr(int num){ int sum = 0; std::sort(ls,ls + num,cmp); for(int i = 0;i < num;i++) { if(!lj(ls[i].x,ls[i].y)) sum += ls[i].w; } printf("%d\n",sum);}int main(){ int n,num; while(~scanf("%d",&n)) { num = 0; for(int i = 0;i <= n;i++) s[i] = i; for(int i = 1;i <= n;i++) { for(int j = 1;j <= n;j++) { if(j <= i) { scanf("%*d"); continue; } ls[num].x = i; ls[num].y = j; scanf("%d",&ls[num].w); num++; } } klskr(num); } return 0;}
0 0
- POJ 1258 ——Agri-Net 克鲁斯卡尔算法
- 克鲁斯卡尔(Kruskal)算法 Agri-Net
- USACO 3.1 Agri-Net 最短网络 (最小生成树)(克鲁斯卡尔算法)
- POJ 1861 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 克鲁斯卡尔算法
- 【POJ】1258 - Agri-Net(克鲁斯塔尔)(水)
- HDOJ 1863 畅通工程——克鲁斯卡尔算法
- 最小生成树—克鲁斯卡尔算法
- 仿天天动态上拉播放界面控件
- computer vision:algorithm and application2
- HDU 4686 Arc of Dream 矩阵快速幂
- rsync+inotify文件实时同步
- 动态链接库理解(二)
- POJ 1258 ——Agri-Net 克鲁斯卡尔算法
- 用 greenlet 协程处理异步事件
- 关联分析LD计算的问题
- 小小程序员加油
- JBOSS系列(一) --EJB远程调用-客户端的配置
- POJ 2472 &&ZOJ 2797 (106 miles to Chicago)
- 脚本语言
- C++、Java、JavaScript中的异常处理(Exception)
- 路由(Routing)与zigbee原语