zoukankan      html  css  js  c++  java
  • poj3662

    http://poj.org/problem?id=3662

    Telephone Lines
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10711   Accepted: 3828

    Description

    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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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.

    Input

    * Line 1: Three space-separated integers: NP, and K
    * Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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
    

    Source

     
    给定n个点m条边,从1走到n的花费为长度最长的边。
    可以将k条边的长度改为0,求1到n的最小花费
     
    解法1
    分层图,建k*n个点,表示已经修改了j条边在第i个点
    然后跑最短路即可。
    #include<cstdio>
    #include<queue>
    #include<cstring> 
    using namespace std;
    const int N=1e3+5,inf=2147483647;
    struct E{
        int v,n,q;
    }e[N*20];
    int fir[N],s,dis[N][N];
    bool vis[N][N];
    struct F{
        int v,c,q;
        bool operator<(const F &a)const{
            return q>a.q;
        } 
    }st;
    priority_queue<F>dl;
    void add(int u,int v,int q){
        e[++s].v=v;
        e[s].q=q;
        e[s].n=fir[u];
        fir[u]=s;
    }
    void init(int m){
        int u,v,q;
        while(m--){
            scanf("%d%d%d",&u,&v,&q);
            add(u,v,q);
            add(v,u,q);
        }
    }
    void dij(int n,int k){
        dl.push(st);
        for(int i=1;i<=n;++i) 
            for(int j=0;j<=k;++j)
                dis[i][j]=inf,vis[i][j]=0;
        dis[st.v][0]=0;
        while(!dl.empty()){
            F t=dl.top();dl.pop();
            if(vis[t.v][t.c]) continue;
            vis[t.v][t.c]=1;
            for(int i=fir[t.v];i;i=e[i].n){
                if(max(t.q,e[i].q)<dis[e[i].v][t.c]){
                    dis[e[i].v][t.c]=max(e[i].q,t.q);
                    if(!vis[e[i].v][t.c]) dl.push((F){e[i].v,t.c,dis[e[i].v][t.c]});
                }
                if(t.q<dis[e[i].v][t.c+1]){
                    dis[e[i].v][t.c+1]=t.q;
                    if(!vis[e[i].v][t.c+1]) dl.push((F){e[i].v,t.c+1,dis[e[i].v][t.c+1]});
                }
            }
                
        }
    }
    int main(){
        freopen("a.in","r",stdin);
        int n,p,k,ans=inf;
        scanf("%d%d%d",&n,&p,&k);
        st.v=1;init(p);
        dij(n,k);
        for(int i=0;i<=k;++i) ans=min(ans,dis[n][k]);
        if(ans==inf) ans=-1;
        printf("%d",ans);
        return 0;
    } 

    解法2:

    二分答案,检查k时

    我们将图上的边,若边权<=k,则边权为0;否则边权为1.

    然后只要用双端队列bfs即可

    对于边权为0的加在队头,边权为加在队尾。

    #include<cstdio>
    #include<cstring>
    #include<deque>
    #include<algorithm>
    using namespace std;
    const int N=1e3+5;
    struct E{
        int v,q,n;
    }e[N*20];
    int s,n,fir[N],cs[N];
    bool vis[N];
    deque< pair<int,int> >dl;
    void add(int u,int v,int q){
        e[++s].v=v;
        e[s].q=q;
        e[s].n=fir[u];
        fir[u]=s;
    }
    int f(int z){
        dl.push_back(make_pair(1,1));
        memset(cs,0,sizeof(cs));
        while(!dl.empty()){
            int u=dl.front().first,c=dl.front().second;
            dl.pop_front();
            if(cs[u]) continue;
            cs[u]=c;
            for(int i=fir[u];i;i=e[i].n){
                int v=e[i].v;
                if(cs[v]) continue;
                if(e[i].q<=z) dl.push_front(make_pair(v,c));
                else dl.push_back(make_pair(v,c+1));
            }
        }
        if(!cs[n]) return 1e8;
        return cs[n]-1;
    }
    int main(){
        freopen("a.in","r",stdin);
        int p,k,u,v,q,l=-1,r=0;
        scanf("%d%d%d",&n,&p,&k);
        for(int i=1;i<=p;++i){
            scanf("%d%d%d",&u,&v,&q);
            r=max(q,r);
            add(u,v,q),add(v,u,q);
        }
        r=r+1;
        while(r-1>l){
            int mid=(l+r)>>1;
            if(f(mid)>k) l=mid;
            else r=mid;
        }
        if(f(r)>k) r=-1;
        printf("%d",r);
        return 0;
    } 
  • 相关阅读:
    linux加载和卸载模块
    java 面试题之银行业务系统
    java 面试题之交通灯管理系统
    java 实现类似spring的可配置的AOP框架
    分析JVM动态生成的类
    最长上升子序列(模板)
    项目管理模式
    让thinkphp 支持ftp上传到远程,ftp删除
    hdu 1280 前m大的数 哈希
    互联网+脑科学,中国脑计划的机会
  • 原文地址:https://www.cnblogs.com/bzmd/p/11452383.html
Copyright © 2011-2022 走看看