sgu176 Flow Construction【有源汇有上下界最小流】
同样是模板题。
首先将有源汇转换为无源汇,假设原来的源汇为st,我们加入的源汇为ST,那么我们应该从t到s连一条流量为+∞的边,使原来的st满足收支平衡,退化为普通节点。
分离必要边和其他边,从S到T跑最大流,所有与源或者汇相连的边都流满则证明有解。
去掉t到s容量为+∞的边,去掉必要边,从t到s跑最大流。
把得到的答案相减即可。
如果我们得到的答案是负的,那么说明它内部t到s连成了环,那么我们加上S到s容量为-ans的边,跑S到t的最大流,这样所有的边的流量应该就是0,再加上流量下界即为答案。
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define pb push_back
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> ;
const i64 INF = ~0ULL >> ;
//****************************** const int maxn = , maxm = ; struct Ed {
int u, v, nx, c; Ed() {}
Ed(int _u, int _v, int _nx, int _c) :
u(_u), v(_v), nx(_nx), c(_c) {}
} E[maxm << ];
int G[maxn], edtot = ;
void addedge(int u, int v, int c) {
E[++edtot] = Ed(u, v, G[u], c);
G[u] = edtot;
E[++edtot] = Ed(v, u, G[v], );
G[v] = edtot;
} int level[maxn], S, T;
bool bfs(int s, int t) {
static int que[maxn]; int qh(), qt();
clr(level); level[que[++qt] = s] = ;
while (qh != qt) {
int x = que[++qh]; if (qh == maxn - ) qh = ;
for (int i = G[x]; i; i = E[i].nx) if (E[i].c && !level[E[i].v]) {
level[que[++qt] = E[i].v] = level[x] + ;
if (qt == maxn - ) qt = ;
}
}
return !!level[t];
}
int dfs(int u, int rm, int t) {
if (u == t) return rm;
int rm1 = rm;
for (int i = G[u]; i; i = E[i].nx) {
if (E[i].c && level[E[i].v] == level[u] + ) {
int flow = dfs(E[i].v, min(E[i].c, rm), t);
E[i].c -= flow, E[i ^ ].c += flow;
if ((rm -= flow) == ) break;
}
}
if (rm1 == rm) level[u] = ;
return rm1 - rm;
} int l[maxm], in[maxn];
int main() {
int n, m;
scanf("%d%d", &n, &m);
rep(i, , m) {
int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b);
l[i] = a * b;
if (b) in[y] += a, in[x] -= a;
addedge(x, y, a - a * b);
}
S = n + , T = n + ;
int sum();
rep(i, , n) {
if (in[i] > ) sum += in[i];
if (in[i] > ) addedge(S, i, in[i]);
else addedge(i, T, -in[i]);
}
addedge(n, , 0x3f3f3f3f);
while (bfs(S, T)) sum -= dfs(S, 0x3f3f3f3f, T);
if (sum) { puts("Impossible"); return ; }
int ans = E[edtot].c;
E[edtot].c = E[edtot ^ ].c = ;
int tmp();
while (bfs(n, )) tmp += dfs(n, 0x3f3f3f3f, );
ans -= tmp;
if (ans < ) {
addedge(S, , -ans);
while (bfs(S, n)) dfs(S, 0x3f3f3f3f, n);
ans = ;
}
printf("%d\n", ans);
rep(i, , m) {
printf(i == ? "%d" : " %d", E[(i << ) ^ ].c + l[i]);
}
puts("");
return ;
}
sgu176 Flow Construction【有源汇有上下界最小流】的更多相关文章
- loj #117. 有源汇有上下界最小流
题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...
- LOJ.117.[模板]有源汇有上下界最小流(Dinic)
题目链接 有源汇有上下界最小流 Sol1. 首先和无源汇网络流一样建图,求SS->TT最大流: 然后连边(T->S,[0,INF]),再求一遍SS->TT最大流,答案为新添加边的流量 ...
- Flow construction SGU - 176 有源汇有上下界最小流 二分法和回流法
/** 题目:Flow construction SGU - 176 链接:https://vjudge.net/problem/SGU-176 题意: 有源汇有上下界的最小流. 给定n个点,m个管道 ...
- sgu 176 Flow construction(有源汇的上下界最小流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...
- 【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)
Description You’ve just built a circuit board for your new robot, and now you need to power it. Your ...
- bzoj 2502 清理雪道(有源汇的上下界最小流)
[题意] 有一个DAG,要求每条边必须经过一次,求最少经过次数. [思路] 有上下界的最小流. 边的下界为1,上界为无穷.构造可行流模型,先不加ts边跑一遍最大流,然后加上t->s的inf边跑 ...
- Crazy Circuits HDU - 3157(有源汇有上下界最小流)
给出每条边的下界 求最小流 板题 提供两个板子代码 虽然这个题 第一个比较快 但在loj上https://loj.ac/problem/117 的板题 第一个1700+ms 第二个才600+ms ...
- LOJ117 有源汇有上下界最小流(上下界网络流)
跑出可行流后从原来的汇点向原来的源点跑最大流,原图最小流=inf-maxflow.显然超源超汇的相关边对其也没有影响.原图最小流=可行流-原图新增流量,因为t向s流量增加相当于s向t流量减少.但为什么 ...
- bzoj2502: 清理雪道(有源汇有上下界最小流)
传送门 别说话,自己看,我不会->这里 我这里用的建图方法是先跑一次最大流,连上$(t,s,inf)$之后再跑一遍,然后答案就是之前连的那条边的反向边的流量 据说还有种方法是连上$(t,s,in ...
随机推荐
- http://www.iteye.com/job/topic/1133159
Lucene 的索引体系是一个写独占,读共享的结构,这意味着,我们在使用多线程进行添加索引时,性能并不会得到明显的提升,所以任何时刻只能有一个线程对索引进行写 入操作,而保障这个操作的安全性则是来自于 ...
- stray '/241' in program 错误
意思是c/c++中的编译错误. 该错误是指源程序中有非法字符,需要去掉非法字符.一般是由于从别的地方粘贴过来造成的. 方法:1.把所粘的文字放到记事本里就行了 2.把出错行的空格删掉重新打一下试试.
- linux中服务器定时程序设定
服务器不重启的情况下定时自动重启apache及mysql服务,其实也大同小异.具体步骤如下: 一.每天的12点及16点重启apache及mysql服务 [root@www bin]# cd /opt ...
- SecureCRT 上传文件的两种方法 Zmodem、SFTP
Zmodem: 无论有xshell还是secureCRT连接linux的时. 默认都用一个zmodem可以帮助window和linux之间传输文件 很方便和实用的工具. 不过默认是无法使用的 需要安装 ...
- css 内联与块
内联元素可以理解为不能直接设置宽度和高度元素,比如span,你为他设置宽度和高度没有效果,除非你把它设置成块级元素. 如下面的代码把display:block;属性值去掉的话,宽度和高度都不会起作用了 ...
- DOM4j 操作XML
<?xml version="1.0" encoding="GBK"?> <persons> <men> <perso ...
- ListView下拉刷新、上拉载入更多之封装改进
在Android中ListView下拉刷新.上拉载入更多示例一文中,Maxwin兄给出的控件比较强大,前面有详细介绍,但是有个不足就是,里面使用了一些资源文件,包括图片,String,layout,这 ...
- angular Jsonp的坑
angular 为了解决跨域问题 一些第三方接口会提供jsonp来调用,需要使用callback=JSON_CALLBACK来处理 这个时候问题来了,有些借口是不支持callback里面带有点语法的, ...
- map map
下面的无法运行. @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Te ...
- cc2530串口通信流程
//串口发送接收流程 main: //主函数 ->osal_init_system(); //操作系统初始化 ->osalInitTasks(); //任务初始化 -->ZDApp_ ...