Topic Link: Click to open the link
Test instructions
Given n points M Bar has a forward edge constant C
The following M-line gives the edge.
The Benquan of the least-modified edge makes the shortest short-circuit length of the 1->n exactly C (the input guarantees that the 1->n has the shortest and shortest-path value >c)
Ideas:
Because it is a DAG, and C is not big, it should be the DP on the DAG, reverse build the edge and then BFS out.
DP[I][J] Represents the minimum number of edges that the point I needs to modify when the distance from the endpoint is exactly J.
The initial state is dp[n][0] = 0, the other is INF.
Transfer from U to V, if the edge is not modified:
1, Dp[v][j+edge.dis] = min (dp[u][j]);
If you modify this edge right, the equation is:
2, Dp[v][j] = min (dp[u][J-?] + 1);
Obviously 2 of the transfer complexity is c*c, so it's too big.
In fact, if the shortest path is modified to exactly C and modified to <=c the answer is the same, because c the smaller the answer should be greater, so we convert test instructions into <=c,
Then ans = min (dp[n][0->c])
So 2 of the equation can be changed to dp[v][j] = min (dp[u][j]+1);
O (1) transfer.
Complexity of O (m*c)
Over.
#include <stdio.h> #include <string.h> #include <set> #include <map> #include <algorith m> #include <iostream> #include <vector> #include <string> #include <queue> #include <CMA th> template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c ! = '-' && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); sgn = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} using namespace Std;const int inf = 1e8;const int N = 105;const int M = 100010;struct edge{int from, to, DIS, NEX;} Edge[1005];int Head[n], edgenum;void Add (int u, int v, int dis) {Edge E = {u, V, dis, head[u]};edge[edgenum] = E;head[u] = edgenum++;} void Init () {memset (head,-1, sizeof HEAD); Edgenum = 0; }int N, M, C;int Dp[n][m], vis[n];void BFS () {Queue<int>q;q.push (n);dp [n][0] = 0;while (!q.empty ()) {int u = Q.front () ; Q.pop (); Vis[u] = 0;for (int i = head[u]; ~i; i = edge[i].nex) {int v = edge[i].to;bool OK = false;for (int j = 0; J <= C; j + +) {I F (dp[u][j] = = inf) continue;if (Dp[v][j] > Dp[u][j] + 1) {Dp[v][j] = Dp[u][j] + 1; ok = true;} if (j + Edge[i].dis <= C && dp[v][j + Edge[i].dis] > Dp[u][j]) {dp[v][j + Edge[i].dis] = dp[u][j]; ok = true; }}if (ok && vis[v] = = 0) Vis[v] = 1, Q.push (v);}}} int main () {int u, V, d;while (cin>>n>>m>>c, n+m+c) {init (); memset (Vis, 0, sizeof Vis); for (int i = 1; I & lt;= N; i++) for (int j = 0; J <= C; j + +) Dp[i][j] = Inf;while (m--) {rd (U); Rd (v); Rd (d); add (V, U, d);} BFS (); int ans = inf;for (int i = 0; I <= C; i++) ans = min (ans, dp[1][i]);p t (ans); Puts ("");} return 0;} /*3 3 31 2 101 2 52 3 34 5 31 2 101 2 52 3 33 4 14 2 64 5 11 2 101 2 52 3 33 4 14 2 64 5 81 2 101 2 52 3 33 4 14 2 64 501 2 101 2 52 3 33 4 14 2 6*/
Aizu 1311Test case tweaking DP on DAG