POJ 3662 Telephone Lines (SPFA、二分搜索)

Telephone Lines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5204   Accepted: 1906

Description

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..N that 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 ≤ K < N) 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

二分答案 大于等于它的记为1 这样就可以用spfa算出所用的电线数 二分终止的时候是 k + 1 然后剩余了一个 k个免费还剩它一个收费 答案就是l

AC代码如下:

//
//  Created by TaoSama on 2015-04-29
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, p, k, d[1005];

int pnt[20005], nxt[20005], dis[20005], head[20005], cnt;
bool in[1005];

void add_edge(int u, int v, int d) {
    pnt[cnt] = v;
    dis[cnt] = d;
    nxt[cnt] = head[u];
    head[u] = cnt ++;
}

int spfa(int x) {
    queue<int> q; q.push(1);
    memset(d, 0x3f, sizeof d);
    memset(in, false, sizeof in);
    in[1] = true; d[1] = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        in[u] = false;
        for(int i = head[u]; ~i; i = nxt[i]) {
            int &v = pnt[i];
            int t = d[u] + (dis[i] >= x ? 1 : 0);
            if(d[v] > t) {
                d[v] = t;
                if(!in[v]) {
                    q.push(v);
                    in[v] = true;
                }
            }
        }
    }
    return d[n];
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    scanf("%d%d%d", &n, &p, &k);
    cnt = 0;
    memset(head, -1, sizeof head);
    for(int i = 1; i <= p; ++i) {
        int u, v, d; scanf("%d%d%d", &u, &v, &d);
        add_edge(u, v, d);
        add_edge(v, u, d);
    }

    int l = 0, r = 1e6 + 2;
    while(l + 1 < r) {
        int mid = l + r >> 1;
        if(spfa(mid) > k) l = mid;
        else r = mid;
    }

    int ans = l > 1e6 ? - 1 : l;
    printf("%d\n", ans);
    return 0;
}


你可能感兴趣的:(POJ 3662 Telephone Lines (SPFA、二分搜索))