HDU 5385 The path (shortest circuit + construction)

Source: Internet
Author: User

Title Link: Hdu 5385 the path


Maintain a l,r,l increment starting from 2, r decrements from N, each distance value L,r at most one step, and each time a point mark is reached, note that the last maximum value can only be one.


#include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm >using namespace std;typedef pair<int,int> pii;const int maxn = 1e5 + 5;int N, M, D[MAXN], R[maxn];int X[MAXN], Y  [MAXN], w[maxn];vector<pii> g[maxn];void init () {scanf ("%d%d", &n, &m); memset (r, 0, sizeof (r)); for (int i = 1; I <= N; i++) {D[i] =-1; G[i].clear ();} for (int i = 1; I <= M; i++) {W[i] = -1;scanf ("%d%d", &x[i], &y[i]); G[x[i]].push_back (Make_pair (y[i], i));//g[y[i]].push_back (Make_pair (x[i], i));}} void Solve () {d[1] = 0;queue<int> Q; Q.push (1); int L = 2, r = n;for (int d = 1; d <= N; d++) {if (R < L) Break;while (! Q.empty ()) {int u = q.front (); Q.pop (); for (int i = 0; i < g[u].size (); i++) {int v = g[u][i].first;if (r[v]) continue; R[V] = G[u][i].second;}} if (R[l]) {D[l] = d;int u = x[r[l]] + y[r[l] "-l; W[r[l]] = d[l]-d[u]; Q.push (l++);} if (R > L && r[r]) {D[r] = d;int u = x[r[r]] + y[r[r]]-r; W[r[r]] = D[R]-d[u]; Q.push (r--);}} /*for (int i = 1; I <= N; i++) printf ("%d", D[i]);p rintf ("\ n"), */}int Main () {int cas;scanf ("%d", &cas); while (CAS --) {init (); solve (); for (int i = 1; I <= M; i++) printf ("%d\n", w[i] = = 1? N:w[i]);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 5385 The path (shortest circuit + construction)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.