SOJ3423: Revamping Trails--(最短路 + 动态规划)
来源:互联网 发布:linux编辑hosts 编辑:程序博客网 时间:2024/06/05 17:05
Description
Farmer John dutifully checks on the cows every day. He traversessome of the M (1 <= M <= 50,000) trails conveniently numbered 1..Mfrom pasture 1 all the way out to pasture N (a journey which isalways possible for trail maps given in the test data). The N (1<= N <= 10,000) pastures conveniently numbered 1..N on Farmer John'sfarm are currently connected by bidirectional dirt trails. Eachtrail i connects pastures P1_i and P2_i (1 <= P1_i <= N; 1 <= P2_i<= N) and requires T_i (1 <= T_i <= 1,000,000) units of time totraverse.He wants to revamp some of the trails on his farm to save time onhis long journey. Specifically, he will choose K (1 <= K <= 20)trails to turn into highways, which will effectively reduce thetrail's traversal time to 0. Help FJ decide which trails to revampto minimize the resulting time of getting from pasture 1 to N.Input
* Line 1: Three space-separated integers: N, M, and K* Lines 2..M+1: Line i+1 describes trail i with three space-separated integers: P1_i, P2_i, and T_iOutput
* Line 1: The length of the shortest path after revamping no more than K edgesSample Input
4 4 11 2 102 4 101 3 13 4 100Sample Output
1注:这道题刚开始我用分层图的思想做,分为(k + 1)层,但是内存太大了,总是RE,没办法,只能用dijkstra+优先队列+动态规划做了,且状态转移是从底层图到高层图。source code:#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef long long ll;const int inf = 0x3fffffff;const int maxn = 10005;const int maxm = 100050;int head[maxn], next[maxm], vis[maxn];ll dis[maxn][25];int e, n, m, k;struct node{ int u, v; int c; node(){} node(int u, int v, int c):u(u), v(v), c(c){}}p[maxm];void addnode(int u, int v, int c){ p[e] = node(u, v, c); next[e] = head[u]; head[u] = e++;}struct ab{ int v, c; ab(){} ab(int v, int c) : v(v), c(c){} bool operator < (const ab &other) const { return c > other.c; }};bool relax(int u, int v, int c){ if (dis[v][0] > dis[u][0] + c) { dis[v][0] = dis[u][0] + c; return true; } return false;}void init(){ e = 0; memset(head, -1, sizeof(head)); memset(next, -1, sizeof(next));}void dij(int s, int t){ for (int i = 1; i <= n; i++) dis[i][0] = inf; memset(vis, 0, sizeof(vis)); priority_queue<ab> que; while (!que.empty()) que.pop(); dis[s][0] = 0; que.push(ab(s, dis[s][0])); while (!que.empty()) { ab u = que.top(); que.pop(); if (vis[u.v]) continue; vis[u.v] = 1; for (int i = head[u.v]; i + 1; i = next[i]) { if (relax(u.v, p[i].v, p[i].c) && !vis[p[i].v]) { que.push(ab(p[i].v, dis[p[i].v][0])); } } }}void dij2(int s, int t, int l){ for (int i = 1; i <= n; i++) dis[i][l] = inf; dis[s][l] = 0; memset(vis, 0, sizeof(vis)); priority_queue<ab> que; while (!que.empty()) que.pop(); que.push(ab(s, dis[s][l])); while (!que.empty()) { ab u = que.top(); que.pop(); if (vis[u.v]) continue; vis[u.v] = 1; for (int i = head[u.v]; i + 1; i = next[i]) { if (dis[p[i].v][l] > min(dis[u.v][l] + p[i].c, dis[u.v][l - 1]) && !vis[p[i].v]) { dis[p[i].v][l] = min(dis[u.v][l] + p[i].c, dis[u.v][l - 1]); que.push(ab(p[i].v, dis[p[i].v][l])); } } }}int main(){ freopen("F:\\test.txt", "r", stdin); int u, v, w; while (scanf("%d%d%d", &n, &m, &k) != EOF) { init(); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); addnode(u, v, w); addnode(v, u, w); } dij(1, n); for (int i = 1; i <= k; i++) { dij2(1, n, i); } printf("%lld\n", dis[n][k]); } return 0;}
0 0
- SOJ3423: Revamping Trails--(最短路 + 动态规划)
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路
- bzoj1579 [Usaco2009 Feb]Revamping Trails 道路升级(分层图最短路)
- BZOJ 1579 Revamping Trails
- TOJ 2609 Revamping Trails -- dp
- HNU 13375 Flowery Trails (最短路)
- 【最短路】【动态规划】数字矩阵 rectangle
- 【jzoj3891】【钻石交易】【动态规划】【最短路】
- [Usaco2009 Feb]Revamping Trails 堆优化 Dijkstra
- USACO 2009 Feb Revamping Trails 道路翻新
- USACO 2009 Feb Gold 3.Revamping Trails
- BZOJ1597 [Usaco2009 Feb]Revamping Trails 道路升级
- 【USACO】2009 Feb Revamping Trails 道路翻新
- BZOJ1579: [Usaco2009 Feb]Revamping Trails 道路升级
- BZOJ1579: [Usaco2009 Feb]Revamping Trails 道路升级
- 【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级
- poj2404 Jogging Trails 欧拉回路、最短路、搜索
- UVa 12878 - Flowery Trails(最短路应用)
- BF匹配算法
- LaTeX插入图片位置报错
- phpstorm8连接xdebug来调试php
- java.sql.SQLException: The Network Adapter could not establish the connection
- markdown pad2破解
- SOJ3423: Revamping Trails--(最短路 + 动态规划)
- 数组、方法和字符串 - 1
- ZOJ 3872 - Team Formation(DP)
- Linux 创建快捷方式
- 仿QQ概念版的登陆界面
- 电子元器件-------电阻(R)
- 《Thinking in JAVA》读书笔记_第二章_2.1
- HDU 2013 2014 2015 2016
- ARM处理器模式