双倍经验

写这两题之前被大佬剧透了呜呜呜。

分层图+最短路。

因为有$k$次机会能够把路径的费用变为$0$,我们可以建$k + 1$层图,对于每一层图我们把原来的边权和双向边连到上面去,而对于层与层之间的连接,对于每一条边,我们连上从下层到上层的有向边,边权为$0$。

这样子其实保证了它并不会向下走,也就是说一定在不断消耗着$k$次机会,对应了使用不超过$k$次机会,这样子的话我们最后只要求出第一层的$st$到第$k + 1$层的$ed$之间的最短路就是答案了。

我使用的是堆优化dijkstra。

时间复杂度$O(nlogn)$,这里的$n$应是$nk$级别的。

Code:

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef pair <int, int> pin; const int N = 4e5 + ;
const int M = 3e6 + ; int n, m, K, tot = , head[N], dis[N];
bool vis[N]; struct Edge {
int to, nxt, val;
} e[M << ]; inline void add(int from, int to, int val) {
e[++tot].to = to;
e[tot].val = val;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int id(int depth, int now) {
return n * (depth - ) + now;
} priority_queue <pin> Q;
void dij(int st) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, , sizeof(vis));
Q.push(pin(dis[st] = , st)); for(; !Q.empty(); ) {
int x = Q.top().second; Q.pop();
if(vis[x]) continue;
vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(dis[y] > dis[x] + e[i].val) {
dis[y] = dis[x] + e[i].val;
Q.push(pin(-dis[y], y));
}
}
}
} int main() {
// freopen("testdata.in", "r", stdin); read(n), read(m), read(K); int st, ed; st = , ed = n;
st = id(, st), ed = id(K + , ed); for(int x, y, v, i = ; i <= m; i++) {
read(x), read(y), read(v);
for(int j = ; j <= K + ; j++)
add(id(j, x), id(j, y), v), add(id(j, y), id(j, x), v);
for(int j = ; j <= K; j++)
add(id(j, x), id(j + , y), ), add(id(j, y), id(j + , x), );
} dij(st); printf("%d\n", dis[ed]);
return ;
}

Luogu 2939

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef pair <int, int> pin; const int N = 2e5 + ;
const int M = 3e6 + ; int n, m, K, tot = , head[N], dis[N];
bool vis[N]; struct Edge {
int to, nxt, val;
} e[M << ]; inline void add(int from, int to, int val) {
e[++tot].to = to;
e[tot].val = val;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int id(int depth, int now) {
return n * (depth - ) + now;
} priority_queue <pin> Q;
void dij(int st) {
memset(dis, 0x3f, sizeof(dis));
memset(vis, , sizeof(vis));
Q.push(pin(dis[st] = , st)); for(; !Q.empty(); ) {
int x = Q.top().second; Q.pop();
if(vis[x]) continue;
vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(dis[y] > dis[x] + e[i].val) {
dis[y] = dis[x] + e[i].val;
Q.push(pin(-dis[y], y));
}
}
}
} int main() {
read(n), read(m), read(K); int st, ed; read(st), read(ed);
st = id(, st + ), ed = id(K + , ed + ); for(int x, y, v, i = ; i <= m; i++) {
read(x), read(y), read(v);
x++, y++;
for(int j = ; j <= K + ; j++)
add(id(j, x), id(j, y), v), add(id(j, y), id(j, x), v);
for(int j = ; j <= K; j++)
add(id(j, x), id(j + , y), ), add(id(j, y), id(j + , x), );
} dij(st); printf("%d\n", dis[ed]);
return ;
}

Luogu 4568

Luogu 2939 [USACO09FEB]改造路Revamping Trails && Luogu 4568 [JLOI2011]飞行路线的更多相关文章

  1. 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2939 本来说是双倍经验题,跟飞行路线一样的,结果我飞行路线拿deque优化SPFA过了这里过不了了. 所以多 ...

  2. LUOGU P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  3. P2939 [USACO09FEB]改造路Revamping Trails

    P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...

  4. 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解

    P2939 [USACO09FEB]改造路Revamping Trails 题目描述 Farmer John dutifully checks on the cows every day. He tr ...

  5. [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线

    题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...

  6. [USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  7. 洛谷P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有\(N\))个牧场.由\(M\)条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场\(1\)出发到牧场\(N\)去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰 ...

  8. 洛谷 P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  9. [USACO09FEB]改造路Revamping Trails

    题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...

随机推荐

  1. 网络基础之网络协议篇---CS架构--网络通信--osi 协议---套接字socket--粘包

    1 C\S 客户端/服务器架构: .硬件 C/S架构 (打印机) .软件 C/S 架构 互联网中处处是C/S架构 如黄色网站是服务端,你的浏览器是客户端(B/S架构也是C/S架构的一种) 腾讯作为服务 ...

  2. 3.4 常用的两种 layer 层 3.7 字体与文本

    3.4 常用的两种 layer 层  //在cocos2d-x中,经常使用到的两种 layer 层 : CCLayer 和 CCLayerColor //CCLayer 的创建 CCLayer* la ...

  3. LightOJ 1224 DNA Prefix

    Given a set of n DNA samples, where each sample is a string containing characters from {A, C, G, T}, ...

  4. SQL夯实基础(四):子查询及sql优化案例

    首先我们先明确一下sql语句的执行顺序,如下有前至后执行: (1)from  (2) on   (3) join  (4) where  (5)group by  (6) avg,sum...  (7 ...

  5. 四、Jmeter--参数化

    一.CSV 参数化 1.我们做性能测试需要并发多个用户,为了真实模拟用户行为,我们需要模拟多个不同的用户登录,这是我们就需要进行参数化.这里我们选择比较常用的参数化方法-CSV Data Set Co ...

  6. hihoCoder#1050(树中最长路)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一 ...

  7. [转] linux 启动文件及设置环境变量

    系统级启动文件  ==================================== 1./etc/rc  主启动文件,不要修改它 2./etc/rc.conf  决定启动哪些系统自带的守护进程 ...

  8. Python运行错误解释

    BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基类 StopIte ...

  9. Hudson和Jenkins的关系

    Jenkins is an open source continuous integration tool written in Java. The project was forked from H ...

  10. 杂项:Grunt

    ylbtech-杂项:Grunt GRUNTJavaScript 世界的构建工具 1. 返回顶部 1. 为何要用构建工具? 一句话:自动化.对于需要反复重复的任务,例如压缩(minification) ...