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..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 {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N 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: 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 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






 题意:就给给你一个图,让你求最小费用。要求给你一个数K,代表其中K条边是免费的,如果你需要超过k条边,所需要的费用是剩下的边中最长的长度作为费用
思路:

利用二分查找搜索最小费用,每次以这个费用为一个标准,也就是我把这个二分出来的费用就当做我要交的费用,即临界点,然后用dijkstra去找出从起点到终点,我至少需要多少条大于临界值的边,即dis数组记录的不再是变得长度,而是大于临界值的边数,然后返回结果,如果需要的边数大于免费的条数K,说明我这个临界值偏小,不能让所有大于它的边都免费,那么就把左边界变成mid,继续查找,反之亦然。

code:

/**
这道题运用dijkstra算法松弛不再是松弛边的长度,而是用二分去查找边的长度
然后用dijkstra去查找要到达终点所需要的大于这个长度的边的个数
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxV = 1001;
const int maxE = 20001;

int N,P,K;//分别代表点数,边数,免费条数
int num = 0;//给边编号
int dis[maxV];//这个dis数组并不是储存最短距离,而是储存到达终点时,大于某个特定长度(二分得到)的边的条数
int first[maxV];//邻接表头数组,储存对应顶点的第一条边
int vis[maxV];
struct Edge{
    int to;
    int val;
    int next;
}e[maxE];

void add(int from,int to,int val){
    e[num].to = to;
    e[num].val = val;
    e[num].next = first[from];
    first[from] = num++;
}

int spfa(int x){
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    dis[1] = 0;
    vis[1] = 1;
    q.push(1);
    while(!q.empty()){
        int now = q.front();
        vis[now] = 0;
        q.pop();
        int i;
        for(i = first[now]; i != -1; i = e[i].next){
            int t = dis[now]+(e[i].val >= x ? 1 : 0);//如果经过这条边得到的大于x的边数
            int v = e[i].to;
            if(dis[v] > t){//如果小于直接到达v所需的边数,松弛
                dis[v] = t;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return dis[N];//返回到达终点所需要的大于x的条数
}
int main(){
    cin >> N >> P >> K;
    memset(first,-1,sizeof(first));
    int i;
    for(i = 0; i < P; i++){
        int from,to,val;
        cin >> from >> to >> val;
        add(from,to,val);
        add(to,from,val);//用邻接表的方式储存
    }
    int L = 0,R = 1e6+2;
    while(R - L > 1){
        int mid = (L+R)>>1;
        if(spfa(mid) > K)//如果以这个长度去spfa得到,到达终点所需要的大于mid长度的边的个数大于K
            L = mid;//说明这个长度偏小,得提高这个长度,才能保证大于它的都免费,所以下界变成mid
        else
            R = mid;//反之
    }
    if(L <= 1e6) cout << L << endl;
    else cout << "-1" << endl;
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值