首页 > 代码库 > POJ 1861 Kruskal
POJ 1861 Kruskal
Network
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 13456 Accepted: 5209 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 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
Source
Northeastern Europe 2001, Northern Subregion
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 13456 Accepted: 5209 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 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
Source
Northeastern Europe 2001, Northern Subregion
/******************************************* author : Grant Yuan time : 2014.7.31 algorithm: Kruskal source : POJ 1861 notice : sample is error! ********************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #define INF 0x3fffffff #define MAX1 1003 #define MAX2 15003 using namespace std; queue<int>q; int par[MAX1]; struct edge{int u;int v;int cost;}; edge e[MAX2]; int n,m,ans; int ml; bool cmp(edge aa,edge bb) { return aa.cost<bb.cost; } int findp(int aa) { if(aa==par[aa]) return aa; else return par[aa]=findp(par[aa]); } void unite(int aa,int bb) { int a1,b1; a1=findp(aa); b1=findp(bb); par[b1]=a1; } bool same(int aa,int bb) { return findp(aa)==findp(bb); } int main() { while(~scanf("%d%d",&n,&m)){ ans=0;ml=0; while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) { par[i]=i; } for(int i=1;i<=m;i++) { scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost); } sort(e+1,e+m+1,cmp); for(int i=1;i<=m;i++) { if(!same(e[i].u,e[i].v)){ unite(e[i].u,e[i].v); ans++; if(e[i].cost>ml) ml=e[i].cost; q.push(i); } } int t; printf("%d\n",ml); printf("%d\n",ans); while(!q.empty()){ t=q.front(); q.pop(); printf("%d %d\n",e[t].u,e[t].v); } } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。