您的位置:首页 > 其它

poj-1861-MST

2014-06-27 22:53 148 查看
最近做了很多最小生成树的题,选择这个作为记录

题意:连接所有的点,使最大的花费尽量的小,输出最大的花费,连接的数目,和连接的情况。

解法:kruskal

1. 1258 最经典的MST★ Yes
2. 1789 Truck History 最小生成树★ Yes
3. 1287 Networking 简单★ Yes
4. 2349 Arctic Network 简单★ Yes
5. 1611 The Suspects 并查集★ Yes
6. 2377 kruskal★ Yes
7. 2524 Ubiquitous Religions 并查集★ Yes
8. 2236 Wireless Network 并查集+计算几何★ Yes
9. 2560 Kruskal 并查集★ Yes
10. 1861 Kruskal ★ Yes
#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 <deque>

#define maxn 20005
#define ull unsigned long long
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001

const ull inf = 1LL << 61;

using namespace std;
vector<pair<int,int> >v;
struct node
{
int be,en;
int val;
}tree[maxn];
bool cmp(node a,node b){
return a.val<b.val;
}
int fat[1005];
void init()
{
int i;
rep(i,1005)
{
fat[i]=i;
}
}
int getfat(int x)
{
if(fat[x]==x)return x;
else return fat[x]=getfat(fat[x]);
}
int main()
{
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(cin>>n>>m)
{
v.clear();
int i,j,k;
rep(i,m)
{
cin>>tree[i].be>>tree[i].en>>tree[i].val;
}
init();
sort(tree,tree+m,cmp);
int ans=0;
int minnum=0;
for(i=0;i<m;i++)
{
int x=getfat(tree[i].be);
int y=getfat(tree[i].en);
if(x==y)continue;
fat[x]=y;
ans+=tree[i].val;
v.push_back(make_pair(tree[i].be,tree[i].en));
minnum=max(minnum,tree[i].val);
}
cout<<minnum<<endl;
cout<<v.size()<<endl;
vector<pair<int,int> >::iterator iter;
for(iter=v.begin();iter!=v.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: