1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include <cstdio> #include <string> #include <cstring> #include <vector> #include <algorithm> #include <map> #include <set> #include <functional> #include <queue> #include <cmath> #include <cstdlib> using namespace std;
typedef long long ll;
typedef pair<int , int > pii; typedef unsigned long long ull;
namespace FastIO { template<typename T> inline T read(T& x) { x = 0; int f = 1; char ch; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x *= f; return x; } template<typename T, typename... Args> inline void read(T& x, Args &...x_) { read(x); read(x_...); return; } inline ll read() { ll x; read(x); return x; } }; using namespace FastIO;
template<int N, int M> class Graph { private : struct Edge { int fr, to, nt; ll wt, fl, cp; Edge() {} Edge(int fr, int to, int nt, ll wt, ll cp, ll fl) : fr(fr), to(to), nt(nt), wt(wt), cp(cp), fl(fl) {} }e[M]; int hd[N], cnte; public : inline void clear() { memset(hd, 0, sizeof(hd)), cnte = 0; } inline void AddEdge(int u, int v, ll w = 0, ll c = 0, ll f = 0) { e[++cnte] = Edge(u, v, hd[u], w, c, f); hd[u] = cnte; } inline int head(int u) { return hd[u]; } inline int fr(int u) { return e[u].fr; } inline int nt(int u) { return e[u].nt; } inline int to(int u) { return e[u].to; } inline ll wt(int u) { return e[u].wt; } inline ll& flw(int u) { return e[u].fl; } inline ll& cap(int u) { return e[u].cp; } };
const int MaxV = 2010; const int MaxE = 40010; const ll INF = 1e18;
int n, m; Graph< MaxV, MaxE >G;
inline void Input() { read(n, m); int u, v, c; for(int i = 1; i <= m; i++) { read(u, v, c); G.AddEdge(u, v, c, 1); G.AddEdge(v, u,-c, 0); G.AddEdge(v, u, c, 1); G.AddEdge(u, v,-c, 0); } }
ll dis[MaxV]; int path[MaxV], vis[MaxV];
inline bool SPFA(int S, int T, int nodenum) { queue<int >q; memset(vis, 0, sizeof(vis)); memset(path,-1,sizeof(path)); for(int i = 0; i <= nodenum; i++) dis[i] = INF; vis[S] = 1, dis[S] = 0; q.push(S); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i = G.head(u); i; i = G.nt(i)) { int v = G.to(i); if(G.cap(i) && dis[v] > dis[u] + G.wt(i)) { dis[v] = dis[u] + G.wt(i); path[v] = i; if(!vis[v]) { q.push(v); vis[v] = 1; } } } } if(dis[T] == INF) return false; return true; }
inline pair<ll, ll> mcmf(int S, int T, int nodenum) { ll flow = 0, minflow, mincost = 0; while(SPFA(S, T, nodenum)) { minflow = INF + 1; for(int i = path[T]; i != -1; i = path[G.fr(i)]) { minflow = min(minflow, G.cap(i)); } flow += minflow; for(int i = path[T]; i != -1; i = path[G.fr(i)]) { G.cap(i) -= minflow; G.cap(((i-1)^1)+1) += minflow; } mincost += dis[T] * minflow; } return make_pair(mincost, flow); }
inline void Work() { G.AddEdge(n + 1, 1, 0, 2); G.AddEdge(1, n + 1, 0, 0); G.AddEdge(n, n + 2, 0, 2); G.AddEdge(n + 2, n, 0, 0); printf("%lld\n", mcmf(n + 1, n + 2, n + 2).first); }
int main() { int T = 1; while(T--) { Input(); Work(); } return 0; }
|