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
| #include<bits/stdc++.h> #define x first #define y second #define ok cout << "ok" << endl; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const long double PI = acos(-1.0); const int INF = 0x3f3f3f3f; const ll LINF = 9223372036854775807; const double Eps = 1e-7; const int N = 1e5+9;
int head[2][1009], tot;
struct Edge { int v, c, next; Edge () {} Edge (int v, int c, int next): v(v), c(c), next(next) {} }edge[2 * N];
void addedge(int u, int v, int c, int ty) { edge[tot] = Edge{v, c, head[ty][u]}; head[ty][u] = tot++; }
void init() { memset(head, -1, sizeof head); tot = 0; }
const int MAX_V=10009; int d[2][MAX_V];
void dijkstra(int s, int ty) { priority_queue<pii, vector<pii>, greater<pii> > que; d[ty][s] = 0; que.push(pii(0, s)); while(!que.empty()) { pii p = que.top(); que.pop(); int u = p.second; if(d[ty][u] < p.first) continue; for(int i = head[ty][u]; ~i; i = edge[i].next) { Edge e = edge[i]; if(d[ty][e.v] > d[ty][u] + e.c) { d[ty][e.v] = d[ty][u] + e.c; que.push(pii(d[ty][e.v], e.v)); } } } }
const int MAXN = 1009; const int MAXM = 200009; struct Edge1{ int to,next,cap,flow; }edge1[MAXM]; int tol; int head1[MAXN]; int gap[MAXN],dep[MAXN],cur[MAXN]; void init1(){ tol = 0; memset(head1,-1,sizeof(head1)); } void addedge1(int u,int v,int w,int rw = 0){ edge1[tol].to = v; edge1[tol].cap = w; edge1[tol].flow = 0; edge1[tol].next = head1[u]; head1[u] = tol++; edge1[tol].to = u; edge1[tol].cap = rw; edge1[tol].flow = 0; edge1[tol].next = head1[v]; head1[v] = tol++; } int Q[MAXN]; void BFS(int start,int end){ memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); gap[0] = 1; int front = 0, rear = 0; dep[end] = 0; Q[rear++] = end; while(front != rear){ int u = Q[front++]; for(int i = head1[u]; i != -1; i = edge1[i].next){ int v = edge1[i].to; if(dep[v] != -1)continue; Q[rear++] = v; dep[v] = dep[u] + 1; gap[dep[v]]++; } } } int S[MAXN]; int sap(int start,int end,int N){ BFS(start,end); memcpy(cur,head1,sizeof(head1)); int top = 0; int u = start; int ans = 0; while(dep[start] < N){ if(u == end){ int Min = INF; int inser; for(int i = 0;i < top;i++) if(Min > edge1[S[i]].cap - edge1[S[i]].flow){ Min = edge1[S[i]].cap - edge1[S[i]].flow; inser = i; } for(int i = 0;i < top;i++){ edge1[S[i]].flow += Min; edge1[S[i]^1].flow -= Min; } ans += Min; top = inser; u = edge1[S[top]^1].to; continue; } bool flag = false; int v; for(int i = cur[u]; i != -1; i = edge1[i].next){ v = edge1[i].to; if(edge1[i].cap - edge1[i].flow && dep[v]+1 == dep[u]){ flag = true; cur[u] = i; break; } } if(flag){ S[top++] = cur[u]; u = v; continue; } int Min = N; for(int i = head1[u]; i != -1; i = edge1[i].next) if(edge1[i].cap - edge1[i].flow && dep[edge1[i].to] < Min) { Min = dep[edge1[i].to]; cur[u] = i; } gap[dep[u]]--; if(!gap[dep[u]])return ans; dep[u] = Min + 1; gap[dep[u]]++; if(u != start)u = edge1[S[--top]^1].to; } return ans; }
int T, n, m, u, v, c[N]; pii a[N];
int main(void) { if(fopen("in", "r")!=NULL) {freopen("in", "r", stdin); freopen("out", "w", stdout);} scanf("%d", &T); while(T--) { init(); scanf("%d%d", &n, &m); for(int i = 0; i < m; i++) { scanf("%d%d%d", &u, &v, c + i); a[i] = pii(u, v); addedge(u, v, c[i], 0); addedge(v, u, c[i], 1); } scanf("%d%d", &u, &v); memset(d, INF, sizeof d); dijkstra(u, 0); dijkstra(v, 1); init1(); int start = 0, en = v; for(int i = 0; i < m; i++) { if(d[0][a[i].x] + c[i] + d[1][a[i].y] == d[0][v]) { addedge1(a[i].x, a[i].y, 1); } } addedge1(start, u, INF); printf("%d\n", sap(start, en, n + 1)); }
return 0; }
|