双倍经验

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

分层图+最短路。

因为有$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. C#文件操作常用相关类(Directory类、File类、Path类)

    1.文件操作常用相关类 1)File //操作文件,静态类,对文件整体操作.拷贝.删除.剪切等 2)Directory //操作目录(文件夹),静态类 3)DirectoryInfo //文件夹的一个 ...

  2. HTTP协议 与 Requests库

    HTTP协议 与 Requests库: 1  HTTP协议: 2 URL作为网络定位的标识: >>>> 用户通过url来定位资源 >>>> 然后通过 g ...

  3. stl_relops.h

    stl_relops.h // Filename: stl_relops.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http ...

  4. CANopenSocket CANopenCGI.c hacking

    /**************************************************************************************** * CANopenS ...

  5. 「BJOI2018」链上二次求和

    「BJOI2018」链上二次求和 https://loj.ac/problem/2512 我说今天上午写博客吧.怕自己写一上午,就决定先写道题. 然后我就调了一上午线段树. 花了2h找到lazy标记没 ...

  6. nodepad++的python环境变量设置

    转:http://blog.csdn.net/memray/article/details/42041975

  7. 【转】Cron表达式简介

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  8. loj 2542 随机游走 —— 最值反演+树上期望DP+fmt

    题目:https://loj.ac/problem/2542 因为走到所有点的期望就是所有点期望的最大值,所以先最值反演一下,问题变成从根走到一个点集任意一点就停止的期望值: 设 \( f[x] \) ...

  9. ov2640数据

    问题部分解决,数据错误的原因是太快了.将0x11->3f  0xd3->7f 哈哈 问题解决 直接降低7670输出频率 调0x11到最大分频比 现在能完整抓拍QVGA的图像 不过就是采集速 ...

  10. Windows:cmd的使用

    1.如果在cmd.exe中无法运行软件(如python),因为在系统的环境变量中,path中没有该软件的安装路径: 2.通过pip安装软件:pip install 文件路径\文件全名,将软件安装在指定 ...