您的位置:首页 > 其它

hdu 3549 Flow Problem (最大流—EK—Dinic)

2016-09-24 09:17 330 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549


Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 13526    Accepted Submission(s): 6465

Problem Description

Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

 

Input

The first line of input contains an integer T, denoting the number of test cases.

For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)

Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

 

Output

For each test cases, you should output the maximum flow from source 1 to sink N.

 

Sample Input

2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1

 

Sample Output

Case 1: 1
Case 2: 2

 

Author

HyperHexagon

 

Source

HyperHexagon's Summer Gift (Original tasks)

 

Recommend

zhengfeng   |   We have carefully selected several similar problems for you:  3572 3416 3081 3491 1533 

 

题目大意:给出u—>v的流量 c ,求1—>n的最大流量

解析:EK,或者 Dinic

Dinic:

#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 19
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
int n, mp

, pre
, book
;
int bfs()
{
memset(pre, 0, sizeof(pre));
queue<int> q;
q.push(1);
pre[1] = 1;
while(!q.empty())
{
int u = q.front(); q.pop();
if(u == n) break;
for(int i = 1; i <= n; i++)
{
if(!pre[i] && mp[u][i])
{
pre[i] = pre[u] + 1;
q.push(i);
}

}
}
return pre
> 0;
}

int dfs(int u, int cur_flow)
{
int i;
if(u == n) return cur_flow;
for(i = 1; i <= n; i++)
{
if(mp[u][i] > 0 && pre[u] + 1 == pre[i])
{
int f = dfs(i, min(cur_flow, mp[u][i]));
if(f)
{
mp[u][i] -= f;
mp[i][u] += f;
return f;
}
}
}
return 0;
}
int Dinic()
{
int f, ans = 0;
while(bfs())
{
while((f = dfs(1, inf)))
ans += f;
}
return ans;
}

int main()
{
int m, u, v, w, i, t, cnt = 0;
cin >> t;
while(t--)
{
memset(mp, 0, sizeof(mp));
scanf("%d%d", &n, &m);
for(i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
mp[u][v] += w;
}
int ans = Dinic();
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}

EK:

#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 19
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;
int n, mp

, pre
, book
;
int EK()
{
int ans = 0;
while(1)
{
memset(pre, 0, sizeof(pre));
memset(book, 0,sizeof(book));
queue<int> q;
q.push(1);
book[1] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int i = 1; i <= n; i++)
{
if(!book[i] && mp[u][i])
{
book[i] = 1;
q.push(i);
pre[i] = u;
}
}
}
int m = inf;
if(!book
) break;
for(int i = n; i != 1; i = pre[i])
{
m = min(m, mp[pre[i]][i]);
}
for(int i = n; i != 1; i = pre[i])
{
mp[pre[i]][i] -= m;
mp[i][pre[i]] += m;
}
ans += m;
}
return ans;
}

int main()
{
int m, u, v, w, i, t, cnt = 0;
cin >> t;
while(t--)
{
memset(mp, 0, sizeof(mp));
scanf("%d%d", &n, &m);
for(i = 1; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
mp[u][v] += w;
}
int ans = EK();
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: