码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3662 Telephone Lines (分层图)

时间:2017-04-04 20:09:39      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:cab   att   nim   des   desc   plm   inpu   too   cep   

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 (分层图)

标签:cab   att   nim   des   desc   plm   inpu   too   cep   

原文地址:http://www.cnblogs.com/jianrenfang/p/6666177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!