/* Question: there are m links in N locations, and I will go back from 1 to n without repeating the road (I think the same road, taking different directions is a non-repetitive path.) The maximum cost of the minimum flow is equivalent to finding two 1 ~ The shortest path of N ~ 1 traffic 2 fee 0n ~ T traffic 2 cost 0 is that each undirected edge is split into two directed edges, and the directed edges are added to the secondary node respectively (there is also his backend, anti-cost, traffic 0) the reverse side of the backend is the same, but the cost is different from the traffic. Therefore, you cannot use the adjacent matrix, only the */# include <iostream> # include <queue> using namespace STD; const int INF = 0x7fffffff; const int en = 40010; const int VN = 1005; int n, m, mincost, S, T; int pre [VN], vis [VN], DIST [VN]; // The pre used for finding the shortest path is used to save the number of the edge flowing to it. The VIS access flag dist is from the struct edge // edge {int U [En], V [En], f [En], C [En], head [En], Yong, next [En]; // the header of the edge's frontend and backend traffic fee neighbor table Edge () of the next edge of the allocation record // initialize {clear ();} void clear () // clear all objects except head are actively written, so {Yong = 1 does not need to be cleared; memset (Head, 0, sizeof (head);} void Adde (INT Uu, int WW, int cost, int flow) // Add an edge and his backend. {Add (Uu, WW, cost, flow); add (WW, UU,-cost, 0 );} void add (INT Uu, int WW, int cost, int flow) // Add an edge {u [Yong] = Uu; V [Yong] = WW; f [Yong] = flow; C [Yong] = cost; next [Yong] = head [UU]; head [UU] = Yong; Yong ++;} E; int min (int A, int B) {return a <B? A: B;} int spfa () // obtain the shortest path {int I, U, V, p; memset (VIS, 0, sizeof (VIS); memset (PRE, 0, sizeof (DIST); queue <int> q; q. push (s); vis [s] = 1; for (I = 1; I <= T; ++ I) Dist [I] = inf; dist [s] = 0; while (! Q. empty () {u = Q. front (); For (P = E. head [u]; P = E. next [p]) {v = E. V [p]; If (E. f [p] & Dist [v]> Dist [u] + E. c [p]) {Dist [v] = DIST [u] + E. c [p]; Pre [v] = P; // record the number of the edge of the flow direction V to facilitate the streaming if (! Vis [v]) {q. push (V); vis [v] = 1 ;}} Q. pop (); vis [u] = 0;} If (Dist [T] = inf) return 0; return 1;} void addf () // press the stream {int I, j; I = pre [T]; while (I! = 0) {if (I % 2) // read other people's code before seeking the return of edge I, I ^ 1 is used directly. The result is always wrong. People started to use it from 0. I started to use it from 1. So here we handle J = I + 1; else J = I-1; E. f [I] --; E. f [J] ++; mincost + = E. c [I]; I = pre [E. U [I] ;}return ;}int main () {int I, U, V, C; CIN >>n> m; mincost = 0; S = 0; t = n + 1; for (I = 1; I <= m; ++ I) {CIN> U> V> C; E. ADDE (u, v, C, 1); E. ADDE (v, U, C, 1);} e. ADDE (s, 1, 0, 2); E. ADDE (n, T, 0, 2); While (spfa () addf (); cout <mincost <Endl; return 0 ;}