Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 11405    Accepted Submission(s):
5418

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
 
 
刚开始看不是太理解  解析会后续更新
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#define INF 0x7fffff
#define MAX 2100
using namespace std;
int ans,head[MAX];
int n,m;
int dis[MAX],vis[MAX];
int cur[MAX];
struct node
{
int beg,end,cap,flow,next;
}edge[MAX];
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].cap=w;
edge[ans].flow=0;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int i,a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,0);
}
}
int bfs(int start,int over)
{
int i,v;
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
queue<int>q;
while(!q.empty())
q.pop();
q.push(start);
vis[start]=1;
dis[start]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(!vis[v]&&edge[i].cap>edge[i].flow)
{
vis[v]=1;
dis[v]=dis[u]+1;
if(v==over)
return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int x,int a,int over)
{
if(x==over||a==0)
return a;
int flow=0,f;
for(int& i=cur[x];i!=-1;i=edge[i].next)
{
if(dis[x]+1==dis[edge[i].end]&&(f=dfs(edge[i].end,min(a,edge[i].cap-edge[i].flow),over))>0)
{
edge[i].flow+=f;
edge[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow(int start,int over)
{
int flow=0;
while(bfs(start,over))
{
memcpy(cur,head,sizeof(head));
flow+=dfs(start,INF,over);
}
return flow;
}
int main()
{
int t,k,j,i;
scanf("%d",&t);
k=1;
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
printf("Case %d: ",k++);
printf("%d\n",maxflow(1,n));
}
return 0;
}

  

 

最新文章

  1. 20145223《Java程序程序设计》第9周学习总结
  2. 解决ewebeditor for php在IE8下报editor.js错误的解决方案
  3. hive 常见面试题
  4. Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
  5. Linux 源码的安装 3个步骤
  6. 详解Android动画之Frame Animation
  7. Windows下父进程监视子进程状态
  8. C primer plus 读书笔记第十章
  9. Hadoop(七)HDFS容错机制详解
  10. Java学习之计算机基础(一)
  11. word中批量转换字母数字为Times New Roman
  12. 【NPR】漫谈轮廓线的渲染
  13. 使用Visual Studio Team Services敏捷规划和项目组合管理(五)——组合管理
  14. ntp 时间同步
  15. C语言判断文件夹或者文件是否存在的方法【转】
  16. am335x SGX 移植
  17. 在windows中将Tomcat作为服务启动
  18. Python+selenium之简单介绍unittest单元测试框架
  19. C程序编译过程浅析(转)
  20. NAND flash阵营ToggleDDR和ONFI

热门文章

  1. py2exe把python程序转换exe
  2. sqlserver分页;mysql分页;orcale分页 的sql 查询语句
  3. 计算器(console version)
  4. 多个div并排显示的居中问题&mdash;&mdash;来自腾讯的一道面试题
  5. 在Python中调用C++,使用SWIG
  6. ffmpeg编译 --enable :没有命令
  7. 汇编语言中,SP,BP ,SI,DI作用?
  8. 板级支持包(BSP)
  9. mingw32 下编译 zlib
  10. centos6.5安装vbox