The problem-solving report is about the upstream and downstream expense streams. I use the upstream and downstream methods here. The same is true without going through the upstream and downstream services. I will change the cost of passing through the edge to-inf, then, when the minimum fee flow is obtained, the program will pass through this edge and also play the role of limiting the lower bound, so as to better understand ~
# Pragma comment (linker, "/STACK: 102400000,102400000 ")
# Include <iostream>
# Include <vector>
# Include <algorithm>
# Include <cstdio>
# Include <queue>
# Include <stack>
# Include <string>
# Include <map>
# Include <set>
# Include <cmath>
# Include <cassert>
# Include <cstring>
# Include <iomanip>
Using namespace std;
# Ifdef _ WIN32
# Define i64 _ int64
# Define out64 "% I64d \ n"
# Define in64 "% I64d"
# Else
# Define i64 long
# Define out64 "% lld \ n"
# Define in64 "% lld"
# Endif
/*********** For topcoder by zz1215 *******************/
# Define FOR (I, a, B) for (int I = (a); I <= (B); I ++)
# Define FF (I, a) for (int I = 0; I <(a); I ++)
# Define FFD (I, a, B) for (int I = (a); I >= (B); I --)
# Define S64 (a) scanf (in64, &)
# Define SS (a) scanf ("% d", &)
# Define LL (a) <1)
# Define RR (a) <1) + 1)
# Define pb push_back
# Define pf push_front
# Define X first
# Define Y second
# Define CL (Q) while (! Q. empty () Q. pop ()
# Define MM (name, what) memset (name, what, sizeof (name ))
# Define MC (a, B) memcpy (a, B, sizeof (B ))
# Define MAX (a, B) (a)> (B )? (A) (B ))
# Define MIN (a, B) (a) <(B )? (A) (B ))
# Define read freopen ("in.txt", "r", stdin)
# Define write freopen ("out.txt", "w", stdout)
Const int inf = 0x3f3f3f;
Const i64 inf64 = 0x3f3f3f3f3f3f3fll;
Const double oo = 10e9;
Const double eps = 10e-9;
Const double pi = acos (-1.0 );
Const int maxn = 222;
Const int add = 111;
Const int head = 220;
Const int end = 221;
Struct zz
{
Int from;
Int;
Int c;
Int cost;
Int id;
} Zx;
Vector <zz> g [maxn];
Int n, m, k;
Int d [maxn] [maxn];
Int way [maxn];
Bool inq [maxn];
Int back [maxn];
Void floyd ()
{
For (int k = 0; k <= n; k ++)
{
For (int I = 0; I <= n; I ++)
{
If (d [I] [k] = inf) continue;
For (int j = 0; j <= n; j ++)
{
If (d [k] [j] = inf) continue;
D [I] [j] = min (d [I] [j], d [I] [k] + d [k] [j]);
}
}
}
Return;
}
Void link (int now, int to, int c, int cost, int bc = 0)
{
Zx. from = now; zx. to = to; zx. c = c; zx. cost = cost; zx. id = g [zx. to]. size ();
G [zx. from]. pb (zx );
Swap (zx. from, zx. to); zx. c = bc; zx. cost =-cost; zx. id = g [zx. to]. size ()-1;
G [zx. from]. pb (zx );
Return;
}
Bool spfa ()
{
For (int I = 0; I <maxn; I ++) way [I] = inf;
MM (back,-1 );
Queue <int> q;
MM (inq, false );
Inq [head] = true;
Q. push (head );
Way [head] = 0;
Int now, to, temp;
While (! Q. empty ())
{
Now = q. front ();
Q. pop ();
For (int I = 0; I <g [now]. size (); I ++)
{
To = g [now] [I].;
If (g [now] [I]. c> 0)
{
Temp = way [now] + g [now] [I]. cost;
If (temp <way [to])
{
Way [to] = temp;
Back [to] = g [now] [I]. id;
If (! Inq [to])
{
Inq [to] = true;
Q. push ();
}
}
}
}
Inq [now] = false;
}
Return way [end]! = Inf;
}
Int dfs (int flow = inf, int to = end)
{
If (to = head) return flow;
Int now = g [to] [back [to].;
Int id = g [to] [back [to]. id;
Int temp = dfs (min (g [now] [id]. c, flow), now );
G [now] [id]. c-= temp;
G [to] [back [to]. c + = temp;
Return temp;
}
Int ek ()
{
Int ans = 0;
For (int I = 0; I <n; I ++)
{
Ans + = d [I] [I + 1];
}
Ans + = d [0] [n];
While (spfa ())
{
Ans + = dfs () * way [end];
}
Return ans;
}
Int main ()
{
While (cin> n> m> k)
{
If (! M &&! N &&! K) return 0;
For (int I = 0; I <maxn; I ++)
{
G [I]. clear ();
}
For (int I = 0; I <= n; I ++)
{
For (int j = 0; j <= n; j ++)
{
D [I] [j] = inf;
}
}
For (int I = 0; I <= n; I ++)
{
D [I] [I] = 0;
}
Int now, to, len;
For (int I = 1; I <= m; I ++)
{
Cin> now> to> len;
D [now] [to] = d [to] [now] = min (d [now] [to], len );
}
Floyd ();
Link (0, 0, d [0] [1], 1 );
For (int I = 2; I <= n; I ++)
{
Link (0, I, 1, d [0] [I]);
}
For (int I = 1; I <n; I ++)
{
Link (I + add, end, 1, d [I] [0]);
}
Link (n + add, end, 0, d [n] [0], 1 );
Link (0, end, inf, 0 );
Link (head, 0, K-1 );
For (int I = 1; I <= n; I ++)
{
Link (I + add, I + 1, 0, d [I] [I + 1], 1 );
For (int j = I + 2; j <= n; j ++)
{
Link (I + add, j, 1, d [I] [j]);
}
}
Cout <ek () <endl;
}
Return 0;
}