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 <= ...
随机推荐
- linux命令学习笔记( 2 ) : cd 命令
Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. . 命 ...
- Android Volley完全解析(四),带你从源码的角度理解Volley
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17656437 经过前三篇文章的学习,Volley的用法我们已经掌握的差不多了,但是 ...
- UVA - 11107 Life Forms (广义后缀自动机)
题意:给你n个字符串,求出在超过一半的字符串中出现的所有子串中最长的子串,按字典序输出. 对这n个字符串建广义后缀自动机,建完后每个字符串在自动机上跑一遍,沿fail树向上更新所有子串结点的出现次数( ...
- Spring框架实现——远程方法调用RMI代码演示
1.spring_RMI02_server服务端02 <?xml version="1.0" encoding="UTF-8"?> <bean ...
- LeetCode 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- JavaWeb框架_Struts2_(一)----->Struts2 框架入门
1. 框架入门 2.1 Struts2简介 (1). Struts2是一种基于MVC模式的的轻量级Web开发框架. MVC模式:MVC全名是Model View Controller,是模型(mo ...
- Qt Creator 中的段落 注释的 快捷方法【转载】
原文网址:http://jingyan.baidu.com/article/d5c4b52bc2bd1dda560dc5bb.html 作为一名合格的程序员,漂漂亮亮的注释是必须的!!怎么在Qt Cr ...
- HDOJ1010 (DFS+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Django基础(四)
Form表单 Admin Django Form表单 django 中的form 一般有两种功能: 输入html 验证用户输入 1,先写一个form import re from django ...
- 四川第七届 E Rectangle
Rectangle frog has a piece of paper divided into nn rows and mm columns. Today, she would like to dr ...