运输问题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 个仓 ...
随机推荐
- Http请求 GET和POST,405错误
我就简单说吧,在用SringMVC时,我们通常会用到 @RequestMapping(value="/test",method=RequestMethod.GET) public ...
- 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)
微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...
- git 命令汇总
本地库处理 git init 初始化仓库 git clone [地址] 下载项目 git status 查看当前暂存等状态 git add 添加暂存 cat .git/config 查看git配置 l ...
- Python导入模块方法
import module_name 导入整个模块 from module_name import function_name 导入特定函数 from module_name import funct ...
- PHP switch问题
$a = 0; switch($a){ case $a > 7: echo 234; break; case $a > 2: echo 4556; break; default: echo ...
- 43.VUE学习之--组件之使用.sync修饰符与computed计算属性超简单的实现美团购物车原理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 739. Daily Temperatures
https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...
- 图学java基础篇之IO
java io体系 如图可以看出,java的io按照包来划分的话可以分为三大块:io.nio.aio,但是从使用角度来看,这三块其实揉杂在一起的,下边我们先来概述下这三块: io:主要包含字符流和字节 ...
- 4819: [Sdoi2017]新生舞会(分数规划)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1031 Solved: 530[Submit][Statu ...
- leetcode 【 Triangle 】python 实现
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...