HDU - 3549
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 = 20;
const int maxM = 1003;
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()
{
int cas = 0;
int TAT; TAT = read();
while(TAT -- )
{
init();
n = read(); m = read();
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("Case %d: %d\n", ++cas, dinic_maxFlow());
}
return 0;
}
Dinic当前弧优化!快了一百多ms!
一个结点可能发出多条边,但如果一条边已经被增广过,并且已经达到满流,也就是增广路会直接从这里断掉,那么我们再走这条边就没有任何意义。所以我们直接修改cur[ ]的值为下一条可能可行的边,也就是把不能再经过的边直接滤掉!
#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 = 20;
const int maxM = 1003;
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 cur[maxN], 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 = cur[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())
{
for(int i = 0; i <= n; ++ i)
cur[i] = head[i];
maxFlow += dfs(s, INF);
}
return maxFlow;
}
int main()
{
int cas = 0;
int TAT; TAT = read();
while(TAT -- )
{
init();
n = read(); m = read();
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("Case %d: %d\n", ++cas, dinic_maxFlow());
}
return 0;
}
/*
10
4 5
1 2 1
1 3 1
2 3 1
2 4 1
3 4 1
*/
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 = 200;
const int maxM = 10003;
int n, m, s, t;
struct Pre{
int last, 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 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(pre, -1, sizeof(pre));
memset(visited, false, sizeof(visited));
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].last)
{
edge[pre[u].Eid].w -= flow;
edge[pre[u].Eid ^ 1].w += flow;
u = pre[u].last;
}
}
int EK_maxFlow()
{
int maxFlow = 0;
while(bfs())
{
int newFlow = INF;
for(int i = t; i != s; i = pre[i].last)
newFlow = min(newFlow, edge[pre[i].Eid].w);
update_residual_network(t, newFlow);
maxFlow += newFlow;
}
return maxFlow;
}
int main()
{
int cas = 0;
int TAT; TAT = read();
while(TAT -- )
{
init();
n = read(); m = read();
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("Case %d: %d\n", ++cas, EK_maxFlow());
}
return 0;
}