TYVJ2477 Erection of telephone lines

Source: Internet
Author: User

Title Description

Farmer John intends to lead the telephone line to his farm, but the telecoms company is not going to provide him with free services. Thus, FJ must pay a certain fee to the telecommunications company for this purpose.

FJ's farm is surrounded by n (1 <= n <= 1,000) root by 1. n Serial number of discarded telephone poles, any two telephone poles are not connected to the telephone line.    Altogether P (1 <= p <= 10,000) can pull the telephone line between the telephone poles, the rest of those are too far apart to be connected. The two endpoints of the telephone pole are a_i, b_i respectively, and the distance between them is l_i (1 <= l_i <= 1,000,000). The data guarantees that each pair {A_i,b_i} appears at most 1 times. The telephone pole numbered 1 has been connected to the national telephone network, and the entire farm's telephone line has been connected to the telephone pole numbered n.    In other words, FJ's task is simply to find a path that connects the 1th and N telephone poles, and the rest of the phone poles do not necessarily connect to the telephone network. After negotiations, the telecommunications company eventually agreed to free the FJ Nexus K (0 <= K < N) to the telephone pole specified by FJ. For those phone lines in addition, the FJ need to pay for them, equal to the length of the longest telephone line (each phone line is only connected to a pair of telephone poles).    If the number of telephone poles that need to be connected does not exceed the K pair, then the total expenditure of FJ is 0. Please calculate how much the FJ need to spend on the telephone line at least. Enter * Line 1th: 3 integers separated by a space: N,p, and k* 2nd. P+1 Line: section i+1 behavior 3 integers separated by spaces: a_i,b_i,l_i Output * Line 1th: Output 1 integers for FJ on this project minimum expenditure. If the task cannot be completed, output-1 sample Input 5 7 1 1 2 5 3 1 4 2 4 8 3 2 3 5 2 9 3 4 7 4 5 6 sample output 4 hints

Enter a description :A total of 5 discarded telephone poles. The telephone pole 1 cannot directly connect with the telephone pole 4, 5. The telephone pole 5 cannot directly connect with the telephone pole 1, 3. All the rest of the telephone poles can pull the telephone line. Telecom companies can link a pair of telephone poles for FJ free of charge.

Output Description :

FJ Select the following link scheme: 1->3;3->2;2->5, the length of the telephone line between the 3 calls to the telephone line is 4, 3, 9 respectively. FJ gave the telecoms company the 9-length phone line, and the maximum length of the phone line he needed to buy was 4.must pay attention to the problem, the problem of this free use of the key, two-point answer, not more than the set of Benquan 0, the other 1, run the shortest circuit is actually looking for the least number of lines to spend, so more than the preset free, you can ensure that the test meets the requirements
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<queue>using namespacestd;Const intMAXN =20005, Maxint =0x3fffffff;structedge{intv; intW;};intn,p,k,dis[maxn],mx;BOOLInq[maxn];vector<edge>G[MAXN];voidinput () {CIN>>n>>p>>K; intu,v,w;    Edge tmp; MX=0;  for(inti =1; I <= p;i++) {scanf ("%d%d%d",&u,&v,&W); TMP.V=v; TMP.W=W;        G[u].push_back (TMP); TMP.V=u;        G[v].push_back (TMP); MX=Max (MX,W); }}voidSPFA (intt) {memset (INQ,false,sizeof(INQ));  for(inti =1; I <= n;i++) dis[i] =Maxint; Queue<int>Q; inq[1] =true; dis[1] =0; Q.push (1);  while(!Q.empty ()) {        intU =Q.front (), v,w; Inq[u]=false;        Q.pop ();  for(inti =0; i < g[u].size (); i++) {v=g[u][i].v; W= G[U][I].W > t?1:0; if(Dis[u] + W <Dis[v]) {Dis[v]= Dis[u] +W; if(!Inq[v]) {Inq[v]=true;                Q.push (v); }            }        }    }}BOOLCheckintt)    {SPFA (t); if(Dis[n] <=k) {        return true; }    Else return false;}voidDiv () {intLANs =0, rans =Mx,mans;  while(LANs <=rans) {Mans= (LANs + rans) >>1; if(check (mans)) {rans= Mans-1; }Else{LANs= Mans +1; }    }    if(check (mans)) {if(Mans > MX) cout<<-1; Elsecout<<Mans; }Else{        if(Mans +1> mx) cout<<-1; ElseCout<<mans +1; }}intMain () {input ();    Div (); return 0;}
View Code

TYVJ2477 Erection of telephone lines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.