hdu 1676(二维Dijkstra)
来源:互联网 发布:数据分解传输技术 编辑:程序博客网 时间:2024/06/05 17:26
Full Tank?
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 142 Accepted Submission(s): 64
Problem 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.
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 130 1 90 2 81 2 11 3 112 3 7210 0 320 1 4
Sample Output
170impossible
Source
2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
Recommend
lcy
n个城市,m条路,一个车有个有容量上限的油箱,各个城市加油的费用不同,现在求起点到终点的最小费用。因为各个城市加油的费用不同,所以简单的走最短路不一定是最优的。我们需要重新设计状态,可以想到到每个城市时的油量是有重要影响的,所以把状态设计成dp[i][j]表示到第i个城市剩余j油量的最小花费,然后状态转移是每次要么油量加1(加k的油量可以由k-1的油量的状态到达),要么走向相邻城市。注意这里的vis标记是出队的时候标记,而不是入队。
#include <iostream>#include <cstring>#include <cstdio>#include <string>#include <algorithm>#include <map>#include <vector>#include <queue>#include <set>#include <cmath>using namespace std;typedef long long LL;typedef pair<int,pair<int,int> > P;typedef pair<int,int> PP;const int maxn = 1000 + 5;const int INF = 1000000000;const int maxc = 100 + 5;int p[maxn];struct Edge{ int to,dis; Edge(int to,int dis){ this -> to = to; this -> dis = dis; }};vector<Edge> G[maxn];priority_queue<P> Q;int vis[maxn][maxc];int s,e,c;int bfs(){ while(!Q.empty()){ P p1 = Q.top();Q.pop(); PP p2 = p1.second; int price = -p1.first; int pos = p2.first; int gas = p2.second; vis[pos][gas] = 1; if(pos == e) return price; if(gas < c && vis[pos][gas+1] == 0){ Q.push(P(-(price+p[pos]),PP(pos,gas+1))); } for(int i = 0;i < G[pos].size();i++){ int to = G[pos][i].to; int dis = G[pos][i].dis; if(dis <= gas && vis[to][gas-dis] == 0){ Q.push(P(-price,PP(to,gas-dis))); } } } return -1;}int main(){ int n,m; while(scanf("%d%d",&n,&m) != EOF){ for(int i = 0;i < n;i++) scanf("%d",&p[i]); for(int i = 0;i < n;i++) G[i].clear(); for(int i = 0;i < m;i++){ int u,v,d; scanf("%d%d%d",&u,&v,&d); G[u].push_back(Edge(v,d)); G[v].push_back(Edge(u,d)); } int q; scanf("%d",&q); while(q--){ scanf("%d%d%d",&c,&s,&e); while(!Q.empty()) Q.pop(); memset(vis,0,sizeof(vis)); Q.push(P(0,PP(s,0))); int ans = bfs(); if(ans == -1) printf("impossible\n"); else printf("%d\n",ans); } } return 0;}
- hdu 1676(二维Dijkstra)
- HDU 4179 二维的Dijkstra
- hdu 2544 (dijkstra)
- hdu 2544(Dijkstra)
- HDU Today ((Dijkstra)
- HDUOJ_2112(HDU Today )(dijkstra)
- hdu 4725(Dijkstra算法)
- hdu 2112 HDU Today (dijkstra)
- HDU 2112 HDU Today(Dijkstra)
- HDU-#2112 HDU Today(Dijkstra+map)
- hdu 2112 HDU Today (Dijkstra + map)
- hdu-2112-HDU Today(dijkstra + map)
- HDU 2112 HDU Today(dijkstra算法)
- hdu 2112 HDU Today &&(dijkstra)
- hdu 2859 (二维dp)
- hdu 1227(二维dp)
- hdu 2544 最短路(Dijkstra 备忘)
- hdu - 2851 - Lode Runner(Dijkstra)
- 水晶报表设计TextObject/PictureObject为不可见
- php之路 2——php数据类型以及常量使用
- UML类图关系大全
- 最大字段和问题 难点分析和C++实现9
- 系统的思考性能问题
- hdu 1676(二维Dijkstra)
- squid,nginx,lighttpd反向代理的区别
- 如何在任务栏添加托盘图标
- MySQL数据库的注释
- 打造专利信息检索平台 服务创新型国家建设
- 小白
- 【LeetCode OJ】Single Number II
- 11gr2 oracle concepts(翻译) --第九章 数据并发和一致性
- #pragma once&&#ifndef解释:头文件只被编译一次