Dijkstra+二分查找

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;
}
原文地址:https://www.cnblogs.com/foursmonth/p/14145127.html