Telephone Lines POJ - 3662(spfa+二分)

本文探讨了如何通过选择合适的电缆连接方式,在限定条件下将农场与电话系统连接起来的问题。主要内容包括:理解问题背景,设计并实现算法解决该问题,采用SPFA变种算法进行最短路径计算,通过调整电缆长度的权重来确定最小花费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(1N1,000)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 ofP(1P10,000)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(1Li1,000,000)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(0K<N)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
评价:好题,spfa的变式原因,因为答案存在单调行(假设答案存在),而且必然是从已知边里选择,关键怎么判断边的可行,只要把边大于mid的地阿亮看作长度为1,反正变为0,然后做spfa,如果num[n]的值小于等于k,则合法。
代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#define maxx 1005
#define maxn 10005
#define INF 0x3f3f3f3f
using namespace std;
int n,m,k;
int head[maxx],_next[maxn<<1],to[maxn<<1],w[maxn<<1];
int num[maxx];
char inQ[maxx];
int cnt;
void addEdge(int x,int y,int r)
{
    to[++cnt]=y,w[cnt]=r,_next[cnt]=head[x],head[x]=cnt;
    to[++cnt]=x,w[cnt]=r,_next[cnt]=head[y],head[y]=cnt;
}
int b[maxn];
bool spfa(int x)
{
    queue<int> que;
    memset(num,INF,sizeof(num));
    num[1]=0;
    que.push(1);
    inQ[1]=true;
    while(que.size())
    {
        int now=que.front();
        que.pop();
        inQ[now]=false;
        for(int i=head[now];i;i=_next[i])
        {
            int L=w[i]>x?1:0;
            int v=to[i];
            if(num[v]>num[now]+L)
            {
                num[v]=num[now]+L;
                if(!inQ[v])
                {
                    que.push(v);
                    inQ[v]=true;
                }
            }
        }
    }
    return num[n]<=k;
}
int main()
{
    cin>>n>>m>>k;
    int x,y,w;
    cnt=0;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d%d",&x,&y,&w);
        addEdge(x,y,w);
        b[i]=w;
    }
    if(spfa(0))
        cout<<0<<endl;
    else
    {
        sort(b,b+m);
        //cout<<"haha"<<spfa(4)<<endl;
        int l=0,r=m-1;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(spfa(b[mid]))
                r=mid-1;
            else
                l=mid+1;
        }
        if(l==m)
            cout<<-1<<endl;
        else
            cout<<b[l]<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值