POJ 3662 Telephone Lines (分层图)

Posted 贱人方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n",ans);
    return 0;
}

 

以上是关于POJ 3662 Telephone Lines (分层图)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3662 Telephone Lines (分层图)

POJ 3662 Telephone Lines (分层图做法)

poj 3662 Telephone Lines(最短路+二分)

POJ3662 Telephone Lines (dijkstra+二分)

poj3662 Telephone Lines

POJ3662 Telephone Lines( dijkstral + 二分 )