Telephone Lines POJ - 3662

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

There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..Nthat are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

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

As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ KN) lengths of cable for free. Beyond that he will 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, and Li

Output

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

题目意思,有N个点,P条边,每条边有权值。然后要把 1 和 n 连起来。不是所有点都要连,只要保证 能从 1 走到 n 就行了。

其中 k 条边是免费的,超出k条边,找最大的一个权值, 让最大的权值最小,输出答案。如果连不到n,那就输出-1,

最大的最小,就是二分了,

扫描二维码关注公众号,回复: 570395 查看本文章

二分加最短路,二分权值,跑最短路的时候,把权值大于枚举的权值变为1,小于的变为0,走到N,到n的最短路就是有多少大于枚举的权值的边数。

如果边数大于 K ,那就是枚举小了,

反之枚举大了。


#include <bits/stdc++.h>
using namespace std;
struct node{
    int next,v,w;
}edge[20009];
int sign,N,P,K;
int head[1009];
bool used[1009];
int dis[1009];
bool vis = 0;
void add_edge(int x,int y,int z){
    sign++;
    edge[sign].next = head[x];
    edge[sign].v = y;
    edge[sign].w = z;
    head[x] = sign;
}

bool judge(int k){
    queue<int>q;
    q.push(1);
    memset(dis,0x3f3f3f3f,sizeof(dis));
    memset(used,0,sizeof(used));
    dis[1] = 0;
    used[1] = 1;
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        used[u] = 0;
        for (int i = head[u]; i != 0; i = edge[i].next) {
            int w = 0;
            if (edge[i].w > k) w = 1;
            if (dis[edge[i].v] > dis[u] + w) {
                dis[edge[i].v] = dis[u] + w;
                if (!used[edge[i].v]) {
                    used[edge[i].v] = 1;
                    q.push(edge[i].v);
                }
            }

        }
    }
    if (dis[N] == 0x3f3f3f3f) vis = 1;
    if (dis[N] <= K) return 1; else return 0;
}
int main() {
    scanf("%d%d%d",&N,&P,&K);
    int x,y,z;
    for(int i = 0; i< P; i++){
        scanf("%d%d%d",&x,&y,&z);
        add_edge(x,y,z);
        add_edge(y,x,z);
    }
    int l = 0, r = 1000000,mid;
    while(l < r){
        mid = (l + r) / 2;
        if (judge(mid)) r  = mid; else l = mid+1;
        if (vis)
        {
            printf("-1\n");
            return 0;
        }
    }
    printf("%d\n",r);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/80279944