DIjkstra (reverse side) POJ 3268 Silver Cow Party | | POJ 1511 Invitation Cards

Source: Internet
Author: User

Topic Portal 1 2

Test instructions: The map, all points go to the X-point, at the point of return from X, ask the largest one of the shortest distance

Analysis: Run the most short-circuit on the pros and cons of the graph, open two arrays to record the distance from X to the rest of the points, so that you can find the shortest possible short circuit and back.

POJ 3268

#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring>using namespace Std;typedef long long ll;const int N = 1e6 + 5;const int E = n;const int INF = 0x3f3f3f3f;s Truct edge{int u, V, W, NEX; Edge () {}edge (int u, int v, int w, int nex): U (U), V (v), W (W), NEX (NEX) {}bool operator < (const Edge &AMP;R) const {return w > R.W;}} Edge[2][e];int head[n];int d[2][n];bool vis[n];int N, M, x, E;inline int read (void) {int f = 1, ret = 0;char ch = getchar ( ); while (Ch > ' 9 ' | | ch < ' 0 ') {if (ch = = '-') F = -1;ch = GetChar ();} while (Ch <= ' 9 ' && ch >= ' 0 ') {ret = RET * + CH-' 0 '; ch = GetChar ();} return ret * f;} void init (void) {memset (head,-1, sizeof (head)); e = 0;} void Add_edge (int u, int v, int w) {Edge[0][e] = Edge (U, V, W, Head[u]); Head[u] = e++;} void Build_re_graph (void) {memset (head,-1, sizeof (head)), E = 0;for (int i=0; i<m; ++i) {edge[1][e] = Edge (Edge[0][i]. V, EDGE[0][I].U, EDGE[0][I].W, HEAD[EDGE[0][I].V]); HEAD[EDGE[0][I].V] = e++;}} void SPFA (int s, int tp) {memset (D[TP], INF, sizeof (D[TP])), memset (Vis, false, sizeof (VIS));d [tp][s] = 0;vis[s] = true;  Queue<int> Que;que.push (s); while (!que.empty ()) {int u = que.front (); Que.pop (); Vis[u] = false;for (int i=head[u]; ~i; I=edge[tp][i].nex) {int v = edge[tp][i].v, W = edge[tp][i].w;if (D[tp][v] > D[tp][u] + W) {D[tp][v] = D[tp][u] + w;if (!v Is[v]) {Vis[v] = True;que.push (v);}}}} void Dijkstra (int s, int tp) {memset (Vis, false, sizeof (VIS)), memset (D[TP], INF, sizeof (D[TP]));d [Tp][s] = 0;priority_q Ueue<edge> Que;que.push (Edge (0, S, D[tp][s], 0)), while (!que.empty ()) {int u = que.top (). V;que.pop (); if (Vis[u]) continue;for (int i=head[u]; ~i; i=edge[tp][i].nex) {int v = edge[tp][i].v, W = edge[tp][i].w;if (!vis[v] && d[tp][ V] > D[tp][u] + W) {D[tp][v] = D[tp][u] + w;que.push (Edge (0, V, D[tp][v], 0));}}} int Get_max (void) {int ret = 0;for (int i=1; i<=n; ++i) {ret = max (ret, d[0][i] + d[1][I]);} return ret;} int main (void) {while (scanf ("%d%d%d", &n, &m, &x) = = 3) {init (); for (int u, V, W, i=0; i<m; ++i) {scanf ("% D%d%d ", &u, &v, &w); Add_edge (U, V, W);} SPFA (x, 0);D Ijkstra (x, 0); Build_re_graph ()//SPFA (x, 1);D Ijkstra (x, 1); int ans = Get_max ();p rintf ("%d\n", ans);} return 0;}

There is a similar problem, is to seek the most and, in fact, are the same

POJ 1511

#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring>using namespace Std;typedef long long ll;const int N = 1e6 + 5;const int E = n;const int INF = 0x3f3f3f3f;s Truct edge{int u, V, W, NEX; Edge () {}edge (int u, int v, int w, int nex): U (U), V (v), W (W), NEX (NEX) {}bool operator < (const Edge &AMP;R) const {return w > R.W;}} Edge[2][e];int head[n];int d[n];bool vis[n];int N, M, e;inline int read (void) {int f = 1, ret = 0;char ch = getchar (); whil E (ch > ' 9 ' | | ch < ' 0 ') {if (ch = = '-') F = -1;ch = GetChar ();} while (Ch <= ' 9 ' && ch >= ' 0 ') {ret = RET * + CH-' 0 '; ch = GetChar ();} return ret * f;} void init (void) {memset (head,-1, sizeof (head)); e = 0;} void Add_edge (int u, int v, int w) {Edge[0][e] = Edge (U, V, W, Head[u]); Head[u] = e++;} void Build_re_graph (void) {memset (head,-1, sizeof (head)), E = 0;for (int i=0; i<m; ++i) {edge[1][e] = Edge (Edge[0][i]. V, edge[0][i].u, EDGE[0][I].W, head[EDGE[0][I].V]); HEAD[EDGE[0][I].V] = e++;}} void SPFA (int s, int tp) {memset (d, INF, sizeof (d)), memset (Vis, false, sizeof (VIS));d [s] = 0;vis[s] = True;queue<int > Que;que.push (s), while (!que.empty ()) {int u = que.front (); Que.pop (); Vis[u] = false;for (int i=head[u]; ~i; i=edge[ Tp][i].nex) {int v = edge[tp][i].v, W = edge[tp][i].w;if (D[v] > D[u] + W) {D[v] = D[u] + w;if (!vis[v]) {Vis[v] = True;qu E.push (v);}}}} ll get_sum (void) {LL ret = 0;for (int i=1; i<=n; ++i) {ret + = D[i];} return ret;} int main (void) {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n, &m), Init (); for (int u, V, W, i=0; i<m ; ++i) {//scanf ("%d%d%d", &u, &v, &w); u = Read (); v = Read (); w = read (); Add_edge (U, V, W);} SPFA (1, 0); ll ans = get_sum (); Build_re_graph (); SPFA (1, 1), ans + = get_sum ();p rintf ("%lld\n", ans); return 0;}

  

DIjkstra (reverse side) POJ 3268 Silver Cow Party | | POJ 1511 Invitation Cards

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.