P1948 [Usaco08jan] telephone line telephone Lines

Source: Internet
Author: User
Tags printf
Main topic:

Years later, dumb-dumb grew up and became a telephone line planner. Because the earthquake caused a city's telephone line all damaged, stupid is responsible for receiving the epicenter of the city's head. The city is surrounded by N (1<=n<=1000) of obsolete telephone poles based on 1......N sequential numbers, with no telephone line connection between any two poles, altogether P (1<=p<=10000) to the telephone pole can pull the telephone line. Other due to earthquakes make it impossible to connect.

The two end points of the first pair of Poles are ai,bi, and their distances are Li (1<=li<=1000000). Each pair (AI,BI) in the data appears only once. The telephone pole numbered 1 has been connected to the national telephone network, and the entire city's telephone line is connected to the number n telephone pole. In other words, the stupid task is simply to find a way to connect the number 1th and n poles, the rest of the telephone poles do not have to be connected to the telephone network.

The telecom company decided to support the disaster area free of charge for this city connection K to the telephone poles designated by the stupid, for those other telephone lines, which need to pay for them, the total cost depends on the length of the longest telephone line (each phone line is connected to only a pair of telephone poles). If you need to connect a phone bar that does not exceed the K pair, the payout is 0.

Please calculate how much it will cost to direct the telephone line to the epicenter city at least on the telephone line. Ideas:

We can two, a value, higher than his need to use the telephone line, then run the shortest time, if the minimum road is greater than k then the value of the two points higher, less than k is legal. Program:

#include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <cstring
> #define N 1000000 using namespace std;
int last[n],cnt,ans,n,p,k,dis[n],x,y,z; struct Data{int w,to,next;}
E[n];
Queue <int> q;
    void Add (int x,int y,int W) {e[++cnt].to=y; e[cnt].w=w; e[cnt].next=last[x]; last[x]=cnt; E[++cnt].to=x; E[cnt].w=w; E[cnt].next=last[y];
last[y]=cnt;
    } bool Cheak (int x,int y) {if (x>y) return 1;
return 0;
    } bool SPFA (int x) {while (!q.empty ())) Q.pop ();
    Q.push (1);
    memset (dis,0x3f,sizeof (dis));
    dis[1]=0;
        while (!q.empty ()) {int U=q.front ();
        Q.pop (); for (int i=last[u];i;i=e[i].next) if (Dis[u]+cheak (e[i].w,x) <dis[e[i].to]) {Dis[e[i].to]=dis[u]+che
            AK (E[I].W,X);
        Q.push (e[i].to);
    }} if (dis[n]<=k) return 1;
return 0;
    } int main () {scanf ("%d%d%d", &n,&p,&k); for (int i=1;i<=p;i++) {scanf ("%d%d%d ", &x,&y,&z);
    Add (x, y, z);
    } int l=1,r=1000000;
    Ans=-1;
        while (l!=r) {int mid= (L+R) >>1;
                if (!SPFA (mid)) l=mid+1; 
    else R=mid,ans=mid;
            } if (ans==-1) printf ("-1"); 
else printf ("%d", ans); }

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.