POJ3662 Telephone Lines【二分+SPFA】

Time limit 1000 ms
Memory limit 65536 kB

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.

题目分析:

大意就是求一条1到n得路径,使路径上第k+1大得边权尽量小

可以二分改造路径的大小范围
大于mid的就改造,不大于就不改变

所以原图中边权大于mid的边看作边权为1
不大于的看为0
跑最短路

若1到n最短路大于k
就令L=mid+1
否则R=mid


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int inf=1128481603;
const int maxn=2010;
int n,m,k;
struct node{int v,dis,nxt;}E[50010];
int head[maxn],tot;
int d[maxn],vis[maxn];
int L=0,R=1e6+10,mid;
int ans;

void add(int u,int v,int dis)
{
    E[++tot].nxt=head[u];
    E[tot].v=v; E[tot].dis=dis;
    head[u]=tot;
} 

void SPFA()
{
    memset(d,67,sizeof(d)); d[1]=0;
    queue<int> q; q.push(1);
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    {
        int u=q.front(); 
        q.pop(); vis[u]=0;
        for(int i=head[u];i;i=E[i].nxt)
        {
            int v=E[i].v,dis=(E[i].dis>mid);
            if(d[v]>d[u]+dis)
            {
                d[v]=d[u]+dis;
                if(!vis[v])q.push(v),vis[v]=1;
            } 
        }
    }
}

int main()
{
    n=read();m=read();k=read();
    for(int i=1;i<=m;++i)
    {
        int u=read(),v=read(),dis=read();
        add(u,v,dis);add(v,u,dis);
    }
    while(L<R)
    {
        mid=L+R>>1;
        SPFA();
        if(d[n]==inf){ printf("-1"); return 0;}
        if(d[n]<=k) R=mid;
        else L=mid+1;
    } 
    printf("%d",R);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值