传送门

吐槽:神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] 汽车加油行驶问题(分层图最短路)的更多相关文章

  1. 【网络流24题】 No.15 汽车加油行驶问题 (分层图最短路i)

    [题意] 问题描述:给定一个 N*N 的方形网格,设其左上角为起点◎, 坐标为( 1, 1), X 轴向右为正, Y轴向下为正, 每个方格边长为 1, 如图所示. 一辆汽车从起点◎出发驶向右下角终点▲ ...

  2. 洛谷P4009 汽车加油行驶问题(分层最短路)

    传送门 说好的网络流24题呢……上次是状压dp,这次怎么又最短路了…… 不过倒是用这题好好学了一下分层图最短路 把每一个位置$(x,y)$,油量剩余$k$表示为一个状态,然后转化成一个$n$进制数,这 ...

  3. P4009 汽车加油行驶问题

    P4009 汽车加油行驶问题 最短路 清一色的spfa....送上一个堆优化Dijkstra吧(貌似代码还挺短) 顺便说一句,堆优化Dj跑分层图灰常好写 #include<iostream> ...

  4. 洛谷 P4009 汽车加油行驶问题 解题报告

    P4009 汽车加油行驶问题 题目描述 给定一个\(N×N\)的方形网格,设其左上角为起点◎,坐标(1,1) ,\(X\)轴向右为正,\(Y\)轴向下为正,每个方格边长为1 ,如图所示. 一辆汽车从起 ...

  5. 汽车加油行驶(cogs 737)

    «问题描述:给定一个N*N 的方形网格,设其左上角为起点◎,坐标为(1,1),X 轴向右为正,Y轴向下为正,每个方格边长为1,如图所示.一辆汽车从起点◎出发驶向右下角终点▲,其坐标为(N,N).在若干 ...

  6. 【题解】【网络流24题】汽车加油行驶问题 [P4009] [Loj6223]

    [题解][网络流24题]汽车加油行驶问题 [P4009] [Loj6223] 传送门:汽车加油行驶问题 \([P4009]\) \([Loj6223]\) [题目描述] 给出一个 \(N \times ...

  7. poj3635Full Tank?[分层图最短路]

    Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7248   Accepted: 2338 Descri ...

  8. HDU 5669 线段树优化建图+分层图最短路

    用线段树维护建图,即把用线段树把每个区间都标号了,Tree1中子节点有到达父节点的单向边,Tree2中父节点有到达子节点的单向边. 每次将源插入Tree1,汇插入Tree2,中间用临时节点相连.那么T ...

  9. BZOJ 2763 分层图最短路

    突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...

随机推荐

  1. ping请求超(iPV4)

    ping请求超(iPV4) arp –a(查看局域网全部IP) cmd 管理员运行 netsh winsock reset(重置Winsock目录借以恢复网络) etsh int ip reset r ...

  2. leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数

    这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...

  3. python_101_类方法

    class Dog(object): n=333 name='小虎子' def __init__(self,name): self.name=name @classmethod def eat(sel ...

  4. cocos2dx lua 热更新方案的实现

    (Upgrade.h) #include <stdio.h> #include "cocos2d.h" #include "framework/utils/U ...

  5. vue中文本域限制字数的方法

    用watch方法,来限制字数 <template> <div class="box"> <textarea v-model="title&q ...

  6. JS - Array.prototype.sort(compare)

    function compare(a, b) { return -1; // a 在 b 前面 return 1; // a 在 b 后面 return 0; // 并列排序,保持在源数组中的先后顺序 ...

  7. redis学习笔记(1)

    最近在学习redis,做了比较详细的学习笔记,分享给大家,欢迎一起讨论和学习 第一部分,简单介绍redis 和 redis的基本操作 NoSQL的特点 : 数据库种类繁多,但是一个共同的特点都是去掉关 ...

  8. VIM 编辑器 -使用详解记录

    1.什么是 vim? Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用.简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但 ...

  9. vue 使用lib-flexable,px2rem 进行移动端适配 但是引入的第三方UI组件 vux 的样式缩小,解决方案

    最近在写移动端项目,就想用lib-flexable,px2rem来进行适配,把px转换成rem但是也用到了第三方UI组件库vux,把这个引入发现一个问题就是vux的组件都缩小了,在网上找不到答案,最后 ...

  10. php使用curl访问https返回无结果的问题

    最近在做一个微信自动登录,发起验证以后回调页面获取openid时 curl函数返回空. $appid = "appid appid "; $secret = "secre ...