Flow Problem HDU - 3549
2024-08-29 21:19:12
Flow Problem HDU - 3549
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.
InputThe 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)OutputFor 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 题意:网络流模版题,1到n的结点的最大的流
思路:用dinic
#include<stdio.h>
#include<iostream>
#include<map>
#include<string.h>
#include<queue>
#include<vector>
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int dis[];
int flow[][];
int n,m;
int bfs()
{
memset(dis,-,sizeof(dis));
queue<int>Q;
dis[]=;
Q.push();
while(!Q.empty())
{
int top=Q.front();
Q.pop();
for(int i=;i<=n;i++)
{
if(flow[top][i]>&&dis[i]<)
{
dis[i]=dis[top]+;
Q.push(i);
}
}
}
if(dis[n]>)return ;
return ;
}
int dinic(int x,int k)
{
if(x==n)
return k;
int y;
for(int i=;i<=n;i++)
{
if(flow[x][i]>&&dis[i]==dis[x]+&&(y=dinic(i,min(k,flow[x][i]))))
{
flow[x][i]-=y;
flow[i][x]+=y;
return y;
}
}
return ; }
int main()
{
int t;
scanf("%d",&t);
int cas=;
while(t--)
{
memset(flow,,sizeof(flow));
scanf("%d%d",&n,&m);
int a,b,c;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
flow[a][b]+=c;
}
int ans=;
while(bfs())
{
//cout<<"++"<<endl;
int res;
while(res=dinic(,inf))ans+=res;
}
printf("Case %d: %d\n",cas++,ans);
}
}
用sap加邻接矩阵,maze那里是叠加,不是覆盖
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 1e3+;
const int mod = 1e9+; int maze[maxn][maxn];
int gap[maxn],dis[maxn],pre[maxn],cur[maxn]; int sap(int start,int end ,int nodenum)
{
memset(cur,,sizeof cur);
memset(dis,,sizeof dis);
memset(gap,,sizeof gap);
int u = pre[start] = start,maxflow = ,aug = -;
gap[] = nodenum;
while(dis[start] < nodenum)
{
loop:
for(int v = cur[u];v < nodenum; v++)
if(maze[u][v] && dis[u] == dis[v] + ){
if(aug == - || aug > maze[u][v])
aug = maze[u][v];
pre[v] = u;
u = cur[u] = v;
if(v == end)
{
maxflow += aug;
for(u = pre[u]; v!=start;v=u,u=pre[u])
{
maze[u][v] -= aug;
maze[v][u] += aug;
}
aug = -;
}
goto loop;
}
int mindis = nodenum - ;
for(int v = ;v<nodenum;v++)
if(maze[u][v] && mindis > dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]]) == )
break;
gap[dis[u] = mindis + ]++;
u = pre[u]; }
return maxflow;
} int main()
{
int t;
scanf("%d",&t);
for(int ca = ;ca <= t; ca++)
{
memset(maze,,sizeof maze);
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int a,b,l;
scanf("%d%d%d",&a,&b,&l);
maze[a-][b-] += l;
}
int res = sap(,n-,n);
printf("Case %d: %d\n",ca,res);
}
}
最新文章
- phpcms 表单提交发送邮件
- android/java 根据当前时间判断股票交易状态(未开盘 交易中 休市中 已收盘)
- C#分割字符串
- xampp笔记
- linux_fedora nexus_auto_start
- [Python]Unicode转ascii码的一个好方法
- 学习ORM框架—hibernate(三):跟踪持久化对象状态,掌握对象持久化
- [INS-30060]:Check for group existence failed
- C++ STL之min_element()与max_element()(取容器中的最大最小值)
- Javascript面向对象编程(三):非构造函数的继承
- SpriteBuilder中锚点的一般用法
- 2017OKR年终回顾与2018OKR初步规划
- oracle 汉字转化拼音函数
- 事件代理on
- python-飞机大战
- WPF Application 类介绍以及怎样修改启动方式
- centos 7 删除yum安装的openjdk
- 秋风下的萧瑟 NOIP2018 游记
- WPF样式(Style)入门
- dubbo高级配置学习(上)