poj 3662 最短路

Telephone Lines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4998 Accepted: 1833

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



在1到n的路径中,可以选择k条边免去费用,最后费用为剩下的所有边中的费用最大值,也就是取决于免去部分费用后的最大那条边

最短路径解决,维护路径中最大值dp[i][j],到点i使用j次免费后剩下的最大费用。 假如u到v有一条边费用为w,如果不使用一次免费则,dp[v][j] = min(dp[v][j], max(dp[u][j], w)), 如果使用一次免费则 dp[v][j+1] = min(dp[v][j+1], dp[u][j])。然后使用bfs+队列优化,即spfa算法求解


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

#define maxn 1001
#define inf 0x3f3f3f3f

struct node
{
    int v, w, next;
    node() {}
    node(int v_, int w_, int n_) { v = v_; w = w_; next = n_;}
};

node g[20*maxn];
int top, first[maxn];

void add(int u, int v, int w)
{
    g[top] = node(v, w, first[u]);
    first[u] = top++;
}

int dp[maxn][maxn];
int n, p, k;
int in[maxn], cnt[maxn];

int spfa()
{
    memset(dp, inf, sizeof(dp));
    memset(in, 0, sizeof(in));
    memset(cnt, 0, sizeof(cnt));

    dp[1][0] = 0;
    queue<int> que;
    que.push(1); in[1] = 0;
    cnt[1]++;

    while(!que.empty()){
        int u = que.front(); que.pop();
        in[u] = 0;

        for(int i = first[u]; i != -1; i = g[i].next){
            int v = g[i].v, w = g[i].w;
            for(int j = 0; j <= k && dp[u][j] != inf; j++){
                if(max(dp[u][j],w) < dp[v][j]){
                    dp[v][j] = max(dp[u][j], w);
                    if(!in[v]){
                        que.push(v);
                        in[v] = 1;
                        cnt[v]++;
                        if(cnt[v]>=n) return 0;
                    }
                }

                if(j <= k-1 && dp[u][j] < dp[v][j+1]){
                    dp[v][j+1] = dp[u][j];
                    if(!in[v]){
                        que.push(v);
                        in[v] = 1;
                        cnt[v]++;
                        if(cnt[v]>=n) return 0;
                    }
                }
            }
        }
    }

    return 1;
}


int main()
{
   while(~scanf("%d%d%d", &n, &p, &k)){
        top = 0;
        memset(first, -1, sizeof(first));

        int u,v,w;
        for(int i = 0; i < p; i++){
            scanf("%d%d%d", &u, &v, &w);
            add(u,v,w);
            add(v,u,w);
        }

        spfa();
        int ans = inf;
        for(int i = 0; i <= k; i++)
            ans = min(dp[n][i], ans);
        if(ans!=inf)
        printf("%d\n", ans);
        else
        printf("%d\n", -1);
   }
    return 0;
}

还有一种方法是,二分枚举答案,即剩下的边中最大值。假如最大值为max,那么边长小于等于max都可以随便走,大于max的边长要用一次免费。所以把大于max的边费用当作1,小于的费用为0,最后计算到点n的最小值是否<=k即可。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值