poj-1861-MST
2014-06-27 22:53
148 查看
最近做了很多最小生成树的题,选择这个作为记录
题意:连接所有的点,使最大的花费尽量的小,输出最大的花费,连接的数目,和连接的情况。
解法:kruskal
题意:连接所有的点,使最大的花费尽量的小,输出最大的花费,连接的数目,和连接的情况。
解法: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; }
相关文章推荐
- POJ 1861 - MST - Krustral
- POJ 1861 Network (MST kruskal 加权合并 (瓶颈生成树))
- POJ 1861 Network [最小生成树算法MST-kruskal 数据结构-并查集 union-find sets]
- POJ 1861 Network (Kruskal求MST模板题)
- POJ 1861 Network(MST)
- zoj1542||poj 1861(简单-MST)
- POJ 1861 Network (Kruskal求MST模板题)
- zoj 1542 || poj 1861 Network(MST = = Kruskal~)
- poj 1861 Network MST
- POJ 1861 Network (MST)
- poj 1861 Network[ MST ]
- POJ--1679--The Unique MST【推断MST是否唯一】
- POJ1861-Network(Kruskal)
- poj_1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST(最小树不唯一的判定)
- POJ 1861 Network(最小瓶颈生成树)
- POJ 1251 Jungle Roads(超裸MST)
- POJ1679 The Unique MST
- poj 1861 Network(图论:最小生成树)
- POJ 1679 The Unique MST (prim判断最小生成树是否唯一)