Luogu 2939 [USACO09FEB]改造路Revamping Trails && Luogu 4568 [JLOI2011]飞行路线
双倍经验
写这两题之前被大佬剧透了呜呜呜。
分层图+最短路。
因为有$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]飞行路线的更多相关文章
- 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解
题目链接:https://www.luogu.org/problemnew/show/P2939 本来说是双倍经验题,跟飞行路线一样的,结果我飞行路线拿deque优化SPFA过了这里过不了了. 所以多 ...
- LUOGU P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- P2939 [USACO09FEB]改造路Revamping Trails
P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails 题解
P2939 [USACO09FEB]改造路Revamping Trails 题目描述 Farmer John dutifully checks on the cows every day. He tr ...
- [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线
题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...
- [USACO09FEB]改造路Revamping Trails 分层最短路 Dijkstra BZOJ 1579
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- 洛谷P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有\(N\))个牧场.由\(M\)条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场\(1\)出发到牧场\(N\)去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰 ...
- 洛谷 P2939 [USACO09FEB]改造路Revamping Trails
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- [USACO09FEB]改造路Revamping Trails
题目描述 Farmer John dutifully checks on the cows every day. He traverses some of the M (1 <= M <= ...
随机推荐
- mysql分表和分区实际应用简介
一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这些区块可以在同一个磁盘 ...
- CANopenSocket CANopenCommand.c hacking
/***************************************************************************** * CANopenSocket CANop ...
- FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
/******************************************************************************** * FAT-fs (mmcblk0p ...
- linux中使用opdir_readdir读取目录中的信息
#include <dirent.h>#include <stdio.h>#include <stdlib.h> int main(int argc, char * ...
- linux下的第一个C程序及其编译方法
#include <stdio.h> #include <stdlib.h> int main(int argc, char ** argv) { printf(& ...
- UVALive - 3490 Generator (AC自动机+高斯消元dp)
初始有一个空串s,从前n个大写字母中不断随机取出一个字母添加到s的结尾,出现模式串t时停止,求停止时s的长度期望. 这道题解法不唯一,比较无脑的方法是对模式串t建一个单串AC自动机,设u为自动机上的一 ...
- Python3.7安装pyspider
下面是Python3.7安装pyspider的方式,能安装成功但是后期有很多问题,所以不建议,请使用3.5版本的Python进行安装!!!由于要做爬虫工作,所以学习pyspider框架,下面介绍安装步 ...
- 加密第四节_IPSec基本理论
加密第四节_IPSec基本理论 本节内容 IPSec简介 IPSec两种工作模式 判断隧道模式和传输模式 IPSec两种模型 IPSec两个数据库 IPSec基本理论 IPSec简介 提供了网络层的安 ...
- java服务覆盖率统计 jacoco ant
● 下载jacoco.exec.jacocoant.jar.jacocoagent.jar ● jvm启动参数中添加(tomcat服务原理一样) JACOCO_OPTS=JAVA_OPTS=" ...
- unix下网络编程之I/O复用(二)
select函数 该函数允许进程指示内核等待多个事件中的任何一个发生,并仅在有一个或是多个事件发生或经历一段指定的时间后才唤醒它.我们调用select告知内核对哪些描述字(就读.写或异常条件)感兴趣以 ...