Two minutes +spfa--poj3662

Source: Internet
Author: User
Language:default Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4397 Accepted: 1607

Description

Farmer John wants to set up a telephone line at the his farm. Unfortunately, the phone company was uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.

There is N (1≤n≤1,000) forlorn telephone poles conveniently numbered 1..N that is scattered around Farmer John ' s pro Perty; No cables connect any them. A total of P (1≤p≤10,000) pairs of poles can is connected by a cable; The rest is too far apart.

The i-th cable can connect the distinct poles Ai and Bi, with length Li (1≤li≤1,000,000) units if used. The input data set never names any {Ai, Bi} pair and more than once. Pole 1 is already connected to the phone system, and Pole N are at the farm. Poles 1 and N need to being connected by a path of cables; The rest of the Poles might is used or might not being used.

As it turns out, the phone company was willing to provide Farmer John with K (0≤k < N) lengths of cable for free. Beyond that he would have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

Input

* Line 1:three space-separated integers:n, P, and K
* Lines 2..p+1:line i+1 contains the three space-separated Integers:ai, Bi, and Li

Output

* Line 1: A single integer, the minimum amount Farmer John can pay. If It is impossible-connect the farm to the phone company, Print-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

Ideas: Two-point answer then the composition, smaller than mid-Benquan value of 0, otherwise, 1, and then seek the shortest path, if larger than K, then return false, otherwise Marines true.
Here's the code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std;
const int max=11000;
const int inf=100000; struct node {int to,len,next;}
Edge[max];
int pre[1010],dis[1010];
BOOL vis[1010];
int n,p,k;
int num;
int A[max],b[max],f[max];
    void Init () {memset (pre,-1,sizeof (pre));
num=0;
    } void Add_edge (int a,int b,int f) {edge[num].to=b;
    Edge[num].next=pre[a];
    Edge[num].len=f;
    pre[a]=num++;
    Edge[num].to=a;
    EDGE[NUM].NEXT=PRE[B];
    Edge[num].len=f;
pre[b]=num++;
    } void Build (int mid) {init ();
        for (int i=0;i<p;i++) {if (F[i]>mid) Add_edge (a[i],b[i],1);
    else Add_edge (a[i],b[i],0);
        }} bool Spfa () {for (int i=1;i<=n;i++) {vis[i]=false;
    Dis[i]=inf;
    } queue<int> Q;
    Q.push (1);
    dis[1]=0;
    Vis[1]=true;
        while (!q.empty ()) {int T=q.front ();
        Vis[t]=false; Q.pop ();
            for (int i=pre[t];i!=-1;i=edge[i].next) {int v=edge[i].to;
                if (Dis[v]>dis[t]+edge[i].len) {Dis[v]=dis[t]+edge[i].len;
                    if (!vis[v]) {Q.push (v);
                Vis[v]=true;
    }}}} if (dis[n]>k) return false;
return true;
    } int main () {//freopen ("In.txt", "R", stdin);
        while (cin>>n>>p>>k) {//init ();
        int max1=0;
            for (int i=0;i<p;i++) {scanf ("%d%d%d", &a[i],&b[i],&f[i]);
        if (F[I]&GT;MAX1) max1=f[i];
        } int l=0,r=max1,mid;
            while (r>l) {mid= (l+r)/2;
            Build (mid);
            BOOL Flag=spfa ();
                if (dis[n]==inf) {l=-1;
            Break
     } if (flag) R=mid;       else l=mid+1;
    } cout<<l<<endl;
} return 0;
 }


Related Article

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.