• POJ 3662 Telephone Lines (分层图)


    Telephone Lines
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6785   Accepted: 2498

    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 {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
    【分析】题意有点绕口我这里就不说了,防止说错,说说做法吧。学长们都是二分+dij过得,我是用的分层图。分层图专门用来解决这种免费问题的,然后d[x][k]表示到x节点用了k次免费
    的最优解,然后跑spfa。
    #include <cstdio>
    #include <map>
    #include <algorithm>
    #include <vector>
    #include <iostream>
    #include <set>
    #include <queue>
    #include <string>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    using namespace std;
    typedef pair<int,int>pii;
    typedef long long LL;
    const int N=2e3+5;
    const int mod=1e9+7;
    int n,m,s,k,t,cnt,idl[N<<1],idr[N<<1];
    bool vis[N][1005];
    int d[N][1005];
    vector<pii>edg[N];
    struct man{
        int v;
        int c;
        int w;
        bool operator<(const man &e)const{
            return w>e.w;
        }
    };
    priority_queue<man>q;
    void spfa(int s){
        memset(d,-1,sizeof d);memset(vis,0,sizeof vis);
        d[s][0]=0;
        q.push(man{s,0,0});
        while(!q.empty()){
            int u=q.top().v,c=q.top().c;q.pop();
            if(vis[u][c])continue;
            vis[u][c]=1;
            for(int i=0;i<edg[u].size();++i){
                int v=edg[u][i].first,w=edg[u][i].second;
                if(!vis[v][c]&&(d[v][c]==-1||d[v][c]>max(d[u][c],w))){
                    d[v][c]=max(d[u][c],w);
                    q.push(man{v,c,d[v][c]});
                }
                if(c<k){
                    if(!vis[v][c+1]&&(d[v][c+1]==-1||d[v][c+1]>d[u][c])){
                        d[v][c+1]=d[u][c];
                        q.push(man{v,c+1,d[v][c+1]});
                    }
                }
            }
        }
    }
    int main()
    {
        int x,y,w;
        scanf("%d%d%d",&n,&m,&k);
        s=1;t=n;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&w);
            edg[x].push_back(make_pair(y,w));
            edg[y].push_back(make_pair(x,w));
        }
        spfa(s);
        int ans=10000000;
        for(int i=0;i<=k;i++)ans=min(ans,d[t][i]);
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    【255】◀▶IEW-Unit20
    【254】◀▶IEW-Unit19
    【253】◀▶IEW-Unit18
    【252】◀▶IEW-Unit17
    [LeetCode] Restore IP Address
    1030
    (Relax 数论1.8)POJ 1284 Primitive Roots(欧拉函数的应用: 以n为模的本原根的个数phi(n-1))
    新知识的遗忘数度真是可怕
    【自由谈】城域网IPv6过渡技术——4v6场景技术总结(1)
    Graph Databases—The NOSQL Phenomenon阅读笔记
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6666177.html
Copyright © 2020-2023  润新知