inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 10000 + 10;
structEdge { int v, w; Edge(int _v = 0, int _w = 0) : v(_v), w(_w) {} };
int n, x, y; std::vector<Edge> G[MAXN];
intspfa(int s){ staticint dist[MAXN]; memset(dist, 0x3f, sizeof dist); staticbool inq[MAXN]; memset(inq, 0, sizeof inq); staticint vis[MAXN]; memset(vis, 0, sizeof vis); std::queue<int> q; q.push(s); dist[s] = 0; inq[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; ++vis[u]; if (vis[u] > n) return-1; for (int i = 0, siz = (int) G[u].size(); i < siz; ++i) { int v = G[u][i].v, w = G[u][i].w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; if (!inq[v]) inq[v] = true, q.push(v); } } } return dist[n] == 0x3f3f3f3f ? -2 : dist[n]; }
intmain(){ int T = read(); while (T --> 0) { n = read(); x = read(); y = read(); for (int i = 1; i <= x; ++i) { int a = read(); int b = read(); int c = read(); // d[b] - d[a] <= c // a -> b : c G[a].push_back(Edge(b, c)); } for (int i = 1; i <= y; ++i) { int a = read(); int b = read(); int c = read(); // d[b] - d[a] >= c // d[a] - d[b] <= -c // b -> a : -c G[b].push_back(Edge(a, -c)); } for (int i = 2; i <= n; ++i) { G[i].push_back(Edge(i - 1, 0)); } for (int i = 1; i <= n; ++i) { G[0].push_back(Edge(i, 0)); } if (spfa(0) == -1) puts("-1"); elseprintf("%d\n", spfa(1)); for (int i = 0; i <= n; ++i) G[i].clear(); } return0; }
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
/** * * 1. 低的往第一个比它高的连正权边 * 2. 按顺序从 i 往 i - 1 连 w = -1 边 * 3. 跑最短路 * */
constint MAXN = 1000 + 10;
structEdge { int v, w; Edge(int _v = 0, int _w = 0) : v(_v), w(_w) {} };
int n, d; int height[MAXN]; std::vector<Edge> G[MAXN];
voidcleanup(){ for (int i = 1; i <= n; ++i) G[i].clear(); }
intspfa(int s, int t){ staticint dist[MAXN]; memset(dist, 0x3f, sizeof dist); staticbool inq[MAXN]; memset(inq, 0, sizeof inq); staticint vis[MAXN]; memset(vis, 0, sizeof vis); std::queue<int> q; dist[s] = 0; q.push(s); inq[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; // printf("u = %d\n", u); ++vis[u]; if (vis[u] > n + 1) return-1; for (int i = 0, siz = (int) G[u].size(); i < siz; ++i) { int v = G[u][i].v, w = G[u][i].w; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; if (!inq[v]) inq[v] = true, q.push(v); } } } return dist[t]; }
intmain(){ int T = read(); for (int cas = 1; cas <= T; ++cas) { n = read(); d = read(); for (int i = 1; i <= n; ++i) height[i] = read(); printf("Case %d: ", cas); int hightest = 0, downh = 0; int lowest = 0x3f3f3f3f, downl = 0; for (int i = 1; i <= n; ++i) { int nowh = 0x3f3f3f3f, down = 0; if (hightest < height[i]) { hightest = height[i]; downh = i; } if (lowest > height[i]) { lowest = height[i]; downl = i; } for (int j = 1; j <= n; ++j) { if (height[j] < nowh && height[j] > height[i]) { nowh = height[j]; down = j; } } // printf("downl: %d %d\n", downl, height[downl]); // printf("downh: %d %d\n", downh, height[downh]); // printf("%d %d\n", down, height[down]);
int fx = i; if (fx > down) std::swap(fx, down); // d[higher] - d[lower] <= d if (down && fx) G[fx].push_back(Edge(down, d)); // d[i] - d[i - 1] >= 1 // d[i - 1] - d[i] <= -1 if (i != 1) G[i].push_back(Edge(i - 1, -1)); } if (downl > downh) std::swap(downl, downh); printf("%d\n", spfa(downl, downh)); cleanup(); } return0; }
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 1000 + 10;
structEdge { int v, w; Edge(int _v = 0, int _w = 0) : v(_v), w(_w) {} };
int n, m, k; std::vector<Edge> G[MAXN << 1];
intspfa(int s, int t){ staticlonglongint dist[MAXN << 1]; for (int i = 0; i <= n + m; ++i) dist[i] = -2147483646; staticbool inq[MAXN << 1]; memset(inq, 0, sizeof inq); staticint vis[MAXN << 1]; memset(vis, 0, sizeof vis); std::queue<int> q; dist[s] = 0; q.push(s); inq[s] = true; while (!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; if (vis[u] > n + m) return-1; ++vis[u]; for (int i = 0, siz = (int) G[u].size(); i < siz; ++i) { int v = G[u][i].v, w = G[u][i].w; if (dist[v] < dist[u] + w) { dist[v] = dist[u] + w; if (!inq[v]) inq[v] = true, q.push(v); } } } return dist[t]; }
intmain(){ int T = read(); while (T --> 0) { n = read(); m = read(); k = read(); for (int i = 1; i <= k; ++i) { int x = read(); int y = read(); int c = read(); // a[i] + b[j] = c // a[i] - (-b[j]) = c // c <= a[i] - (-b[j]) <= c // c <= S[i] - S[n + j] <= c // S[i] - S[n + j] >= c // S[n + j] - S[i] >= -c G[x].push_back(Edge(n + y, -c)); G[n + y].push_back(Edge(x, c)); } for (int i = 1; i <= n + m; ++i) { G[0].push_back(Edge(i, 0)); } puts(spfa(0, n + m) == -1 ? "No" : "Yes"); for (int i = 0; i <= n + m; ++i) { G[i].clear(); } } return0; }