Agri-Net-并查集(最小生成树)
2016-04-21 18:53
369 查看
http://poj.org/problem?id=1258Agri-Net
DescriptionFarmer 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.
InputThe 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.OutputFor 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 Input4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output28SourceUSACO 102简单的并查集-克鲁斯卡尔算法#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 110
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
struct edge{
int s,e;
int dist;
}e[10000];
bool cmp(edge a,edge b)
{
return a.dist<b.dist;
}
int fa[maxn];
void init(int n){
for(int i=0;i<=n;i++)
fa[i]=i;
}
int findfa(int x){
if(fa[x]==x)return x;
return fa[x]=findfa(fa[x]);
}
void Union(int x,int y){
fa[x]=y;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,d;
while(cin>>t){
init(t);
int k=0;
for(int i=1;i<=t;i++)
for(int j=1;j<=t;j++){
scanf("%d",&d);
if(i!=j){
k++;
e[k].s=i;
e[k].e=j;
e[k].dist=d;
}
}
sort(e+1,e+k+1,cmp);
ll sum=0;
for(int i=1;i<=k;i++){
int u=findfa(e[i].s);
int v=findfa(e[i].e);
if(u!=v){
sum+=e[i].dist;
Union(u,v);
}
}
printf("%I64d\n",sum);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42267 | Accepted: 17261 |
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.
InputThe 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.OutputFor 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 Input4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output28SourceUSACO 102简单的并查集-克鲁斯卡尔算法#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 110
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
struct edge{
int s,e;
int dist;
}e[10000];
bool cmp(edge a,edge b)
{
return a.dist<b.dist;
}
int fa[maxn];
void init(int n){
for(int i=0;i<=n;i++)
fa[i]=i;
}
int findfa(int x){
if(fa[x]==x)return x;
return fa[x]=findfa(fa[x]);
}
void Union(int x,int y){
fa[x]=y;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,d;
while(cin>>t){
init(t);
int k=0;
for(int i=1;i<=t;i++)
for(int j=1;j<=t;j++){
scanf("%d",&d);
if(i!=j){
k++;
e[k].s=i;
e[k].e=j;
e[k].dist=d;
}
}
sort(e+1,e+k+1,cmp);
ll sum=0;
for(int i=1;i<=k;i++){
int u=findfa(e[i].s);
int v=findfa(e[i].e);
if(u!=v){
sum+=e[i].dist;
Union(u,v);
}
}
printf("%I64d\n",sum);
}
return 0;
}
相关文章推荐
- Memcached 使用记录(以及解决数据不同步的问题的方案)
- Post Office
- FatMouse\' Trade -HUD1009
- Piggy-Bank 完全背包问题
- How many ways 记忆化搜索
- Humble Numbers 丑数
- 关于图的深度优先的递归和非递归算法
- 搬寝室
- Common Subsequence(LCS)
- 最少拦截系统
- HDU 2577 How to Type
- Modified GCD
- A. Winner
- 免费馅饼
- 数塔
- 命运
- HDU 2602 Bone Collector
- HDU 4706 Children\'s Day
- HDU 1551 Cable master
- HDU 4708 Rotation Lock Puzzle