POJ 3635 Full Tank? 最短路DP
2016-03-03 13:35
351 查看
给出一个图(1≤n≤1000,0≤m≤10000),每个点有加油站费用ci,对于每个询问,油箱容积c的车从s到e的最小费用。
如果这个图是个DAG的话很显然我们可以直接使用动态规划求解。
但它现在不是DAG,回忆起GD神犇姜碧野(应该没记错。。)有篇论文是讲SPFA的应用的,不过没负边,用Dij+Heap即可求有环的动态规划。
Total Submissions: 6910 Accepted: 2248
To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.
10 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4
impossible
如果这个图是个DAG的话很显然我们可以直接使用动态规划求解。
但它现在不是DAG,回忆起GD神犇姜碧野(应该没记错。。)有篇论文是讲SPFA的应用的,不过没负边,用Dij+Heap即可求有环的动态规划。
#include <cstdio> #include <cstring> #include <queue> #define rep(i,j,k) for(i=j;i<k;i++) using namespace std; const int N = 1001, M = 20001, inf = 0x7f7f7f7f; int h , p[M], v[M], w[M], c , cnt = 0; int dp [101], vis [101], s, t, cap; struct Data { int x, o, c; }; bool operator <(Data a, Data b){ return a.c>b.c; } void add(int x, int y, int z) { p[++cnt] = h[x]; w[cnt] = z; v[cnt] = y; h[x] = cnt; } int dij() { priority_queue<Data> q; memset(dp, 127, sizeof dp); memset(vis, 0, sizeof vis); dp[s][0] = 0; q.push((Data) { s, 0 }); while (!q.empty()) { Data u = q.top(); q.pop(); vis[u.x][u.o] = 1; if (u.x == t) return u.c; if (u.o < cap && !vis[u.x][u.o + 1] && dp[u.x][u.o + 1] > dp[u.x][u.o] + c[u.x]) { dp[u.x][u.o + 1] = dp[u.x][u.o] + c[u.x]; q.push((Data) {u.x, u.o + 1, dp[u.x][u.o + 1]}); } for (int i = h[u.x]; i; i = p[i]) if (u.o >= w[i] && !vis[v[i]][u.o - w[i]] && dp[v[i]][u.o - w[i]] > dp[u.x][u.o]) { dp[v[i]][u.o - w[i]] = dp[u.x][u.o]; q.push((Data) {v[i], u.o - w[i], dp[v[i]][u.o - w[i]]}); } } return -1; } int main() { int n, i, m, q; scanf("%d%d", &n, &m); rep(i,0,n) scanf("%d", c + i); while (m--) { scanf("%d%d%d", &s, &t, &cap); add(s, t, cap); add(t, s, cap); } scanf("%d", &q); while (q--) { scanf("%d%d%d", &cap, &s, &t); int re = dij(); if (re == -1) puts("impossible"); else printf("%d\n", re); } return 0; }
Full Tank?
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6910 Accepted: 2248
Description
After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.
Input
The first line of input gives 1 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ u, v < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 100, s and e, where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.Output
For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or “impossible” if there is no way of getting from s to e with the given car.Sample Input
5 510 10 20 12 13
0 1 9
0 2 8
1 2 1
1 3 11
2 3 7
2
10 0 3
20 1 4
Sample Output
170impossible
相关文章推荐
- linux面试之你认识的fork、vfork、clone
- GIS in logistics(2)
- LINUX下SVN安装 配置 web目录同步
- 终于会用c#中的delegate(委托)和event(事件)了
- Python的Pexpect详解
- Android消息机制之四---Looper,Handler,Message例子
- Android Activity 四种启动模式
- c++的POD类型详解
- 更新windows10 world 和 excel 打不开
- 使用Mac OS X自带的Vim安装使用Command-T插件
- 如何在web.xml中配置jsp的访问路径
- 蓝桥杯历届-土地测量
- android自定义view(一),打造绚丽的验证码
- 我为什么开通微信公众号
- iOS开发过程中 xcode文件与Finder中文件保持一致 + 支付宝集成出错
- 好客户端是怎样炼成的
- usaco 1-1-3 Friday the Thirteenth
- 浅谈json,自己初认识json,写下自己对其概念的理解。
- PCB布线及相关规则设置
- 管道createnamepipe