【luogu P2936 [USACO09JAN]全流Total Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P2936
菜
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000;
const int inf = 1e9;
int n, m, s, t, maxflow, deep[maxn];
struct edge{
int next, to, len;
}e[maxn<<2];
int head[maxn], cnt = -1, cur[maxn];
queue<int> q;
void add(int u, int v, int w, int flag)
{
e[++cnt].next = head[u];
e[cnt].to = v;
if(flag) e[cnt].len = w;
head[u] = cnt;
}
bool bfs(int s, int t)
{
memset(deep, 0x7f, sizeof(deep));
while(!q.empty()) q.pop();
for(int i = 1; i <= n; i++) cur[i] = head[i];
q.push(s); deep[s] = 0;
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] > inf && e[i].len)
{
deep[e[i].to] = deep[now] + 1;
q.push(e[i].to);
}
}
}
if(deep[t] < inf) return true;
else return false;
}
int dfs(int now, int t, int limit)
{
if(!limit || now == t) return limit;
int flow = 0, f;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] == deep[now] + 1 && (f = dfs(e[i].to, t, min(e[i].len, limit))))
{
flow += f;
limit -= f;
e[i].len -= f;
e[i^1].len += f;
if(!limit) break;
}
}
return flow;
}
void Dinic(int s, int t)
{
while(bfs(s, t))
maxflow += dfs(s, t, inf);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d",&m);
s = 1, t = 26;
for(int i = 1; i <= m; i++)
{
char a, b; int u, v, w;
cin>>a>>b>>w;
u = a-'A'+1;
v = b-'A'+1;
//cout<<u<<" "<<v<<endl;
add(u, v, w, 1);
add(v, u, w, 0);
}
Dinic(s, t);
printf("%d\n",maxflow);
return 0;
}
隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。
隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。