http://acm.hdu.edu.cn/showproblem.php?pid=3549
题意:典型最大流模型。
思路:详见我的上一篇,几乎一样的题,最大流割草游戏= =。
Ekarp:
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int cap[N][N], pre[N];
bool vis[N];
int n, m;
int EKarp()
{
int num = 0;//最大流
while(1)
{
queue<int>que;
memset(vis, false, sizeof(vis));
memset(pre, 0, sizeof(pre));
que.push(1);
vis[1] = true;
while(!que.empty())
{
int cnt = que.front();
que.pop();
if(cnt == n) break;
for(int i = 1; i <= n; i++)
{
if(!vis[i] && cap[cnt][i]>0)
{
vis[i] = true;
que.push(i);
pre[i] = cnt;
}
}
}
if(vis[n] == false) break;
int minf = INF;
for(int i = n; i != 1; i = pre[i])
{
minf = min(minf, cap[pre[i]][i]);
}
for(int i = n; i != 1; i = pre[i])
{
cap[pre[i]][i] -= minf;
cap[i][pre[i]] += minf;
}
num+=minf;
}
return num;
}
int main()
{
// freopen("in.txt", "r", stdin);
int t0, s, t, w, Case = 1;
scanf("%d", &t0);
while(t0--)
{
scanf("%d%d", &n, &m);
memset(cap, 0, sizeof(cap));
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &s, &t, &w);
cap[s][t] += w;
}
int ans = EKarp();
printf("Case %d: %d\n", Case++, ans);
}
return 0;
}
Dinic:(厉害啊,时间从2137ms变成了156ms。。)
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long LL;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int head[N], dis[N], cur[N];
bool vis[N];
int n, m, cnt;
struct Edge
{
int to, cap, flow, next;
}edge[N*2];
void add(int u, int v, int w)
{
edge[cnt] = (struct Edge){v, w, 0, head[u]};
head[u] = cnt++;
edge[cnt] = (struct Edge){u, 0, 0, head[v]};
head[v] = cnt++;
}
bool bfs(int start, int endd)
{
memset(dis, -1, sizeof(dis));
memset(vis, false, sizeof(vis));
queue<int>que;
dis[start] = 0;
vis[start] = true;
que.push(start);
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.flow<E.cap)
{
dis[E.to] = dis[u]+1;
vis[E.to] = true;
if(E.to == endd) return true;
que.push(E.to);
}
}
}
return false;
}
int dfs(int x, int res, int endd)
{
if(x==endd || res==0) return res;
int flow = 0, f;
for(int& i = cur[x]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(dis[E.to]==dis[x]+1)
{
f = dfs(E.to, min(res, E.cap-E.flow), endd);
if(f>0)
{
edge[i].flow+=f;
edge[i^1].flow-=f;
flow+=f;
res-=f;
if(res == 0) break;
}
}
}
return flow;
}
int max_flow(int start, int endd)
{
int flow = 0;
while(bfs(start, endd))
{
memcpy(cur, head, sizeof(head));
flow += dfs(start, INF, endd);
}
return flow;
}
void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
int main()
{
// freopen("in.txt", "r", stdin);
int s, t, w, t0, Case = 1;
scanf("%d", &t0);
while(t0--)
{
scanf("%d%d", &n, &m);
init();
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &s, &t, &w);
add(s, t, w);
}
int ans = max_flow(1, n);
printf("Case %d: %d\n", Case++, ans);
}
return 0;
}