# SPFA (negative ring) Lightoj 1074 Extended Traffic

Source: Internet
Author: User

Topic Portal

Test instructions: Received tolls. If the last charge is less than 3 or cannot be reached, output '? '. Otherwise, the minimum tolls for output to n points

Analysis: The key weights can be negative, if the negative ring is, less than 3 of the constraints are not enough, then in the knowledge that there is a negative ring, the point of the ring is marked, DFS implementation.

`#include <cstdio> #include <cstring> #include <algorithm> #include <queue>using namespace std; const int N = 2e2 + 5;const int E = 1e4 + 5;const int INF = 0x3f3f3f3f;struct Edge{int V, W, NEX;  Edge () {}edge (int v, int W, int. NEX): V (v), W (W), NEX (NEX) {}bool operator < (const Edge &r) Const{return w > R.W;}} Edge[e];int head[n];int d[n];int cnt[n];int a[n];bool vis[n], Vis2[n];int N, M, e;void Init () {memset (head,-1, sizeof (he AD)); e = 0;} void Add_edge (int u, int v, int w) {Edge[e] = Edge (V, W, Head[u]); Head[u] = e++;} void DFS (int u) {vis2[u] = true;for (int i=head[u]; ~i; i=edge[i].nex) {int v = edge[i].v;if (!vis2[v]) {DFS (v)}}} void SPFA (int s) {memset (CNT, 0, sizeof (CNT)), memset (Vis, false, sizeof (VIS)), memset (Vis2, False, sizeof (VIS2)); Memse T (d, INF, sizeof (d));d [s] = 0;cnt[s] = 0;vis[s] = true;queue<int> Que;que.push (s); while (!que.empty ()) {int u = q Ue.front (); Que.pop (); Vis[u] = false;for (int i=head[u]; ~i; i=edge[i].nex) {int v = edGE[I].V, W = edge[i].w;if (Vis2[v]) continue;if (D[v] > D[u] + W) {D[v] = D[u] + w;if (!vis[v]) {Vis[v] = True;que.push (v if (++cnt[v] > N) {DFS (v);}}}}} int cal (int i, int j) {int ret = a[i]-A[j];ret = RET * RET * ret;return ret;} int main (void) {int T, cas = 0;scanf ("%d", &t), while (t--) {init (); scanf ("%d", &n); for (int i=1; i<=n; ++i) {SC ANF ("%d", &a[i]);} scanf ("%d", &m), for (int u, V, i=1; i<=m; ++i) {scanf ("%d%d", &u, &v), Add_edge (U, V, Cal (V, U));}  SPFA (1); int q;scanf ("%d", &q);p rintf ("Case%d:\n", ++cas) and while (q--) {int x;scanf ("%d", &x); if (d[x] = = INF | | D[X] < 3 | | VIS2[X]) puts ("?"); elseprintf ("%d\n", D[x]);}} return 0;}`

SPFA (negative ring) Lightoj 1074 Extended Traffic

Related Keywords:

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.