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. SpringMVC 返回自定义属性名

    SpringMVC 返回的属性名默认是小写驼峰形式的实体对象中的属性名,如 userID 属性名它会返回 userId. 如果接口方式之前已经定下来,这样前端按原来的方式取数据会读取失败的,那有没有方 ...

  2. wcf post

    服务端: 1.接口 [OperationContract] [ServiceKnownType(typeof(CreatMicroBlogFeedViewModel))] [WebInvoke(Bod ...

  3. jQuery学习笔记(三)

    jQuery中的事件 页面加载 原生DOM中的事件具有页面加载的内容onload事件,在jQuery中同样提供了对应的内容ready()函数. ready与onload之间的区别: onload re ...

  4. Hibernate笔记7--JPA CRUD

    1.环境搭建,注意包结构的问题,src下建立名为META-INF的文件夹,放persistence.xml,位置放错,读不到会报错. <?xml version="1.0" ...

  5. Maven建立spring-web项目

    参考博客网址: https://blog.csdn.net/caoxuekun/article/details/77336444 1.eclipse集成maven 2.maven创建web项目 3.搭 ...

  6. 几幅手稿讲解CNN

    学习深度神经网络方面的算法已经有一段时间了,对目前比较经典的模型也有了一些了解.这种曾经一度低迷的方法现在已经吸引了很多领域的目光,在几年前仅仅存在于研究者想象中的应用,近几年也相继被深度学习方法实现 ...

  7. 使用C#版OpenCV进行圆心求取

    OpenCVSharp是OpenCV的.NET wrapper,是一名日本工程师开发的,项目地址为:https://github.com/shimat/opencvsharp. 该源码是 BSD开放协 ...

  8. [opencv3.2cmake error ] sys/videoio.h no such file or directories

    I don't have /usr/include/sys/videoio.h at all Before that , I have ipp download question. So I down ...

  9. nginx学习书籍推荐

    最好的书是源码 深入理解NGINX" 陶辉著 <实战Nginx...>张宴 <深入理解Nginx:模块开发与架构解析> nginx开发从入门到精通 Nginx HTT ...

  10. Using an Image for the Layer’s Content

    Using an Image for the Layer’s Content Because a layer is just a container for managing a bitmap ima ...