POJ 3662 Telephone Lines (二分+Dijkstra)

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

题意:

N条电线,可以免费连接K条线,问连接1到N的电线杆所需要的花费(花费为其中需要付费的最长的一根电线的长度)最小是多少?

题解:

问题本质就是找第K大的数,最短路径可以用dijkstra来寻找,然后对电线的长度进行二分,假设mid为免费电线的长度,大于mid的路径花费可以设为1,小于mid设为0,这样得到的d[N]的值就是需要免费的电线数。如果存在一个临界值x,使得长于x的电线数量恰好等于K,这个临界值对应的解就是最优解。

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=1005,INF=0x3f3f3f3f;
int d[maxn];
int n,p,k;
struct edge
{
    int to,cost;
    edge(){}
    edge(int to,int cost):to(to),cost(cost){}
};
typedef pair<int,int> P;
vector<edge> G[maxn];
void dijkstra(int s,int x)
{
    priority_queue<P,vector<P>,greater<P> >que;
    fill(d+1,d+n+1,INF);
    d[s]=0;
    que.push(P(0,s));
    while(que.size())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(d[v]<p.first)
            continue;
        for(int i=0;i<G[v].size();i++)
        {
            edge e=G[v][i];
            int dis=d[v]+(e.cost>=x?1:0);
            if(d[e.to]>dis)
            {
                d[e.to]=dis;
                que.push(P(d[e.to],e.to));
            }
        }
    }
}
void solve()
{
    int lb=0,ub=1000000+2;
    while(ub-lb>1)
    {
        int mid=(lb+ub)>>1;
        dijkstra(1,mid);
        if(d[n]>k)
        {
            lb=mid;
        }
        else
        {
            ub=mid;
        }
    }
    cout<<(lb>1000000?-1:lb)<<endl;
}
int main()
{
    cin>>n>>p>>k;
    for(int i=0;i<p;i++)
    {
        int u,v,c;
        cin>>u>>v>>c;
        G[u].push_back(edge(v,c));
        G[v].push_back(edge(u,c));
    }
    solve();
}

转载于:https://www.cnblogs.com/orion7/p/7814413.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值