洛谷P1948 [USACO08JAN]电话线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.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着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

题解

这题简化之后就是:在两点之间,求一条路径,使得路径上第k+1大的边最小。(不过要先特判-1和0的情况)

思路就是:二分答案(将边按权值排序后再二分),每一次都来一遍SPFA(貌似Dijstra也可以吧)

注意:SPFA的时候把比当前二分的数据大的边都标成1,其他标成0,然后求出从1到n的最短路,和k比较,如果大于等于k,就更新最终答案ans,二分循环条件是l<=r。

自己想一想为什么这么做是对的

时间复杂度:快排nlogn,SPFA是ke,二分是logn,因为是二分+SPFA,所以是kelogn(应该可以吧)。

空间复杂度:邻接表e,距离n,所以是n+e,没问题。

最重要的是:一定要注意这题是双向边!!!(我被坑了无数次,一直认为出了其他错误,结果。。。)
下来贴代码:


#include<bits/stdc++.h>
#define inf 2000000000
using namespace std;
struct edge{
    int to,next,w;
};
int n,p,k,mx;
edge e[20005];
int d[1001];
int head[1001];
int bian[20005];
void spfa(int cmp){
    bool in[1001];
    for(int i=1;i<=n;i++){
        d[i]=inf;
        in[i]=0;
    }
    queue<int> q;
    in[1]=1;
    q.push(1);
    d[1]=0;
    while(!q.empty()){
        int t=q.front();
        q.pop();
        in[t]=0;
        for(int i=head[t];i!=0;i=e[i].next){
            if(d[e[i].to]>d[t]+(e[i].w>cmp)){
                d[e[i].to]=d[t]+(e[i].w>cmp);
                if(!in[e[i].to]){
                    in[e[i].to]=1;
                    q.push(e[i].to);
                }
            }
        }
    }
}
void check(){
    spfa(-1);
    if(d[n]==inf){
        cout<<-1;
        return;
    }
    else if(d[n]<=k){
        cout<<0;
        return;
    }
    sort(bian+1,bian+2*p+1);
    int l=1,r=p*2;
    int mid=(l+r)/2;
    int ans=-1;
    while(l<r){
        mid=(l+r)/2;
        spfa(bian[mid]);
        if(d[n]<=k){
            ans=bian[mid];
            r=mid;
        }
        else{
            l=mid+1;
        }
    }
    cout<<ans;
}
int main(){
    mx=0;
    scanf("%d %d %d",&n,&p,&k);
    for(int i=1;i<=2*p;i++){
        int x,y,l;
        scanf("%d %d %d",&x,&y,&l);
        e[i].to=y;
        e[i].w=l;
        e[i].next=head[x];
        head[x]=i;
        i++;
        e[i].to=x;
        e[i].w=l;
        e[i].next=head[y];
        head[y]=i;
        bian[i]=l;
    }
    check();
    return 0;
}
posted @ 2017-07-01 20:39  玫葵之蝶  阅读(165)  评论(0编辑  收藏  举报