Flow Problem
Flow Problem
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)
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
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的更多相关文章
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu------(3549)Flow Problem(最大流(水体))
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- hdu 3549 Flow Problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...
- hdu 3549 Flow Problem Edmonds_Karp算法求解最大流
Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...
- hdoj 3549 Flow Problem【网络流最大流入门】
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- Flow Problem(最大流)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- HDU3549 Flow Problem 【最大流量】
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- HDU 3549 Flow Problem 网络流(最大流) FF EK
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- 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 ...
随机推荐
- Spring Boot2.0使用Spring Security
一.Spring Secutity简介 Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...
- Firefox 中出现的 “Network Protocol Error”怎么办
Mozilla Firefox 多年来一直是我的默认 Web 浏览器,我每天用它来进行日常网络活动,例如访问邮件,浏览喜欢的网站等.今天,我在使用 Firefox 时遇到了一个奇怪的错误.我试图在 R ...
- 手动用tomcat启动war包,无法访问web项目
先说一下自己采的小坑,网上大多解答都是复制来复制去,不说重点在哪.我这里简单总结下访问路径问题 一.用idea打成war包,具体步骤如下图: 步骤:在项目配置选Artifacts新建Web Appli ...
- Python——Tk控件说明
控件 描述 具体说明 Button 按钮 点此链接 Canvas 提供绘图形状的功能,包含图像和位图 点此链接 Checkbutton 多选框 点此链接 Entry 单行文本框 点此链接 Fr ...
- Linux取代ifconfig指令的ip指令
- docker完整配置nginx+php+mysql
首先了解一个方法: 使用docker exec进入Docker容器 docker在1.3.X版本之后还提供了一个新的命令exec用于进入容器,这种方式相对更简单一些,下面我们来看一下该命令的使用: s ...
- python学习日记(内置函数补充)
剩余匿名函数 序列 序列——列表和元组相关的:list和tuple 序列——字符串相关的:str,format,bytes,bytearry,memoryview,ord,chr,ascii,repr ...
- Django模板
Django模板系统 官方文档 常用语法 只需要记住两种特殊符号: {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 { 变量名 }} 变量名由字母数字和下划线组成. 点 ...
- 通过nginx访问linux目录
http { ...... autoindex on; autoindex_exact_size off; autoindex_localtime on; server { listen 80; .. ...
- 如何重写Java中的equals方法
Java中,只有8种基本类型不是对象,例如:4种整形类型(byte, short, int,long),2种浮点类型(flout, double),boolean, char不是对象,其他的所有类型, ...