POJ3662 Telephone Lines(二维最短路)

最优电缆铺设策略
本文探讨了一个涉及图论和算法优化的问题,即如何在有限的免费电缆资源下,找到连接农场到电话系统的最经济路径,最小化最长电缆的成本。通过采用二维最短路径算法,文章详细解释了如何结合二分搜索和Dijkstra算法来解决这一挑战。

传送门
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

题意
给你一个带权无向图,最小化点1到点n的路径中经过边的最大值,其中你可以免费k条边,也就是你可以选择k条边将权值变为0;

思路
网上大部分的做法是二分加最短路,这里提供一个二维最短路解法:

先不考虑免费

d[v]=min(max(d[u],e))//v和u有权值为e的边,可以用dijkstra;

考虑免费,dp[i][v]表示免费i条边,到v点的最小代价

dp[i][v]=min(dp[i-1][u],max(dp[i][u],e))//v和u有权值为e的边;

将每个点v的初始值置为min(dp[i-1][u]),跑dijkstra即可;
时间复杂度为O(n^2logn);

参考代码

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

const int INF=0x3f3f3f3f;
const int MAX_N=1000+5;

struct edge{
    int to,cost;
    edge(int t,int c):to(t),cost(c){}
};

int n,p,k;
vector<edge> G[MAX_N];
int dp[MAX_N][MAX_N];

void dijkstra(int *d){
    priority_queue<P,vector<P>,greater<P> >que;
    for(int i=0;i<n;i++)if(d[i]!=INF)que.push(P(d[i],i));
    while(!que.empty()){
        P p=que.top();que.pop();
        int v=p.second;
        if(p.first>d[v])continue;
        for(int i=0;i<G[v].size();i++){
            edge &e=G[v][i];
            if(d[e.to]>max(d[v],e.cost)){
                d[e.to]=max(d[v],e.cost);
                que.push(P(d[e.to],e.to));
            }
        }
    }
}

int main(){
    scanf("%d%d%d",&n,&p,&k);
    for(int i=0;i<p;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);u--;v--;
        G[u].push_back(edge(v,w));
        G[v].push_back(edge(u,w));
    }
    fill(dp[0],dp[0]+n,INF);
    dp[0][0]=0;
    dijkstra(dp[0]);

    for(int i=1;i<=min(p,k);i++){
        for(int v=0;v<n;v++){
            dp[i][v]=dp[i-1][v];
            for(int j=0;j<G[v].size();j++){
                int u=G[v][j].to;
                dp[i][v]=min(dp[i][v],dp[i-1][u]);
            }

        }
        dijkstra(dp[i]);
    }
    if(dp[min(p,k)][n-1]!=INF)printf("%d\n",dp[min(p,k)][n-1]);
    else printf("-1\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值