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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF(0x3f3f3f3f3f3f3f); const double ESP(1e-9); const ll mod(998244353); #define per(i, a, b) for(int i=a; i<b; i++) #define rep(i, a, b) for(int i=b-1; i>=a; i--) #define mem(a, b) memset(a, b, sizeof(a)) ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
const int MAXN(1e4 + 7); int T, n, m; struct spfa { int head1[MAXN], nxt1[2*MAXN]; int to[2*MAXN]; ll val[2*MAXN]; ll len[MAXN]; int vis[MAXN]; queue<int> Q; int cnt1; spfa(int n=0, int m=0) { per(i, 1, n+1) { head1[i]=0; } per(i, 1, m+1) { nxt1[i]=0; } cnt1 = 1; } void init(int n=0, int m=0) { per(i, 1, n+1) { head1[i]=0; } per(i, 1, m+1) { nxt1[i]=0; } cnt1 = 1; } void add(int u, int v, int w) { nxt1[cnt1] = head1[u]; head1[u] = cnt1; to[cnt1] = v; val[cnt1++] = w; } void SPFA(int s) { per(i, 1, n+1) { len[i] = INF; vis[i] = 0; } vis[s] = 1; len[s] = 0; Q.push(s);
while (!Q.empty()) { s = Q.front(); Q.pop(); vis[s] = 0; for(int it=head1[s]; it; it=nxt1[it]) { if(len[to[it]]>len[s]+val[it]) { len[to[it]]=len[s]+val[it]; if(!vis[to[it]]) { Q.push(to[it]); vis[to[it]] = 1; } } } } } }SS;
class Dinic { public: int N, S, T; vector<int> head; vector<int> cur; vector<int> depth; vector<int> nxt; vector<int> To; vector<ll> Val; ll outt;
public: Dinic(int Nn=0, int Ss=0, int Tt=0): N(Nn), S(Ss), T(Tt) { head.assign(N+1, 0); depth.resize(N+1); Val.resize(2); To.resize(2); nxt.resize(2); } void init(int Nn=0, int Ss=0, int Tt=0) { N=Nn, S=Ss, T=Tt; head.assign(N+1, 0); depth.resize(N+1); Val.resize(2); To.resize(2); nxt.resize(2); } void _Add(int u, int v, ll w) { nxt.push_back(head[u]); head[u] = To.size(); To.push_back(v); Val.push_back(w); } void addEdge(int u, int v, ll w) { _Add(u, v, w); _Add(v, u, 0); } int bfs() { queue<int> qq; qq.push(S); vector<int> marked; marked.assign(N+1, 0); marked[S] = 1; marked[T] = depth[S] = 0; int tmp;
while(!qq.empty()) { tmp = qq.front(); qq.pop(); for(int i=head[tmp]; i; i=nxt[i]) { if(Val[i] && !marked[To[i]]) { marked[To[i]] = 1; depth[To[i]] = depth[tmp]+1; qq.push(To[i]); } } } if(marked[T]) { return 1; } return 0; } int dfs(int pos, ll val) { if(pos==T) { return val; } for(int& i=cur[pos]; i; i=nxt[i]) { if(Val[i] && depth[To[i]]==depth[pos]+1) { ll valu = dfs(To[i], min(val, Val[i])); if(valu) { Val[i] -= valu; Val[i^1] += valu; return valu; } } } return 0; } void dinic() { outt = 0; while(bfs()) { cur = head; ll val; while((val=dfs(S, INF))) { outt += val; } } } }DD;
int marked[MAXN]; void dfs(int pos) { marked[pos] = 1; for(int it=SS.head1[pos]; it; it=SS.nxt1[it]) { if(SS.len[SS.to[it]]==SS.len[pos]+SS.val[it]) { DD.addEdge(pos, SS.to[it], SS.val[it]); if(!marked[SS.to[it]]) { dfs(SS.to[it]); } } } }
int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> T; while(T--) { cin >> n >> m; SS.init(n, m); int u, v, w; per(i, 0, m) { cin >> u >> v >> w; SS.add(u, v, w); } SS.SPFA(1); DD.init(n, 1, n); per(i, 1, n+1) { marked[i]=0; } dfs(1); DD.dinic(); cout << DD.outt << endl; }
return 0; }
|