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. iOS开发基础-九宫格坐标(2)之模型

    在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...

  2. deb包转化为rpm包

    deb文件格式本是ubuntu的安装文件,那么我想要在fedora中安装,需要把deb格式转化成rpm格式,我们用skype举例: 1.下载转换工具alien_8.78.tar.gz 2.deb转化成 ...

  3. [转帖]SAP BASIS日常需要做的工作

    SAP BASIS日常需要做的工作 https://www.cnblogs.com/swordxia/p/4790684.html SAP Basis的一些日常工作包括用户权限管理.集团管理.数据库管 ...

  4. Elasticsearch 创建以及修改索引结构

    从问题出发,这篇内容可以解决以下几个问题: 一:如何开启关闭Es索引(数据库)? 二:如何创建索引(数据库)结构? 三:如何向已有索引(数据库)中添加类型(表)结构? 四:如何向已有类型(表)中添加新 ...

  5. sqlite 数据库 boolean类型的小小测试

    根据官方文档的介绍: SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored ...

  6. 【并发编程】【JDK源码】J.U.C--线程池

    原文:慕课网实战·高并发探索(十四):线程池 Executor new Thread的弊端 每次new Thread 新建对象,性能差. 线程缺乏统一管理,可能无限制的新建线程,相互竞争,可能占用过多 ...

  7. Java基础 -- final关键字

    在java的关键字中,static和final是两个我们必须掌握的关键字.不同于其他关键字,他们都有多种用法,而且在一定环境下使用,可以提高程序的运行性能,优化程序的结构.下面我们来了解一下final ...

  8. R语言统计学习-1简介

    一. 统计学习概述 统计学习是指一组用于理解数据和建模的工具集.这些工具可分为有监督或无监督.1.监督学习:用于根据一个或多个输入预测或估计输出.常用于商业.医学.天体物理学和公共政策等领域.2.无监 ...

  9. centos7项目部署

    1. 安装nginx   添加CentOS 7 Nginx yum资源库 sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/ng ...

  10. 使用Eclipse创建动态的web工程

    使用Eclipse创建动态的web工程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.修改工作区的编码 1>.点击Window选择Preferences 2>.将默 ...