HDU 1676 Full Tank? 限制最短路(difficult)
2014-03-21 22:52
351 查看
点击打开链接
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 155 Accepted Submission(s): 71
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.
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 5
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
Sample Output
170
impossible
Source
2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
有n个城市和m条路,一辆车要从s城市到e城市,而且这辆车能够乘的油的容量为cap,每走1单位距离就耗费1单位油。每个城市都有加油站,但是每个加油站的价格不同。让你判断这辆车能否到达城市e,如果能够达到,那么所要耗费的最少价格是多少。
每个城市油的价格不同,所以单纯的求最短路肯定行不通。用dp[i][j]来维护cost,代表到达到达第i个城市时,油箱的剩余油量为j时的最小花费。然后对于每个城市油量都是+1,然后更新与此城市相连的城市。
Full Tank?
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 155 Accepted Submission(s): 71
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.
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 5
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
Sample Output
170
impossible
Source
2008
“Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)
有n个城市和m条路,一辆车要从s城市到e城市,而且这辆车能够乘的油的容量为cap,每走1单位距离就耗费1单位油。每个城市都有加油站,但是每个加油站的价格不同。让你判断这辆车能否到达城市e,如果能够达到,那么所要耗费的最少价格是多少。
每个城市油的价格不同,所以单纯的求最短路肯定行不通。用dp[i][j]来维护cost,代表到达到达第i个城市时,油箱的剩余油量为j时的最小花费。然后对于每个城市油量都是+1,然后更新与此城市相连的城市。
//250MS 4900K #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> #include<queue> #define M 1007 using namespace std; int n,m,cap,s,e,num; int val[M],dp[M][M],head[M]; struct node { int u,cost,res;//u代表节点,cost代表当前节点的花费,res代表此节点剩余的油 bool operator < (const node a)const { return cost>a.cost; } }; struct E { int v,to,dist; } edg[M*20]; vector<int>G[M]; void addedge(int u,int v,int dist) { edg[num].v=v; edg[num].dist=dist; edg[num].to=head[u]; head[u]=num++; } void init() { num=0; memset(head,-1,sizeof(head)); memset(val,0,sizeof(val)); for(int i=0; i<n; i++) G[i].clear(); } void bfs() { for(int i=0; i<n; i++) for(int j=0; j<=cap; j++) dp[i][j]=0; priority_queue<node> q; q.push((node){s,0,0}); while(!q.empty()) { node x=q.top(); q.pop(); int u=x.u,cost=x.cost,res=x.res; if(u==e) { printf("%d\n",cost); return ; } if(res<cap&&(dp[u][res+1]==0||dp[u][res+1]>cost+val[u]))//如果多加1单位油,能够花费更少 { dp[u][res+1]=cost+val[u]; q.push((node){u,dp[u][res+1],res+1}); } for(int i=head[u]; i!=-1; i=edg[i].to)//更新与此节点相邻的节点 { int v=edg[i].v,dist=edg[i].dist; if(res<dist)continue;//当前油量不够到达下一个节点 if(dp[v][res-dist]==0||dp[v][res-dist]>cost)//能够达到下一个节点,而且花费更少 { dp[v][res-dist]=cost; q.push((node){v,cost,res-dist}); } } } printf("impossible\n"); return ; } int main() { while( scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=0; i<n; i++) scanf("%d",&val[i]); int a,b,c; for(int i=1; i<=m; i++) { scanf("%d%d%d",&a,&b,&c); addedge(a,b,c); addedge(b,a,c); } int q; scanf("%d",&q); while(q--) { scanf("%d%d%d",&cap,&s,&e); bfs(); } } return 0; }
相关文章推荐
- linux 简单的mysql备份和导入,以及文件的备份和导入
- 使用 Video4Linux 采集USB摄像头的图像,并保存到一张 jpg图像文件中的程序
- 仿jdk的LinkedList代码实现
- Facebook HHVM 和 Hack 手册----1.什么是Hack?
- linux 简单的mysql备份和导入,以及文件的备份和导入
- 14.6: 脚本化状态栏
- Linux服务器的初步配置流程
- SQLServer创建约束
- git 常用命令总结
- 代码质量
- Cocos2D-x游戏开发之九:屏幕触摸一:基本屏幕操作响应
- 全备份、增量备份、差异备份和永久增量备份
- ADS1.2 DEBUG调试时提示:erro starting external process,Process error code 87(0x57)
- 14.5: 简单的对话框
- this
- 深入java虚拟机 - 垃圾收集 - 火车算法
- Linux下SVN的备份与恢复
- Wildcard Matching
- iOS面试题
- 黑马程序员-第二十三天(字符编码)