poj 3662 最短路
来源:互联网 发布:软件开发逻辑测试 编辑:程序博客网 时间:2024/05/16 06:13
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 11 2 53 1 42 4 83 2 35 2 93 4 74 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 0x3f3f3f3fstruct 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即可。
- poj 3662 最短路
- poj 3662(经典最短路)
- poj 3662 二分+最短路
- poj 3662 最短路+优先队列+二分法
- POJ - 3662 Telephone Lines (二分 + 最短路)
- poj 2253 最短路
- POJ 1135 最短路
- POJ 2267 最短路
- POJ 1797 最短路
- poj 1135最短路
- poj 1122最短路
- POJ 2240 最短路
- POJ 3268 最短路
- poj 1135 最短路
- poj 1502 最短路
- poj 1511 最短路
- poj 2139 最短路
- poj 2394 最短路
- NAND Flash产生坏块原因
- cas(一)
- 从久负盛名的GoDaddy开发革命来看Node.js的风靡程度
- MongoDB基本命令用
- 欢迎使用CSDN-markdown编辑器
- poj 3662 最短路
- Protocol Buffer使用
- 极光推送使用实例(三) Android客户端
- Android-Universal-Image-Loader
- 石子合并问题(大水题)
- 通过pid查看它载入dll,通过dll查询那些pid载入它
- 新华万银为2015投资财富起航
- 简易版硬盘装机
- 我参与的一个x86平台项目的经历