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;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值