CCF-CSP-201703-4- subway construction

Problem Description
  A transportation hub city has n where n No. 1 and No. very important, in order to enhance transport capacity, between n A city decided to build a subway hub at No. 1.
  Subway tunnel composed by a number of segments, each tunnel connecting the two transport hubs. After exploration, there are m sections of the tunnel as a candidate, the candidate most only one tunnel between two transport hub, not connected to the ends of the tunnel with a transport hub.
  There are tunnel construction company n home, each candidate by the construction of the tunnel is only a company, the same number of days each company's construction needs. And each company can only build up to a candidate tunnel. All company also began construction.
  As project leader, you get information about the candidate of the tunnel, now you can choose according to their own ideas part of the tunnel construction, will build the entire subway requires a minimum number of days.
Input Format
  The first line of input contains two integers n-m , separated by a space, respectively, and the number of candidate traffic tunnel hub.
  2 through line m + 1'd rows each comprising three integers abc , represents the hub a and hub b can build a tunnel between the time required for c days.
Output Format
  Output an integer, the number of days the whole construction requires a minimum of subway lines.
Sample input
6 6
1 2 4
2 3 4
3 6 7
1 4 2
4 5 5
5 6 6
Sample Output
6
Sample Description
  There are two lines can be built.
  Sequentially through a first hub 1, 2, 3, 6, the time required respectively 4, 4, 7, the entire subway lines completion or 7 days;
  the second order of 1 through hub, 4 , 5, 6, the time required are 2, 5, 6, the entire subway line 8 days completion.
  The number of days used in a second embodiment less.
Evaluation scale cases and agreed with
  For evaluation by 20% in Example,. 1 ≤  n-  ≤ 10,1 ≤  m  ≤ 20 is;
  for 40% of cases with evaluation,. 1 ≤  n-  ≤ 100,1 ≤  m  ≤ 1000;
  for reviews by 60% in Example,. 1 ≤  n-  ≤ 1000 ,. 1 ≤  m  ≤ 10000,1 ≤  C  ≤ 1000;
  for reviews with 80% of cases,. 1 ≤  n-  ≤ 10000,1 ≤  m  ≤ 100000;
  to 100% of the cases with the evaluation,. 1 ≤  n-  ≤ 100000,1 ≤  m  ≤ 200000 ,. 1 ≤  AB  ≤  n- ,. 1 ≤  C  ≤ 1000000.

  Reviews in all use cases to ensure that all candidate No. 1 tunnel for repair through the hub can reach all the other hubs through the tunnel.
 
 
 
After sorting kept bordered edge until 1 and 6 Unicom, the right output at this time last added value that edge is the answer. With disjoint-set to maintain connectivity.
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define inf 0x3f3f3f3f
 4 #define pii pair<int,int>
 5 #define mp make_pair
 6 #define x first
 7 #define y second
 8 
 9 const int maxn=100010;
10 const int maxm=200020;
11 struct Edge{
12     int u,v,w;
13     bool operator<(const Edge& chs)const{
14         return w<chs.w;
15     }
16 }e[maxm];
17 int N,M,f[maxn];
18 int getf(int u){return u==f[u]?u:f[u]=getf(f[u]);}
19 int main(){
20   scanf("%d%d",&N,&M);
21   int u,v,fu,fv,w;
22   for(int i=1;i<=N;++i)f[i]=i;
23   for(int i=0;i<M;++i){
24     scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
25   }sort(e,e+M);
26   for(int i=0;i<M;++i){
27     u=e[i].u,v=e[i].v,w=e[i].w;
28     fu=getf(u),fv=getf(v);
29     if(fu!=fv){
30         f[fu]=fv;
31     }
32     if(getf(1)==getf(N)){
33         printf("%d\n",w);
34         break;
35     }
36   }
37   return 0;
38 }

 

Guess you like

Origin www.cnblogs.com/zzqc/p/12512624.html