zoj 1542 Network
2013-10-09 09:41
169 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1542
解题思路:最小生成树(kruskal 算法 + 优先队列 + 并查集)
解题思路:最小生成树(kruskal 算法 + 优先队列 + 并查集)
/************************************************************************** user_id: SCNU20102200088 problem_id: zoj 1542 problem_name: Network **************************************************************************/ #include <algorithm> #include <iostream> #include <iterator> #include <iomanip> #include <sstream> #include <fstream> #include <cstring> #include <cstdlib> #include <climits> #include <bitset> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <list> #include <set> #include <map> using namespace std; //线段树 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 //手工扩展栈 #pragma comment(linker,"/STACK:102400000,102400000") const double EPS=1e-9; const double PI=acos(-1.0); const double E=2.7182818284590452353602874713526; //自然对数底数 const double R=0.5772156649015328606065120900824; //欧拉常数:(1+1/2+...+1/n)-ln(n) const int x4[]={-1,0,1,0}; const int y4[]={0,1,0,-1}; const int x8[]={-1,-1,0,1,1,1,0,-1}; const int y8[]={0,1,1,1,0,-1,-1,-1}; typedef long long LL; typedef int T; T max(T a,T b){ return a>b? a:b; } T min(T a,T b){ return a<b? a:b; } T gcd(T a,T b){ return b==0? a:gcd(b,a%b); } T lcm(T a,T b){ return a/gcd(a,b)*b; } /////////////////////////////////////////////////////////////////////////// //Add Code: int n,s[1005]; struct Node{ int u,v,w; bool operator <(const Node &a) const{ return w>a.w; } }node; priority_queue<Node> pq; void Union(int x,int y){ s[y]=x; } int Find(int x){ if(s[x]<0) return x; return s[x]=Find(s[x]); } void Kruskal(){ int num=0,ret=0,i,x[1005],y[1005]; memset(s,-1,sizeof(s)); while(!pq.empty() && num<n-1){ node=pq.top(); pq.pop(); if(Find(node.u)!=Find(node.v)){ Union(Find(node.u),Find(node.v)); ret+=node.w; num++; x[num]=node.u; y[num]=node.v; } } printf("%d\n",node.w); printf("%d\n",num); for(i=1;i<=num;i++) printf("%d %d\n",x[i],y[i]); while(!pq.empty()) pq.pop(); } /////////////////////////////////////////////////////////////////////////// int main(){ std::ios::sync_with_stdio(false); //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); /////////////////////////////////////////////////////////////////////// //Add Code: int m,i; while(scanf("%d%d",&n,&m)!=EOF){ for(i=1;i<=m;i++){ scanf("%d%d%d",&node.u,&node.v,&node.w); pq.push(node); } Kruskal(); } /////////////////////////////////////////////////////////////////////// return 0; } /************************************************************************** Testcase: Input: 4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1 Output: 1 3 1 2 2 4 1 3 **************************************************************************/
相关文章推荐
- ZOJ 1542 Network
- POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)
- ZOJ 1542 Network(裸MST)
- network 最小生成树 Kruskal 算法 poj 1861 zoj 1542
- zoj 1542 Network
- zoj 1542 || poj 1861 Network(MST = = Kruskal~)
- zoj 1542 Network
- zoj 1542 Network
- zoj 1542 Network(最小生成树))
- 【最小生成树】 ZOJ 1542 Network
- ZOJ 1542 poj 1861 Network(并查集+最小树)
- zoj 1542 Network
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- 【最小生成树】 ZOJ 1542 Network
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- ZOJ 1542 poj 1861 Network(并查集+最小树)
- ZOJ 1542 / POJ 1861 Network (kruskal )
- POJ 1861/ZOJ 1542 Network
- ZOJ - 1542 Network
- zoj 1542 Network【kruskal】