运输问题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 个仓 ...
随机推荐
- curl_easy_setopt函数介绍
本节主要介绍curl_easy_setopt中跟http相关的参数.注意本节的阐述都是以libcurl作为主体,其它为客体来阐述的. 1. CURLOPT_URL 设置访问URL 2. ...
- 26.VUE学习之--提交表单不刷新页面,事件修饰符之使用$event与prevent修复符操作表单
提交表单不刷新页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 精通Spring Boot---使用@ControllerAdvice处理异常
在Spring 3.2中,新增了@ControllerAdvice.@RestControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@Mode ...
- Django API 为 D3 提供数据
在工作中见过有的人即便使用了Django,依然还在采取json或geojson的文件形式为页面提供数据,相当于嵌入数据而非加载.下面是个简单有效的例子: 先从 model.py 开始 # models ...
- 谭浩强C第四版(p141)16.输出以下图案
运行结果: * *** ***** ******* ***** *** * Press any key to continue #include<stdio.h> int main() { ...
- 必须使用member initialization list来初始化的情况
// member initialization #include <iostream> using namespace std; class Circle { double radius ...
- Network of Schools POJ - 1236 (强联通)
一些学校连接到了一个计算机网络.网络中的学校间有如下约定:每个学校维护一个列表,当该学校收到软件或信息后将会转发给列表中的所有学校(也就是接收方列表).需要注意的是如果B学校在A学校的接收方列表中,A ...
- 笔记-爬虫-robots.txt
笔记-爬虫-robots.txt 1. robots.txt文件简介 1.1. 是什么 robots.txt是用来告诉搜索引擎网站上哪些内容可以被访问.哪些不能被访问.当搜索引擎访问一 ...
- momo不是玩具,.Net雄起
互联网时代 .NET 会渐渐衰落吗?一个架构师对 .NET 的思考 2015-12-14 11:03 darklx 博客园 字号:T | T 为了更好的适应互联网时代的需求,我们公司已经把我们的 .N ...
- Python linecache模块
Table of Contents 1. linecache 1.1. 其它 2. 参考资料 linecache 今天分享一个python的小模块: linecache, 可以用它方便地获取某一文件某 ...