码迷,mamicode.com
首页 > Web开发 > 详细

POJ 1861 Network

时间:2014-11-20 20:18:47      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   java   

Network

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 1861
64-bit integer IO format: %lld      Java class name: Main
 
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

 
解题:最小生成树。。。题目看起来很唬人啊。。。
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 20000;
18 struct arc{
19     int u,v,w;
20     bool operator<(const arc &y) const {
21         return w < y.w;
22     }
23 };
24 arc e[maxn];
25 int n,m,uf[maxn],ans[maxn],tot;;
26 int Find(int x){
27     if(x != uf[x]) uf[x] = Find(uf[x]);
28     return uf[x];
29 }
30 int kruskal(){
31     for(int i = 0; i < 1500; ++i) uf[i] = i;
32     int maxw = tot = 0;
33     for(int i = 0; i < m; ++i){
34         int tx = Find(e[i].u);
35         int ty = Find(e[i].v);
36         if(tx != ty){
37             uf[tx] = ty;
38             ans[tot++] = i;
39             maxw = max(maxw,e[i].w);
40         }
41     }
42     return maxw;
43 }
44 
45 int main() {
46     while(~scanf("%d %d",&n,&m)){
47         for(int i = 0; i < m; ++i)
48             scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
49         sort(e,e+m);
50         printf("%d\n%d\n",kruskal(),tot);
51         for(int i = 0; i < tot; ++i)
52             printf("%d %d\n",e[ans[i]].u,e[ans[i]].v);
53     }
54     return 0;
55 }
View Code

 

POJ 1861 Network

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/crackpotisback/p/4111355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!