POJ - 1273
Dinic算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
inline int read()
{
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int maxN = 202;
const int maxM = 202;
int n, m, s, t;
int deep[maxN];
struct EDGE{
int adj, to, w;
EDGE(int a = -1, int b = 0, int c = 0): adj(a), to(b), w(c) {}
}edge[maxM << 1];
int head[maxN], cnt;
void add_edge(int u, int v, int w)
{
edge[cnt] = EDGE(head[u], v, w);
head[u] = cnt ++ ;
}
void init()
{
memset(head, -1, sizeof(head));
cnt = 0;
}
bool bfs()
{
memset(deep, 0, sizeof(deep));
queue<int>q;
q.push(s); deep[s] = 1;
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].adj)
{
int v = edge[i].to;
if(!deep[v] && edge[i].w)
{
deep[v] = deep[u] + 1;
q.push(v);
}
}
}
return deep[t];
}
int dfs(int u, int flow)
{
if(u == t) return flow;
for(int i = head[u]; ~i; i = edge[i].adj)
{
int v = edge[i].to;
if(deep[v] == deep[u] + 1 && edge[i].w)
{
if(int newFlow = dfs(v, min(flow, edge[i].w)))
{
edge[i].w -= newFlow;
edge[i ^ 1].w += newFlow;
return newFlow;
}
}
}
return 0;
}
int dinic_maxFlow()
{
int maxFlow = 0;
while(bfs())
{
if(int newFlow = dfs(s, INF))
maxFlow += newFlow;
}
return maxFlow;
}
int main()
{
while(~scanf("%d%d", &m, &n))
{
init();
s = 1; t = n;
for(int i = 0; i < m; ++ i )
{
int u, v, w;
u = read(); v = read(); w = read();
add_edge(u, v, w);
add_edge(v, u, 0);
}
printf("%d\n", dinic_maxFlow());
}
return 0;
}
EK算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
inline int read()
{
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int maxN = 202;
const int maxM = 202;
int n, m, s, t;
struct Pre{
int pre, Eid;
}pre[maxN];
bool visited[maxN];
struct EDGE{
int adj, to, w;
EDGE(int a = -1, int b = 0, int c = 0): adj(a), to(b), w(c) {}
}edge[maxM << 1];
int head[maxN], cnt;
void init()
{
memset(head, -1, sizeof(head));
cnt = 0;
}
void add_edge(int u, int v, int w)
{
edge[cnt] = EDGE(head[u], v, w);
head[u] = cnt ++ ;
}
bool bfs()
{
memset(visited, false, sizeof(visited));
memset(pre, -1, sizeof(pre));
queue<int>q;
q.push(s); visited[s] = true;
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = head[u]; ~i; i = edge[i].adj)
{
int v = edge[i].to;
if(!visited[v] && edge[i].w)
{
pre[v] = Pre{u, i};
if(v == t) return true;
q.push(v); visited[v] = true;
}
}
}
return false;
}
void update_residual_network(int u, int flow)
{
while(~pre[u].pre)
{
edge[pre[u].Eid].w -= flow;
edge[pre[u].Eid ^ 1].w += flow;
u = pre[u].pre;
}
}
int EK_maxFlow()
{
int maxFlow = 0;
while(bfs())
{
int newFlow = INF;
for(int i = t; i != s; i = pre[i].pre)
newFlow = min(newFlow, edge[pre[i].Eid].w);
update_residual_network(t, newFlow);
maxFlow += newFlow;
}
return maxFlow;
}
int main()
{
while(~scanf("%d%d", &m, &n))
{
init();
s = 1; t = n;
for(int i = 0; i < m; ++ i )
{
int u, v, w;
u = read(); v = read(); w = read();
add_edge(u, v, w);
add_edge(v, u, 0);
}
printf("%d\n", EK_maxFlow());
}
return 0;
}
Clover是不是可以拆成c+lover,可 爱人~hhhhhhh
And, i have another question:
Where is my four-leaf clover?【写在最后你们看不见看不见看不见】