HDU 5521 meeting (Shortest way, Dijstra)

Source: Internet
Author: User

Test instructions: There are n points, two people, one of whom live at point 1, the other one lives at point N, there is a M-point set, the number within the set represents any two point distance for Dis, now ask, if two people want to meet,

What is the shortest distance, and what points can be considered as meeting points?

Analysis: respectively, 1 and n the shortest operation, the most uncomfortable is the side too many, if you directly all save, then will be mle, so must optimize the edge. So we're going to write down the sides of every line,

Instead of 22, write down the number of each line, and the last query, the line number to navigate to which line, so that no hyper-memory. Here it is, and the rest is simple.

The code is as follows:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < Stack>using namespace Std;typedef Long Long ll;typedef pair<int, int> p;const int INF = 0x3f3f3f3f;const double I NF = 0x3f3f3f3f3f3f;const LL LNF = 100000000000000000;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int MAXN = 1e5 + 5;const int mod = 1e9 + 7;const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};i NT N, m;inline bool is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && c < m; }inline ll Max (ll A, ll b) {return a < b b:a;} inline ll Min (ll A, ll b) {return a > b b:a;} Vector<int> g[maxn];vector<int> B[MAXN]; LL D1[maXN]; LL d2[maxn];int t[maxn];bool vis[maxn];void dijstra (int s, ll* d) {priority_queue<p, vector<p>, GREATER&LT;P&G T    > PQ;    Pq.push (P (0, s));    Memset (Vis, false, sizeof Vis);    Fill (d, d+n+1, LNF);    D[s] = 0;  while (!pq.empty ()) {p p = pq.top ();        Pq.pop ();        int u = p.second;        if (D[u] < P.first) continue;            for (int i = 0; i < b[u].size (); ++i) {//from the number to find out in which line int v = b[u][i];            if (Vis[v]) continue;            VIS[V] = true;                for (int j = 0; J < g[v].size (); ++j) {int UV = G[v][j], w = t[v];                    if (D[uv] > D[u] + W) {D[uv] = D[u] + W;                Pq.push (P (D[UV], UV));  }}}}}int main () {int T;    Cin >> T;        for (int kase = 1; kase <= T; ++kase) {scanf ("%d%d", &n, &m);        for (int i = 1; I <= n; ++i) g[i].clear (), b[i].clear (); for (int i = 1; I <= m; ++i) {int k;            scanf ("%d%d", &t[i], &k);                for (int j = 0; j < K; ++j) {int V;                scanf ("%d", &v);        G[i].push_back (v);//Margin B[v].push_back (i);//downlink number}} dijstra (1, D1);        Dijstra (n, D2);        printf ("Case #%d:", Kase);        LL ans = LNF;        for (int i = 1; I <= n; ++i) ans = Min (ans, Max (D1[i], d2[i]));        if (ans = = LNF) puts ("Evil John");            else{printf ("%i64d\n", ans);            int cnt = 0;                    for (int i = 1; I <= n; ++i) if (ans = = Max (D1[i], d2[i])) {if (CNT) Putchar (');                    printf ("%d", I);                ++cnt;        } printf ("\ n"); }} return 0;}

  

HDU 5521 meeting (Shortest way, Dijstra)

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.