poj 1861 Network
Time Limit: 1000MS | Memory Limit: 30000K | |||
Total Submissions: 13260 | Accepted: 5119 | Special Judge |
Description
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.
Input
Output
Sample Input
4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1
Sample Output
1 4 1 2 1 3 2 3 3 4
题意:给出节点个数n和边数m,下面m行给出边(x,y)以及权值p。输出第一行为最小生成树中的最大边权值,第二行为一个可行生成树方案的边数k,下面k行为可行生成树的k条边。
题目是Special Judge
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; #define M 1500000 int f[M]; struct node { int x,y,l; }p[M],v[M]; bool cmp(node a,node b) { return (a.l<b.l); } void Init (int n) { for(int i=1;i<=n;i++) f[i]=i; } int find(int x) { if(x!=f[x]) return f[x]=find(f[x]); return f[x]; } void Union(int x,int y) { int fx,fy; fx=find(x); fy=find(y); if(fx!=fy) f[fy]=fx; } int main () { int n,m; int i; cin>>n>>m; Init(n); for(i=0;i<m;i++) cin>>p[i].x>>p[i].y>>p[i].l; sort(p,p+m,cmp); int k=0; int max=0; for(i=0;i<m;i++) // 寻找最小生成树 { int k1=find(p[i].x); int k2=find(p[i].y); if(k1!=k2) { k++; // 边的条数 Union(k1,k2); v[k]=p[i]; // 记录可行生成树的边 if(p[i].l>max) max=p[i].l; // 记录最大的边权值 } } cout<<max<<endl<<k<<endl; for(i=1;i<=k;i++) cout<<v[i].x<<' '<<v[i].y<<endl; return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。