Flow Problem

TimeLimit:5000MS  MemoryLimit:32768KB
64-bit integer IO format:%I64d
 
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.
SampleInput
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
SampleOutput
Case 1: 1
Case 2: 2
题解:模板题,求最大流量
 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f;
struct node
{
int to,flow,next;
} edge[maxn*];
int first[maxn],sign,vis[maxn],pre[maxn];
void init()
{
memset(first,-,sizeof(first));
sign=;
}
void add_edge(int u,int v,int flow)
{
edge[sign].to=v;
edge[sign].flow=flow;
edge[sign].next=first[u];
first[u]=sign++;
edge[sign].to=u;
edge[sign].flow=;///建一条反向边,流量为0;
edge[sign].next=first[v];
first[v]=sign++;
}
bool bfs(int s,int t)
{
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
vis[s]=;
queue<int >que;
que.push(s);
while(!que.empty())
{
int now=que.front();
que.pop();
if(now==t)
{
return ;
}
for(int i=first[now];~i;i=edge[i].next)
{
int to=edge[i].to,flow=edge[i].flow;
if(!vis[to]&&flow>)
{
vis[to]=;
que.push(to);
pre[to]=i;///记录路径 记录的是由上一条边到to这个点
}
}
}
return ; }
int edomon_krap(int s,int t)///起点 终点
{
int max_flow=;
while(bfs(s,t))
{
int min_flow=inf;
int x=t;
while(x!=s)
{
// printf("%d\n",x);
int index=pre[x];
//printf("intdex %d\n",index);
min_flow=min(min_flow,edge[index].flow);
x=edge[index^].to;
//printf("x==%d\n",x);
}
x=t;
while(x!=s)
{
int index=pre[x];
edge[index].flow-=min_flow;
edge[index^].flow+=min_flow;///反向边加上min_flow
x=edge[index^].to;
}
max_flow+=min_flow; }
return max_flow;
}
int main()
{
int t,n,m;
scanf("%d",&t);
for(int i=;i<=t;i++)
{
init();
scanf("%d%d",&n,&m);
for(int j=;j<=m;j++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
printf("Case %d: %d\n",i,edomon_krap(,n));
}
}

Flow Problem的更多相关文章

  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(最大流(水体))

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

  3. hdu 3549 Flow Problem

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

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

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

  5. hdoj 3549 Flow Problem【网络流最大流入门】

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

  6. Flow Problem(最大流)

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

  7. HDU3549 Flow Problem 【最大流量】

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

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

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

  9. Hdu3549 Flow Problem 2017-02-11 16:24 58人阅读 评论(0) 收藏

    Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a ...

随机推荐

  1. .Net Core应用框架Util介绍(二)

    Util的开源地址 https://github.com/dotnetcore/util Util的开源协议 Util以MIT协议开源,这是目前最宽松的开源协议,你不仅可以用于商业项目,还能把Util ...

  2. python 项目自动生成requirements.txt文件

    主要使用目的: 任何应用程序通常需要设置安装所需并依赖一组类库来满足工作要求.通过requirements.txt可以一次性安装程序所需要和依赖的包. 为工程生成requirements.txt的两种 ...

  3. 远程连接腾讯云服务器MySQL数据库

    1.添加腾讯云安全组规则的MySQL 3306端口 将所有端口打开,至少打开3306,不在赘述. 2.打开更改MySQL配置文件 打开配置文件 vi /etc/mysql/mysql.conf.d/m ...

  4. URL https://i.cnblogs.com/EditPosts.aspx?opt=1

    URL url = new URL("https://i.cnblogs.com");URL url1 = new URL(url, "EditPosts.aspx?op ...

  5. Day 3 下午

    依旧是组合数问题 先来看一道题 如图,一个n*m的方格中,从原点开始,每次只能向上走或者向右走,求走到点(n,m)共有多少种走法 一般做法: 一个一个写,每一个节点的种数=它左边的数量+右边的数量 显 ...

  6. MT【317】两次判别式

    已知$a^2+b^2+c^2-ab-bc=1$求$c$的最大值______ 注意到$2c^2-3(a^2+b^2+c^2-ab-bc)=-(c-\dfrac{3}{2}b)^2-3(a-\dfrac{ ...

  7. <数据结构基础学习>(三)Part 2 队列

    一.队列 Queue 队列也是一种线性结构 相比数组,队列对应的操作是数组的子集 只能从一端(队尾)添加元素,只能从另一端(队首)取出元素. (排队) 队列是一种先进先出的数据结构(先到先得)FIFO ...

  8. mysql慢查询日志按天切割归纳

    问题描述: mysql开启慢查询功能,再正常不过,那么存在这样一种情况:慢查询写入的文件位置和文件名是指定好的,如果慢查询时间设定严苛,不出意外,记录慢查询的单个文件大小会日益增大,几十兆或者上百兆, ...

  9. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  10. 题解-洛谷P1601 A+B Problem(高精)

    https://www.luogu.org/problemnew/show/P1601(题目传送) 显然数据范围超过了long long类型,故不能简单的用两个长整型存起来相加.这里用到大数据的高精度 ...