运输问题2(cogs 12)
第一行,一个整数n,表示共有n个城市(2<=n<=100),产地是1号城市,销地是n号城市
下面有n行,每行有2n个数字。第p行第2q−1,2q列的数字表示城镇p与城镇q之间有无公路连接。数字为0表示无,大于0表示有公路,且这两个数字分别表示该公路流量的下界,上界。
第一行,1个整数ans,表示最大流量为ans
0 0 1 3 0 10 0 0 0 0 0 0
0 0 0 0 0 0 5 7 0 0 0 0
0 0 0 0 0 0 0 0 2 8 0 0
0 0 0 0 1 3 0 0 0 0 3 5
0 0 2 4 0 0 0 0 0 0 2 6
0 0 0 0 0 0 0 0 0 0 0 0
/*
有源汇的上下界最大流。
先按照无源汇网络流的方法建边,然后从汇点T向源点S连一条inf的边,跑可行流。
然后从题目要求的S向T跑最大流。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#define N 110
#define M 21000
#define inf 1000000000
using namespace std;
int s[N],head[N],dis[N],n,cnt=,S,T;
struct node{int v,f,pre;}e[M];
queue<int> q;
void add(int u,int v,int f){
e[++cnt].v=v;e[cnt].f=f;e[cnt].pre=head[u];head[u]=cnt;
e[++cnt].v=u;e[cnt].f=;e[cnt].pre=head[v];head[v]=cnt;
}
bool bfs(){
memset(dis,-,sizeof(dis));
q.push(S);dis[S]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].pre)
if(e[i].f&&dis[e[i].v]==-){
dis[e[i].v]=dis[u]+;
q.push(e[i].v);
}
}
return dis[T]!=-;
}
int dinic(int x,int f){
int rest=f;
if(x==T) return f;
for(int i=head[x];i;i=e[i].pre){
if(!e[i].f||dis[e[i].v]!=dis[x]+) continue;
int t=dinic(e[i].v,min(rest,e[i].f));
e[i].f-=t;e[i^].f+=t;rest-=t;
}
if(rest==f) dis[x]=-;
return f-rest;
}
int main(){
scanf("%d",&n);S=;T=n+;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int x,y;scanf("%d%d",&x,&y);
if(!x&&!y) continue;
s[i]+=x;s[j]-=x;add(i,j,y-x);
}
for(int i=;i<=n;i++)
if(s[i]>) add(i,T,s[i]);
else if(s[i]<) add(S,i,-s[i]);
add(n,,inf);
while(bfs()) dinic(S,inf);
int maxflow=;S=;T=n;
while(bfs()) maxflow+=dinic(S,inf);
printf("%d",maxflow);
return ;
}
运输问题2(cogs 12)的更多相关文章
- Cogs 12 运输问题2 (有上下界网络流)
#include <cstdlib> #include <algorithm> #include <cstring> #include <iostream&g ...
- cogs.12运输问题2题解
乍一看貌似和运输问题1没有任何区别,但本题有一个有意思的东西叫做下限,我个人称之为非强制下限,因为本题中要求的实际是我走这条边这条边才至少走下限的流,虽然出题人没说,但从样例来看确实是这样的,而强制下 ...
- Cogs 12. 运输问题2(有上下界的有源汇最大流)
运输问题2 ★★☆ 输入文件:maxflowb.in 输出文件:maxflowb.out 简单对比 时间限制:1 s 内存限制:128 MB 运输问题 [问题描述] 一个工厂每天生产若干商品,需运输到 ...
- SQL*Loader之CASE10
CASE10 1. SQL脚本 [oracle@node3 ulcase]$ cat ulcase10.sql rem host write sys$output "Building dem ...
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- [COGS 0011] 运输问题1
11. 运输问题1 ★★☆ 输入文件:maxflowa.in 输出文件:maxflowa.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 一个工厂每天生 ...
- 【费用流】【网络流24题】【cogs 739】运输问题
739. [网络流24题] 运输问题 ★★ 输入文件:tran.in 输出文件:tran.out 简单对照 时间限制:1 s 内存限制:128 MB «问题描写叙述: «编程任务: 对于给定的m 个仓 ...
随机推荐
- 前端-带header和footer的双栏布局
目标是实现如上图带header和footer的双栏布局,其中右侧sidebar是固定宽度,左侧content是自适应: https://www.zybuluo.com/dengzhirong/note ...
- daemon函数实现原理 守护进程
linux提供了daemon函数用于创建守护进程,实现原理如下: #include <unistd.h> int daemon(int nochdir, int noclose); 1. ...
- Drop it-freecodecamp算法题目
Drop it 1.要求 丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止. 第二个参数,func,是一个函数.用来测试数组的第一个元素,如果返回fasle,就从数组中抛 ...
- ORA-04031: Unable To Allocate 32 Bytes Of Shared Memory
记录一次生产库遇到的4031错误,后来通过调整sga大小将问题解决了 报错信息: ORA-04031: 无法分配 32 字节的共享内存 ("shared pool","s ...
- spring data事务
事务在spring data中的使用 1:事务一般在service层.因为一个service方法可能会多次调用不同的dao,为了保证事务的完整性,那么多次的dao都放到一个方法里面 2:读的时候可以不 ...
- Spring中注解大全和应用
@Controller@RestController:@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable@C ...
- Node 操作MySql数据库
1, 需要安装 MySQL 依赖 => npm i mysql -D 2, 封装一个工具类 mysql-util.js // 引入 mysql 数据库连接依赖 const mysql = re ...
- Python Map, Filter and Reduce
所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...
- Python9-MySQL-pymysql模块-day44
import pymysql user = input('username: ') pwd = input('password: ') conn = pymysql.connect(host=',da ...
- 动态拼接SQL语句
1.参考官方文档 ? if:字符判断 ? choose (when, otherwise):分支选择 ? trim (where, set):字符串截取:其中where标签封装查询条件,set标签封装 ...