Dijkstra+二分查找

使用Dijkstra和二分查找解决电话线路连接问题
农夫约翰想要在他的农场设置电话线,但需要自己连接部分电缆。给定N个电话杆和P对可连接的杆,每对杆之间的长度为Li。电话公司提供K条免费电缆,其余需要付费。问题是要找到最小的额外费用以连接杆1到杆N。通过Dijkstra算法和二分查找,确定最小费用。如果无法连接,则输出-1。
Telephone Lines
题目描述

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.

输入描述:
  • 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
输出描述:
  • 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.
输入
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
输出
4
说明

There are 5 poles. Pole 1 cannot be connected directly to poles 4 or 5. Pole 5 cannot be connected directly to poles 1 or 3. All other pairs can be connected. The phone company will provide one free cable.
If pole 1 is connected to pole 3, pole 3 to pole 2, and pole 2 to pole 5 then Farmer John requires cables of length 4, 3, and 9. The phone company will provide the cable of length 9, so the longest cable needed has length 4.

大意,给定一个图,求一条路径,可以删除k条边,使得剩下的最长的一条边的长度最小。
看到两个最字,应该就是二分了。
删除的话当然是删路径上最大的k条边。
假设答案是mid,大于mid的权值为1,否则为0,这样,求一下最短路就知道了当前答案下最少要删多少条边。

#include<bits/stdc++.h>
using namespace std;
#define Init(arr,val) memset(arr,val,sizeof(arr))
const int inf=0x3f3f3f3f,mod=1e9+7,MAXN=1e3+8,MAXM=2e4+8;;
typedef long long ll;
struct Edge{
    int to,val,nxt,w;
}e[MAXM];
int head[MAXN],cnt;
inline void add(int x,int y,int val){
    e[++cnt].to=y;
    e[cnt].val=val;
    e[cnt].nxt=head[x];
    head[x]=cnt;
}
int n,p,k;
struct Node{
    int y,w;
    bool operator<(const Node&other)const{return w>other.w;}
};
bool vis[MAXN];
int dis[MAXN];
void dijkstra(){
    Init(vis,0);
    Init(dis,inf);
    priority_queue<Node>heap;
    dis[1]=0;
    heap.push({1,0});
    while(!heap.empty()){
        Node tmp=heap.top();
        heap.pop();
        int x=tmp.y;
        if(vis[x])continue;
        vis[x]=1;
        for(int i=head[x];i;i=e[i].nxt){
            int y=e[i].to;
            if(!vis[y]&&dis[y]>dis[x]+e[i].w){
                dis[y]=dis[x]+e[i].w;
                heap.push({y,dis[y]});
            }
        }
    }
}
void change(int ans){//以ans为界限改变 边权。
    for(int i=1;i<=cnt;++i){
        if(e[i].val>ans)e[i].w=1;
        else e[i].w=0;
    }
}
int main() {
    std::ios::sync_with_stdio(0);std::cin.tie(0);std::cout.tie(0);
    cin>>n>>p>>k;
    int x,y,v,l=0,r=-1;
    for(int i=0;i<p;++i){
        cin>>x>>y>>v;
        add(x,y,v);
        add(y,x,v);
        if(r<v)r=v;
    }
    while(l<r){
        int mid=(r+l)>>1;
        change(mid);
        dijkstra();
        if(dis[n]==inf){cout<<-1;return 0;}//无答案。
        if(dis[n]>k)l=mid+1;//说明mid太小了,加大。
        else r=mid;//mid符合条件,看看有没有更小的
    }
    cout<<r;
    return 0;
}
标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值