zoukankan      html  css  js  c++  java
  • POJ 3662 Telephone Lines (二分+Dijkstra)

    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.

    Input

    • 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

    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

    题意:

    N条电线,可以免费连接K条线,问连接1到N的电线杆所需要的花费(花费为其中需要付费的最长的一根电线的长度)最小是多少?

    题解:

    问题本质就是找第K大的数,最短路径可以用dijkstra来寻找,然后对电线的长度进行二分,假设mid为免费电线的长度,大于mid的路径花费可以设为1,小于mid设为0,这样得到的d[N]的值就是需要免费的电线数。如果存在一个临界值x,使得长于x的电线数量恰好等于K,这个临界值对应的解就是最优解。

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int maxn=1005,INF=0x3f3f3f3f;
    int d[maxn];
    int n,p,k;
    struct edge
    {
        int to,cost;
        edge(){}
        edge(int to,int cost):to(to),cost(cost){}
    };
    typedef pair<int,int> P;
    vector<edge> G[maxn];
    void dijkstra(int s,int x)
    {
        priority_queue<P,vector<P>,greater<P> >que;
        fill(d+1,d+n+1,INF);
        d[s]=0;
        que.push(P(0,s));
        while(que.size())
        {
            P p=que.top();
            que.pop();
            int v=p.second;
            if(d[v]<p.first)
                continue;
            for(int i=0;i<G[v].size();i++)
            {
                edge e=G[v][i];
                int dis=d[v]+(e.cost>=x?1:0);
                if(d[e.to]>dis)
                {
                    d[e.to]=dis;
                    que.push(P(d[e.to],e.to));
                }
            }
        }
    }
    void solve()
    {
        int lb=0,ub=1000000+2;
        while(ub-lb>1)
        {
            int mid=(lb+ub)>>1;
            dijkstra(1,mid);
            if(d[n]>k)
            {
                lb=mid;
            }
            else
            {
                ub=mid;
            }
        }
        cout<<(lb>1000000?-1:lb)<<endl;
    }
    int main()
    {
        cin>>n>>p>>k;
        for(int i=0;i<p;i++)
        {
            int u,v,c;
            cin>>u>>v>>c;
            G[u].push_back(edge(v,c));
            G[v].push_back(edge(u,c));
        }
        solve();
    }
    
  • 相关阅读:
    Java中try-catch-finally的一点理解
    子类继承父类的私有属性
    Java中的String[] args
    Java类和类成员的访问权限修饰符
    JAVA中抽象类与接口的区别
    Java C# .net 和 C C++ 跨平台的区别
    Java中的instanceof关键字
    深入理解JAVA的多态性[转]
    Linux文件系统的目录结构
    硬盘分区
  • 原文地址:https://www.cnblogs.com/orion7/p/7814413.html
Copyright © 2011-2022 走看看