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. 基于 HTML5 结合互联网+ 的 3D 隧道

    前言 目前,物资采购和人力成本是隧道业发展的两大瓶颈.比如依靠民间借贷,融资成本很高:采购价格不透明,没有增值税发票:还有项目管控和供应链管理的问题.成本在不断上升,利润在不断下降,隧道产业的“互联网 ...

  2. 移除Windows图标快捷方式小箭头

    以管理员身份运行cmd,输入 reg delete "HKEY_CLASSES_ROOT\lnkfile" /v IsShortcut /f & taskkill /f / ...

  3. Python是如何实现生成器的原理

    python中函数调用的实质原理: python解释器(即python.exe)其实是用C语言编写的, 在执行python代码时,实际上是在用一个叫做Pyeval_EvalFramEx(C语言的函数) ...

  4. JAVA多线程-实现同步

    一.什么是线程安全问题 当多个线程同时共享,同一个全局变量或静态变量,做写的操作时,可能会发生数据冲突问题,也就是线程安全问题.但是做读操作是不会发生数据冲突问题. 二.如何解决线程安全问题 1)如何 ...

  5. flask 实现登录 登出 检查登录状态 的两种方法的总结

    这里我是根据两个项目的实际情况做的总结,方法一(来自项目一)的登录用的是用户名(字符串)和密码,前后端不分离,用form表单传递数据:方法二用的是手机号和密码登录,前后端分离,以json格式传递数据, ...

  6. Linux取代ifconfig指令的ip指令

  7. Java连接远程Mysql过程中遇到的各种问题

    2018-11-16 10:46 2018-11-19 21:35 前言 本篇文章记录的是本人在使用Java程序连接另一台电脑(同一局域网)上的Mysql数据库的过程中遇到的各种问题及解决方案.希望能 ...

  8. 洛谷P3806 点分治

    点分治 第一次写点分治..感觉是一个神奇而又暴力的东西orz 点分治大概就是用来处理树上链的信息,把路径分成过点x和不过点x的两种,不过点x的路径可以变成过点x的子树中一点的路径,递归处理 #incl ...

  9. 【SPOJ】DIVCNTK min_25筛

    题目大意 给你 \(n,k\),求 \[ S_k(n)=\sum_{i=1}^n\sigma_0(i^k) \] 对 \(2^{64}\) 取模. 题解 一个min_25筛模板题. 令 \(f(n)= ...

  10. Scrapy 框架,爬虫文件相关

    Spiders 介绍 由一系列定义了一个网址或一组网址类如何被爬取的类组成 具体包括如何执行爬取任务并且如何从页面中提取结构化的数据. 简单来说就是帮助你爬取数据的地方 内部行为 #1.生成初始的Re ...