【codevs1912】汽车加油行驶问题 分层图最短路
来源:互联网 发布:three.js全景图预览 编辑:程序博客网 时间:2024/04/29 17:43
题目描述 Description
给定一个N*N 的方形网格,设其左上角为起点◎,坐标为(1,1),X 轴向右为正,Y
轴向下为正,每个方格边长为1,如图所示。一辆汽车从起点◎出发驶向右下角终点▲,其
坐标为(N,N)。在若干个网格交叉点处,设置了油库,可供汽车在行驶途中加油。汽车在
行驶过程中应遵守如下规则:
(1)汽车只能沿网格边行驶,装满油后能行驶K 条网格边。出发时汽车已装满油,在起
点与终点处不设油库。
(2)汽车经过一条网格边时,若其X 坐标或Y 坐标减小,则应付费用B,否则免付费用。
(3)汽车在行驶过程中遇油库则应加满油并付加油费用A。
(4)在需要时可在网格点处增设油库,并付增设油库费用C(不含加油费用A)。
(5)(1)~(4)中的各数N、K、A、B、C均为正整数,且满足约束:2 £ N £ 100,2 £ K £ 10。
设计一个算法,求出汽车从起点出发到达终点的一条所付费用最少的行驶路线。
输入描述 Input Description
第一行是N,K,A,B,C的值。第二行起是一
个N*N 的0-1 方阵,每行N 个值,至N+1 行结束。方阵的第i 行第j 列处的值为1 表示在
网格交叉点(i,j)处设置了一个油库,为0 时表示未设油库。各行相邻两个数以空格分隔。
输出描述 Output Description
程序运行结束时,将最小费用输出
样例输入 Sample Input
9 3 2 3 60 0 0 0 1 0 0 0 00 0 0 1 0 1 1 0 01 0 1 0 0 0 0 1 00 0 0 0 0 1 0 0 11 0 0 1 0 0 1 0 00 1 0 0 0 0 0 1 00 0 0 0 1 0 0 0 11 0 0 1 0 0 0 1 00 1 0 0 0 0 0 0 0
样例输出 Sample Output
12
数据范围及提示 Data Size & Hint
分k层,再好好研究一下建图…
需要注意的是要特判到终点正好没油的情况……坑爹……
#include<cstdio>#include<cstring>#include<iostream>#include<cstring>#include<queue>using namespace std;const int INF = 1000000010;const int SZ = 1000010;int head[SZ],nxt[SZ],tot = 1,n,k,N;struct edge{ int t,d;}l[SZ];void build(int f,int t,int d){ l[++ tot].t = t; l[tot].d = d; nxt[tot] = head[f]; head[f] = tot;}int getnode(int x,int y,int k){ return (x - 1) * n + y + k * n * n;}struct Heap{ int u,d;};bool operator <(Heap a,Heap b){ return a.d > b.d;}priority_queue<Heap> q;int dist[SZ];bool vis[SZ];int dij(int s){ memset(dist,63,sizeof(dist)); dist[s] = 0; q.push((Heap){s,0}); while(q.size()) { int u = q.top().u; q.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = head[u];i;i = nxt[i]) { int v = l[i].t; if(dist[v] > dist[u] + l[i].d) { dist[v] = dist[u] + l[i].d; q.push((Heap){v,dist[v]}); } } } int ans = INF; for(int k = 0;k <= ::k;k ++) ans = min(ans,dist[getnode(n,n,k)]); return ans;}int maps[233][233],A,B,C;const int dx[] = {0,-1,1,0};const int dy[] = {-1,0,0,1};void build_zw(int x,int y,int k){ for(int i = 0;i < 2;i ++) //左上 { int xx = x + dx[i]; int yy = y + dy[i]; if(xx > 0 && xx <= n && yy > 0 && yy <= n) { if(maps[xx][yy]) { if(k < ::k) build(getnode(x,y,k),getnode(xx,yy,::k),A + B); } else build(getnode(x,y,k),getnode(xx,yy,::k),C + B + A); if(k != 1 || (xx == n && yy == n)) build(getnode(x,y,k),getnode(xx,yy,k - 1),B); } } for(int i = 2;i < 4;i ++) //右下 { int xx = x + dx[i]; int yy = y + dy[i]; if(xx > 0 && xx <= n && yy > 0 && yy <= n) { if(maps[xx][yy]) { if(k < ::k) build(getnode(x,y,k),getnode(xx,yy,::k),A); } else build(getnode(x,y,k),getnode(xx,yy,::k),C + A); if(k != 1 || (xx == n && yy == n)) build(getnode(x,y,k),getnode(xx,yy,k - 1),0); } }}int main(){ scanf("%d%d%d%d%d",&n,&k,&A,&B,&C); for(int i = 1;i <= n;i ++) for(int j = 1;j <= n;j ++) scanf("%d",&maps[i][j]); for(int i = 1;i <= n;i ++) for(int j = 1;j <= n;j ++) { if(maps[i][j]) build_zw(i,j,::k); else for(int k = 1;k <= ::k;k ++) { build_zw(i,j,k); } } printf("%d\n",dij(getnode(1,1,k))); return 0;}
0 0
- 【codevs1912】汽车加油行驶问题 分层图最短路
- 汽车加油行驶问题(分层图最短路)
- 【网络流24题】汽车加油行驶(分层图+最短路)
- 汽车行驶加油问题
- 汽车加油行驶问题
- 汽车加油行驶问题
- 汽车加油行驶问题
- light oj 1254 - Prison Break (Dijkstra 汽车加油行驶最优问题 最短路)
- 网络流24题 之十五 汽车加油行驶问题 分层图
- 【线性规划与网络流24题】汽车加油行驶问题 分层图
- loj6223「网络流 24 题」汽车加油行驶问题(分层图spfa)
- [网络流24题-15] 汽车加油行驶 - 分层图
- 【dp】汽车加油行驶问题
- 汽车加油行驶问题专题
- 【最短路】汽车加油
- [codevs 1912] 汽车加油行驶问题
- 线性规划与网络流24题:《汽车加油行驶问题》
- 【网络流24题】汽车加油行驶问题
- OpenCV中文文档——Some Functions
- Android中 GsonFormat插件解析Jason 数据+和风天气接口解析案例
- [备份]这是Markdown编辑器的默认帮助文档
- HOJ 1640 Mobile Phone
- iOS项目工程中添加C文件,报错!
- 【codevs1912】汽车加油行驶问题 分层图最短路
- 淘宝大秒系统设计详解
- getBean
- Nodejs 一个简单的后台实例
- 解决自定义Shiro.Realm扩展类不能用注解(@Resource或@Autowire)自动装配的问题
- MYSQL操作中错误The user specified as a definer ('root'@'%') does not exist的解决
- Uva524——Prime Ring Problem
- Codeforces Round #273 (Div. 2)(A)模拟
- 自定义视图属性