CodeForces 827F. Dirty Arkady's Kitchen

4573 단어
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.
Input
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.
Output
Print one integer — minimum time Arkady should spend to reach the destination. If he can't reach the place n, print -1.
Examples
input
5 6
1 2 0 1
2 5 2 3
2 5 0 1
1 3 0 1
3 4 1 2
4 5 2 3

output
3

input
2 1
1 2 1 100

output
-1

Note
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 
#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 wxh010910
	freopen("data.in", "r", stdin);
#endif
	n = 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
", 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
", ans), 0; }

좋은 웹페이지 즐겨찾기