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 ...
随机推荐
- HTML60秒倒计时
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Nginx 关于进程数 与CPU核心数相等时,进程间切换的代价是最小的-- 绑定CPU核心
在阅读Nginx模块开发与架构模式一书时: "Nginx 上的进程数 与CPU核心数相等时(最好每个worker进程都绑定特定的CPU核心),进程间切换的代价是最小的;" &am ...
- Masonry 与 frame 混用导致的问题
https://www.jianshu.com/p/357fab4b84e7 Masonry 与 frame 混用可能出现子控件大小跟预期不一致的情况,具体是什么样呢? 例如,自定义一个 UIView ...
- git中如何撤销部分修改?
以提问中修改了两个文件a.b为例,假设需要撤销文件a的修改,则修改后的两个文件: 1.如果没有被git add到索引区 git checkout a 便可撤销对文件a的修改 2.如果被git add到 ...
- vue2.0里的路由钩子
路由钩子 在某些情况下,当路由跳转前或跳转后.进入.离开某一个路由前.后,需要做某些操作,就可以使用路由钩子来监听路由的变化 全局路由钩子: router.beforeEach((to, from, ...
- Jupyter Notebook(推荐使用Anaconda安装)
一.Jupyter Notebook介绍 1.简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文档编写.运行代码和展示结果. 简而言之,Jupy ...
- mpvue——componets中引入vant-weapp组件
前言 这个问题很奇葩,网上也有很多人,给了很多方法,但是我想说的是,那些方法我用了都没效果,我试了一些都没效果,因为我当时引入时报错说没有export default出来,但是一旦暴露出来就又出其他问 ...
- Java【第四篇】基本语法之--循环
循环语句功能 在循环条件满足的情况下,反复执行特定代码 循环语句的四个组成部分 初始化部分(init_statement)循环条件部分(test_exp) 循环体部分(body_statement) ...
- 解决类似umount target is busy挂载盘卸载不掉问题
问题描述: Linux下挂载后的分区或者磁盘某些时候需要umount的时候出现类似“umount: /mnt: target is busy.”等字样,或者“umount: /xxx: device ...
- PEP8中文翻译(转)
原文:https://github.com/zgia/manual PEP 8 -- Style Guide for Python Code PEP Index > PEP 8 -- Style ...