首页 > 代码库 > poj 1861 Network 解题报告

poj 1861 Network 解题报告

Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 16171 Accepted: 6417 Special Judge

Description

Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs). 
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

The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.

Output

Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.

Sample Input

4 61 2 11 3 11 4 22 3 13 4 12 4 1

Sample Output

141 21 32 33 4

Source

Northeastern Europe 2001, Northern Subregion

[Submit]   [Go Back]   [Status]   [Discuss]

技术分享Home Page   技术分享Go Back  技术分享To top

————————————————我是分割线————————————————

最小生成树。

一开始没看到Special judge

发现与样例输出不同,以为自己错了,改了很长时间

最后受不了了强交一波 结果过了 TAT

一口毒奶喷了出来..........................

仔细读题果然必要,即使是题目的题目。

话说为什么这么多题都叫network。。。

 

技术分享
  1 /*  2     Problem:  3     OJ:  4     User:  5     Time:  6     Memory:  7     Length:  8 */  9 #include<iostream> 10 #include<cstdio> 11 #include<cstring> 12 #include<cmath> 13 #include<algorithm> 14 #include<queue> 15 #include<cstdlib> 16 #include<iomanip> 17 #include<cassert> 18 #include<climits> 19 #include<vector> 20 #include<list> 21 #include<map> 22 #define maxn 15001 23 #define F(i,j,k) for(int i=j;i<=k;i++) 24 #define M(a,b) memset(a,b,sizeof(a)) 25 #define FF(i,j,k) for(int i=j;i>=k;i--) 26 #define inf 0x7fffffff 27 #define maxm 2016 28 #define mod 1000000007 29 //#define LOCAL 30 using namespace std; 31 int read(){ 32     int x=0,f=1;char ch=getchar(); 33     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();} 34     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();} 35     return x*f; 36 } 37 int n,m; 38 struct EDGE 39 { 40     int from; 41     int to; 42     int value; 43 }e[maxn]; 44 int fa[maxn]; 45 int ans[maxn],cnt; 46 int num,maxedge; 47 inline void init() 48 { 49     for(int i=1;i<=n;i++) fa[i]=-1; 50 } 51 inline int find(int u) 52 { 53     int i; 54     for(i=u;fa[i]>=0;i=fa[i]); 55     while(i!=u){//路径压缩  56         int temp=fa[u]; 57         fa[u]=i; 58         u=temp; 59     } 60     return i; 61 } 62 inline void Union(int a,int b) 63 { 64     int aa=find(a),bb=find(b); 65     int temp=fa[aa]+fa[bb]; 66     if(fa[aa]>fa[bb])//按秩合并  67     { 68         fa[aa]=bb; 69         fa[bb]=temp; 70     } 71     else{ 72         fa[bb]=aa; 73         fa[aa]=temp; 74     } 75 } 76 int comp(const void *a,const void *b) 77 { 78     EDGE aa=*(const EDGE *)a; 79     EDGE bb=*(const EDGE *)b; 80     return aa.value-bb.value; 81 } 82 int cmp(EDGE a,EDGE b) 83 { 84     return a.value<b.value; 85 } 86 inline void kruskal() 87 { 88     cnt=0; 89     int u,v; 90     init(); 91     for(int i=0;i<m;i++) 92     { 93         u=e[i].from;v=e[i].to; 94         if(find(u)!=find(v)) 95         { 96             ans[cnt]=i;cnt++; 97             if(e[i].value>maxedge) 98                 maxedge=e[i].value; 99             num++;100             Union(u,v);    101         }102         if(num>=n-1) break;103     }104 } 105 int main()106 {107     std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;108     #ifdef LOCAL109     freopen("data.in","r",stdin);110     freopen("data.out","w",stdout);111     #endif112     while(cin>>n>>m)113     {114         for(int i=0;i<m;i++) cin>>e[i].from>>e[i].to>>e[i].value;115         sort(e,e+m,cmp);116 //        qsort(e,m,sizeof(e[0]),comp);117         maxedge=0;num=0;118         kruskal();119         cout<<maxedge<<endl;120         cout<<num<<endl;121         for(int i=0;i<num;i++) cout<<e[ans[i]].from<<" "<<e[ans[i]].to<<endl;122     }123     return 0;124 }
poj 1861

 

poj 1861 Network 解题报告