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