hdu 3549 Flow Problem (最大流—EK—Dinic)
2016-09-24 09:17
330 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549
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;
}
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;
}
相关文章推荐
- centos6安装禅道
- ubuntu 使用 find&grep 查找文件
- 开闭原则
- huffman编码压缩算法(转)
- intent详解2
- 高德地图坐标转换为百度地图坐标
- 密码学:生日攻击
- [置顶] Mysql学习笔记
- 初学移动开发技术_实验二_任务四_Activity的生命周期测试
- 浅谈Web自适应
- C语言中两种方式表示时间日期值time_t和struct tm类型的相互转换
- java之二维数组
- java之面向对象
- 详解微信小程序开发教程
- list和vector有什么区别?
- 微机原理与接口技术之8086的编程结构
- uva 679 Dropping Balls
- Chapter 1 First Sight——24
- 使用dd命令烧写linux系统到sd卡
- 微信小程序下载工具及调试详解