【链接】 我是链接,点我呀:)
【题意】
【题解】
模板题【代码】
const int INF = 1e9;
const int maxn = 1e3 + 7;
const int maxv = 2e4 + 5;
int head[maxv], cur[maxv], d[maxv], s, t, k, k1, sum, book[maxn];
int n, m;
void init()
{
k = 0;
memset(head, -1, sizeof(head));
}
struct node
{
int v, w, next;
}edge[maxv];
void addEdge(int u, int v, int w)
{
edge[k].v = v;
edge[k].w = w;
edge[k].next = head[u];
head[u] = k++;
edge[k].v = u;
edge[k].w = 0;
edge[k].next = head[v];
head[v] = k++;
}
int Q[maxv];
int bfs()
{
memset(d, 0, sizeof(d));
d[s] = 1;
int frnt = 0, rear = 0;
Q[rear++] = s;
while(frnt != rear)
{
int u = Q[frnt++];
if(u == t) return 1;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int to = edge[i].v, w = edge[i].w;
if(w && d[to] == 0)
{
d[to] = d[u] + 1;
if(to == t) return 1;
Q[rear++] = to;
}
}
}
return 0;
}
int dfs(int u, int maxflow)
{
if(u == t || !maxflow) return maxflow;
int ret = 0;
for(int& i = cur[u]; i != -1; i = edge[i].next)
{
int to = edge[i].v, w = edge[i].w;
if(w && d[to] == d[u]+1)
{
int f = dfs(to, min(maxflow-ret, w));
edge[i].w -= f;
edge[i^1].w += f;
ret += f;
if(ret == maxflow) return ret;
}
}
return ret;
}
int Dinic()
{
int ans = 0;
while(bfs() == 1)
{
memcpy(cur, head, sizeof(head));
ans += dfs(s, INF);
}
return ans;
}
int main()
{
int _;
scanf("%d", &_);
while(_--)
{
int x, y, z;
init();
scanf("%d%d", &n, &m);
scanf("%d%d", &s, &t);
for(int i = 0; i < m; i++)
{
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, z*(m+1)+1);
}
printf("%d
", Dinic()%(m+1));
}
return 0;
}