CodeForces 827F. Dirty Arkady's Kitchen
来源:互联网 发布:身材好的男生知乎 编辑:程序博客网 时间:2024/06/06 09:11
Arkady likes to walk around his kitchen. His labyrinthine kitchen consists of several important places connected with passages. Unfortunately it happens that these passages are flooded with milk so that it's impossible to pass through them. Namely, it's possible to pass through each passage in any direction only during some time interval.
The lengths of all passages are equal and Arkady makes through them in one second. For security reasons, Arkady can never stop, also, he can't change direction while going through a passage. In other words, if he starts walking in some passage, he should reach its end and immediately leave the end.
Today Arkady needs to quickly reach important place n from place 1. He plans to exit the place 1 at time moment 0 and reach the place nas early as he can. Please find the minimum time he should spend on his way.
The first line contains two integers n and m (1 ≤ n ≤ 5·105, 0 ≤ m ≤ 5·105) — the number of important places and the number of passages, respectively.
After that, m lines follow, each of them describe one passage. Each line contains four integers a, b, l and r (1 ≤ a, b ≤ n, a ≠ b, 0 ≤ l < r ≤ 109) — the places the passage connects and the time segment during which it's possible to use this passage.
Print one integer — minimum time Arkady should spend to reach the destination. If he can't reach the place n, print -1.
5 61 2 0 12 5 2 32 5 0 11 3 0 13 4 1 24 5 2 3
3
2 11 2 1 100
-1
In the first example Arkady should go through important places 1 → 3 → 4 → 5.
In the second example Arkady can't start his walk because at time moment 0 it's impossible to use the only passage.
题意:给一个图,每条边有一个出现时间[l, r],边权都为1,必须一直走,求1到n最短路
题解:
DP
想到DP边就好做多了,按时间奇偶和方向拆边
首先用一个堆维护边的DP值,然后它可以更新反向边,然后同向的边按照l排序之后有单调性,直接更新就好了
#include <bits/stdc++.h>#define xx first#define yy second#define mp make_pair#define pb push_back#define mset(x, y) memset(x, y, sizeof x)#define mcpy(x, y) memcpy(x, y, sizeof x)using namespace std;typedef long long LL;typedef pair < int, int > pii;inline int Read(){int x = 0, f = 1, c = getchar();for (; !isdigit(c); c = getchar())if (c == '-')f = -1;for (; isdigit(c); c = getchar())x = x * 10 + c - '0';return x * f;}const int MAXN = 500005;const int INF = 0x3f3f3f3f;struct Node { int v, x, t, i; bool operator < (const Node &b) const { return v > b.v; } };struct Edge { int p, l, r, v; };int n, m, cur[MAXN][2];vector < Edge > G[MAXN];vector < pii > adj[MAXN][2];vector < int > f[MAXN][2];priority_queue < Node > Q;int main(){#ifdef wxh010910freopen("data.in", "r", stdin);#endifn = Read(), m = Read();if (n == 1)return puts("0"), 0;for (int i = 1, u, v, l, r; i <= m; i ++)u = Read(), v = Read(), l = Read(), r = Read(), G[u].pb({v, l, r, G[v].size()}), G[v].pb({u, l, r, G[u].size() - 1});for (int i = 1; i <= n; i ++)for (int j = 0; j < 2; j ++){f[i][j].resize(G[i].size(), INF);for (int k = 0; k < G[i].size(); k ++)adj[i][j].pb(mp(G[i][k].l + ((G[i][k].l & 1) ^ j), k));sort(adj[i][j].begin(), adj[i][j].end());}for (int i = 0; i < G[1].size(); i ++)if (!G[1][i].l)f[1][0][i] = 0, Q.push({0, 1, 0, i});while (!Q.empty()){Node tmp = Q.top(); Q.pop();int x = tmp.x, t = tmp.t, p = tmp.i, r = G[x][p].v, y = G[x][p].p, v = f[x][t][p], d = G[x][p].r - ((G[x][p].r & 1) ^ t);if (tmp.v > v)continue;//printf("%d %d %d %d\n", x, t, p, v);if (f[y][t ^ 1][r] > v + 1 && G[x][p].l <= v + 1 && G[x][p].r >= v + 1)f[y][t ^ 1][r] = v + 1, Q.push({v + 1, y, t ^ 1, r});if (v > d)continue;while (cur[x][t] < G[x].size()){pii g = adj[x][t][cur[x][t]];if (g.xx > d)break;if (max(g.xx, v) < f[x][t][g.yy])f[x][t][g.yy] = max(g.xx, v), Q.push({max(g.xx, v), x, t, g.yy});cur[x][t] ++;}}int ans = INF;for (int i = 0; i < G[n].size(); i ++)for (int j = 0; j < 2; j ++)ans = min(ans, f[n][j][i]);if (ans == INF)ans = -1;return printf("%d\n", ans), 0;}
- CodeForces 827F. Dirty Arkady's Kitchen
- codeforces 827F. Dirty Arkady's Kitchen
- kitchen's memory of Singapore
- codeforces 727F. Polycarp's problems
- codeforces 858F Wizard's Tour
- 英语练习Mrs Lee's Kitchen
- Codeforces 858F ( Codeforces Round #434 Div. 2 F ) Wizard's Tour 图论dfs
- Codeforces 659F F
- [树链剖分 线段树] Codeforces 860E. Arkady and a Nobody-men
- Dr. Mao's Secrets of Longevity___Toxins in the Kitchen
- Lesson 25 Mrs. Smith's Kitchen 史密斯太太的厨房
- s'd'f's'f's'd
- codeforces 727F. Polycarp's problems DP or 二分预处理答案
- d's'f's'd
- Codeforces 234 F. Fence
- 【Codeforces 500F】Dp
- codeforces 234F - Fence
- F-Logo Turtle codeforces
- Linux IO模式及 select、poll、epoll详解
- hibernateUtil工具类的封装
- CSS学习笔记:简单样式与选择器(伪元素选择器存疑)
- KeepAlive详解
- TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
- CodeForces 827F. Dirty Arkady's Kitchen
- 关键字final
- 获取手机内所有正在运行的进程processinfo
- wordpress各种获取url函数总结
- python小程序-0015
- win10 svn图标不显示
- 使用共享内存实现进程间通信 -使用信号实现同步
- MYSQL数据库设计规范
- Centos updatedb命令不好使