洛谷 1948 [USACO08JAN]电话线Telephone Lines

面对地震破坏后的城市,笨笨作为电话线布置师需重建电话网络。任务是在N个电话线杆间建立连接,利用免费提供的K条线路及额外付费线路,确保1号杆与N号杆相连。本篇介绍了一种通过二分答案+暴力检索+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 (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.
多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。
第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。
电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.
请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?
输入输出格式
输入格式:
输入文件的第一行包含三个数字n,p,k;
第二行到第p+1行,每行分别都为三个整数ai,bi,li。
输出格式:
一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.
输入输出样例
输入样例#1:
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
输出样例#1:
4

//二分答案+暴力检索+SPFA检验
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define MAXN 30007
int n,m,cnt,tot,ans,k;
int head[MAXN],dis[MAXN],is[MAXN];
bool exist[MAXN];
struct Edge{ int to,next,w; }e[MAXN];
inline void Add_Edge(int u,int v,int w){
    e[++tot].to=v;e[tot].w=w;
    e[tot].next=head[u];head[u]=tot;
}
queue<int> q;
void SPFA(){
    q.push(1);memset(exist,0,sizeof exist );
    memset(dis,127/3,sizeof dis );dis[1]=0;
    while(!q.empty()){
        int u=q.front();q.pop();exist[u]=false;
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to;
            if(dis[v]>dis[u]+is[i]){
                dis[v]=dis[u]+is[i];
                if(!exist[v]) q.push(v),exist[v]=true;
            }
        }
    }
}
bool Check(int x){
    for(int i=1;i<=n;i++)
        for(int j=head[i];j;j=e[j].next){
            if(e[j].w>x) is[j]=1;
            else is[j]=0;
        }
    SPFA();
    return dis[n]<=k;
}
void solve(){
    int l=0,r=1000007;
    while(l<r){
        int Mid=(l+r)>>1;
        if(Check(Mid)) r=Mid;
        else l=Mid+1;
    }
    if(r==1000007) printf("-1");
    else printf("%d\n",r);
}
inline int read(int &x){
    x=0;int f=1;char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int main(){
    read(n);read(m);read(k);
    for(int u,v,w,i=1;i<=m;i++){
        read(u);read(v);read(w);
        Add_Edge(u,v,w);Add_Edge(v,u,w);
    }
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值