# Ambush UVALive - 8267

xiaoxiao2021-03-01  10

1.前几遍T了是因为有一行memset的代码我看着他长就把他和一行短的代码交换了位置，交换了位置。。。这个可不能交换。。。

2.有一遍WA了是因为这是一幅无向图一条边的正反容量都是ceil（敌军总数量 * 1.0 / 3)

#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1000 + 10; const int INF = 0x3f3f3f3f; bool vis[maxn]; int d[maxn]; int cur[maxn]; int n,m,s,t,c,a,b; struct edge { int from,to,cap,flow; edge(int a,int b,int c,int d):from(a),to(b),cap(c),flow(d) {} }; vector<int> G[maxn]; vector<edge> edges; void init() { for(int i=1; i<=n; i++) G[i].clear(); edges.clear(); } void addedge(int u,int v) { edges.push_back(edge(u,v,c,0)); edges.push_back(edge(v,u,c,0)); int t = edges.size(); G[u].push_back(t-2); G[v].push_back(t-1); } bool bfs() { queue<int> q; q.push(s); memset(vis,0,sizeof(vis)); d[s] = 0,vis[s] = 1; while(!q.empty()) { int x = q.front(); q.pop(); for(int i=0; i<(int)G[x].size(); i++) { edge e = edges[G[x][i]]; if(!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[x] + 1; q.push(e.to); } } } return vis[t]; } int dfs(int x,int a) { if(x == t || a == 0) return a; int flow = 0, f; for(int &i = cur[x]; i<(int)G[x].size(); i++) { edge &e = edges[G[x][i]]; if(d[x] + 1 == d[e.to] && (f = dfs(e.to,min(a,e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i]^1].flow -= f; a -= f; flow += f; if(a == 0) break; } } return flow; } int max_flow() { int flow = 0; while(bfs()) { memset(cur,0,sizeof(cur)); flow += dfs(s,INF); } return flow; } int main() { int k; scanf("%d",&k); while(k--) { scanf("%d%d%d%d%d",&n,&m,&s,&t,&c); init(); c = ceil(c * 1.0 / 3); for(int i=1; i<=m; i++) { scanf("%d%d",&a,&b); addedge(a,b); } int ans = max_flow(); printf("%d\n",ans); } return 0; }