[CODEVS1912] 汽车加油行驶问题(分层图最短路)
吐槽:神tm网络流
dis[i][j][k] 表示到 (i, j) 还有 k 油的最优解
然后跑spfa,中间分一大堆情况讨论
1.当前队头还有油
1.目标点有加油站——直接过去
2.目标点每加油站——1.直接过去
2.在当前点召唤一个加油站再过去
2.没油——召唤加油站再走
——代码
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 101
#define min(x, y) ((x) < (y) ? (x) : (y)) inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} int n = read(), k = read(), a = read(), b = read(), c = read(), ans = ~( << );
int map[N][N], dis[N][N][N];
int dx[] = {, , , -}, dy[] = {, , -, }, cos[] = {, , b, b};
bool vis[N][N][N]; struct node
{
int x, y, res;
node(int x = , int y = , int res = ) : x(x), y(y), res(res) {}
}; std::queue <node> q; inline void init(int x, int y, int res, int cost)
{
if(dis[x][y][res] > cost)
{
dis[x][y][res] = cost;
if(!vis[x][y][res])
{
vis[x][y][res] = ;
q.push(node(x, y, res));
}
}
} inline void spfa()
{
node now;
int i, x, y, res, cost;
memset(dis, / , sizeof(dis));
dis[][][k] = ;
q.push(node(, , k));
while(!q.empty())
{
now = q.front();
q.pop();
vis[now.x][now.y][now.res] = ;
for(i = ; i < ; i++)
{
x = now.x + dx[i];
y = now.y + dy[i];
if(!x || x > n || !y || y > n) continue;
cost = dis[now.x][now.y][now.res] + cos[i];
if(now.res)
{
if(map[x][y]) init(x, y, k, cost + a);
else
{
init(x, y, now.res - , cost);
init(x, y, k - , cost + a + c);
}
}
else init(x, y, k - , cost + a + c);
}
}
} int main()
{
int i, j;
for(i = ; i <= n; i++)
for(j = ; j <= n; j++)
map[i][j] = read();
spfa();
for(i = ; i <= k; i++) ans = min(ans, dis[n][n][i]);
printf("%d\n", ans);
return ;
}
[CODEVS1912] 汽车加油行驶问题(分层图最短路)的更多相关文章
- 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)
[题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...
- 洛谷P4009 汽车加油行驶问题(分层最短路)
传送门 说好的网络流24题呢……上次是状压dp,这次怎么又最短路了…… 不过倒是用这题好好学了一下分层图最短路 把每一个位置$(x,y)$,油量剩余$k$表示为一个状态,然后转化成一个$n$进制数,这 ...
- P4009 汽车加油行驶问题
P4009 汽车加油行驶问题 最短路 清一色的spfa....送上一个堆优化Dijkstra吧(貌似代码还挺短) 顺便说一句,堆优化Dj跑分层图灰常好写 #include<iostream> ...
- 洛谷 P4009 汽车加油行驶问题 解题报告
P4009 汽车加油行驶问题 题目描述 给定一个\(N×N\)的方形网格,设其左上角为起点◎,坐标(1,1) ,\(X\)轴向右为正,\(Y\)轴向下为正,每个方格边长为1 ,如图所示. 一辆汽车从起 ...
- 汽车加油行驶(cogs 737)
«问题描述:给定一个N*N 的方形网格,设其左上角为起点◎,坐标为(1,1),X 轴向右为正,Y轴向下为正,每个方格边长为1,如图所示.一辆汽车从起点◎出发驶向右下角终点▲,其坐标为(N,N).在若干 ...
- 【题解】【网络流24题】汽车加油行驶问题 [P4009] [Loj6223]
[题解][网络流24题]汽车加油行驶问题 [P4009] [Loj6223] 传送门:汽车加油行驶问题 \([P4009]\) \([Loj6223]\) [题目描述] 给出一个 \(N \times ...
- poj3635Full Tank?[分层图最短路]
Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7248 Accepted: 2338 Descri ...
- HDU 5669 线段树优化建图+分层图最短路
用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...
- BZOJ 2763 分层图最短路
突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...
随机推荐
- [R] 简单笔记(一)
library(lattice) xyplot(Petal.Length~Petal.Width,data=iris,goups = Species)//画分类图 plot(model,subdata ...
- 2407: C语言习题 整数转换成字符串
2407: C语言习题 整数转换成字符串 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 917 Solved: 416[Submit][Status] ...
- python-判断alter是否存在
from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait f ...
- mysql group by的特殊性
SELECT create_year, userno , sum(sal) FROM user GROUP BY userno 以上语句,在oracle 或sql server肯定是语法错误 因为g ...
- UI Testing in Xcode 7
参考文章: UI Testing in Xcode - WWDC 2015https://developer.apple.com/videos/play/wwdc2015-406/ Document ...
- 解决 cocos2dx iOS/mac 设置纹理寻址模式后纹理变黑的问题
sprite:getTexture():setTexParameters(gl.LINEAR,gl.LINEAR,gl.REPEAT,gl.REPEAT) 在安卓设备上,设置了纹理自定义寻址模式,纹理 ...
- win8/10 bcdboot引导修复命令的原理和使用方法
win8/10 bcdboot引导修复命令的原理和使用方法 [迅维网原创文章禁止转载] (本文所述已用UEFI+GPT.BIOS+MBR,WIN10 64位企业版和专业版测试过) 在win8/10系统 ...
- 14-15.Yii2.0模型的创建/读取数据使用,框架防止sql注入
目录 创建数据库 表article 配置 db.php 连接数据库 创建控制器 HomeController.php 创建models 创建数据库 表article 1.创建库表 CREATE TAB ...
- Lecture 3
surface models 1. The two main methods of creating surface models are interpolation and triangulatio ...
- python中打印金字塔和九九乘法表的几种方法
# 打印九九乘法表for i in range(1,10): for j in range(1,i+1): # x=i*j # print(i,'*',j,'=',x,end=' ') print(' ...