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);
}
}

Flow Problem HDU - 3549的更多相关文章

  1. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  2. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  3. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  4. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  5. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  6. HDU 3549 Flow Problem 网络流(最大流) FF EK

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  7. hdu 3549 Flow Problem (网络最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  8. HDU 3549 基础网络流EK算法 Flow Problem

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit ...

  9. HDU 3549 Flow Problem (最大流ISAP)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

随机推荐

  1. PHP setcookie()用法

    定义和用法 setcookie() 函数向客户端发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过 ...

  2. Default Bearer, Dedicated Bearer... What exactly is bearer ?

    Default Bearer, Dedicated Bearer... What exactly is bearer ?   While trying to get a better understa ...

  3. HDU 5505——GT and numbers——————【素数】

    GT and numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. C#字符串自增自减算法

    本文URL:http://www.cnblogs.com/CUIT-DX037/p/6770535.html 实现字符串自增和自减运算: 1.数字从 0-9 变化: 2.字母从 A-Z.a-z 变化: ...

  5. Java开发笔记(九十八)利用Callable启动线程

    前面介绍了如何利用Runnable接口构建线程任务,该方式确实方便了线程代码的复用与共享,然而Runnable不像公共方法那样有返回值,也就无法将线程代码的处理结果传给外部,造成外部既不知晓该线程是否 ...

  6. cairo-dock天气位置代码

    cairo-dock天气位置代码: 城市: 北京CHXX0008哈尔滨CHXX0046长春CHXX0010沈阳CHXX0119大连CHXX0019天津CHXX0133呼和浩特CHXX0249乌鲁木齐C ...

  7. unobtrusive验证,ajax局部加载后验证失效解决方法

    页面加载后运行此代码 $(function() {$.validator.unobtrusive.parse($("form")); }); 原因: 页面加载后unobtrusiv ...

  8. li浮动 第二行第一个位置空白

    li浮动 第二行第一个位置空白:解决办法 li加上 vertical-align:bottom;overflow:hidden; 一行字多了 省略号代替: text-overflow: ellipsi ...

  9. Linux命令行环境与桌面环境护切换

    1.前言 在大部分情况下,我们在使用Linux时习惯使用命令行环境,但是有时候也还是会使用到安装桌面环境,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境.以Centos 6.5 为例演示一 ...

  10. Eucalyptus学习汇总

    Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) 是一种开 ...