POJ3801 Crazy Circuits
嘟嘟嘟
上下界网络流之最小流。
建图不说啦,裸的。
在有附加源\(S\)和附加汇\(T\)的图上跑完后,删除和\(S, T\)相连的边。然后因为可能流多了,所以现在应该退流,于是我们从\(t\)到\(s\)跑一遍最大流就行了。
今天总算有一道1A的题了,舒服。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 55;
const int maxe = 1e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, s, t, S, T;
char s1[2], s2[2];
struct Edge
{
int nxt, from, to, cap, flow;
}e[maxe];
int head[maxn], ecnt = -1;
void addEdge(int x, int y, int w)
{
e[++ecnt] = (Edge){head[x], x, y, w, 0};
head[x] = ecnt;
e[++ecnt] = (Edge){head[y], y, x, 0, 0};
head[y] = ecnt;
}
int dis[maxn];
bool bfs(int s, int t)
{
Mem(dis, 0); dis[s] = 1;
queue<int> q; q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now], v; i != -1; i = e[i].nxt)
{
if(!dis[v = e[i].to] && e[i].cap > e[i].flow)
{
dis[v] = dis[now] + 1;
q.push(v);
}
}
}
return dis[t];
}
int cur[maxn];
int dfs(int now, int _t, int res)
{
if(now == _t || res == 0) return res;
int flow = 0, f;
for(int& i = cur[now], v; i != -1; i = e[i].nxt)
{
if(dis[v = e[i].to] == dis[now] + 1 && (f = dfs(v, _t, min(res, e[i].cap - e[i].flow))) > 0)
{
e[i].flow += f; e[i ^ 1].flow -= f;
flow += f; res -= f;
if(res == 0) break;
}
}
return flow;
}
int maxflow(int s, int t)
{
int flow = 0;
while(bfs(s, t))
{
memcpy(cur, head, sizeof(head));
flow += dfs(s, t, INF);
}
return flow;
}
int d[maxn];
void init()
{
Mem(head, -1); ecnt = -1;
s = 0, t = n + 1;
S = t + 1; T = S + 1;
}
int cg(char c)
{
if(c == '+') return s;
if(c == '-') return t;
return c -'0';
}
int main()
{
while(scanf("%d%d", &n, &m))
{
if(!n && !m) break;
init();
for(int i = 1; i <= m; ++i)
{
scanf("%s%s", s1, s2); int w = read();
int x = cg(s1[0]), y = cg(s2[0]);
addEdge(x, y, INF - w);
d[x] += w; d[y] -= w;
}
int tot = 0;
for(int i = 0; i <= t; ++i)
if(d[i] < 0) addEdge(S, i, -d[i]);
else addEdge(i, T, d[i]), tot += d[i];
addEdge(t, s, INF);
if(maxflow(S, T) < tot) puts("impossible");
else
{
int tp = -e[ecnt].flow; e[ecnt ^ 1].flow = e[ecnt ^ 1].cap;
for(int i = head[S]; i != -1; i = e[i].nxt) e[i ^ 1].cap = e[i ^ 1].flow;
for(int i = head[T]; i != -1; i = e[i].nxt) e[i ^ 1].cap = e[i ^ 1].flow;
write(tp - maxflow(t, s)), enter;
}
}
return 0;
}
POJ3801 Crazy Circuits的更多相关文章
- HDU 3157 Crazy Circuits(有源汇上下界最小流)
HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...
- hdoj 3157 Crazy Circuits 【有下界最小流】
题目:hdoj 3157 Crazy Circuits 题意:如今要制造一个电路板.电路板上有 n 个电子元件,各个元件之间有单向的电流流向.然后有一个 + .电流进入, -- 电流汇入,然后推断能不 ...
- hdu Crazy Circuits
Crazy Circuits 题目: 给出一个电路板,从+极出发到负极. 如今给你电路板上的最小电流限制,要你在电流平衡的时候求得从正极出发的最小电流. 算法: 非常裸的有源汇最小流.安有源汇最大流做 ...
- HDU 3157 Crazy Circuits (有源汇上下界最小流)
题意:一个电路板,上面有N个接线柱(标号1~N) 还有两个电源接线柱 + - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...
- HDU 3157 Crazy Circuits
Crazy Circuits Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...
- 【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)
Description You’ve just built a circuit board for your new robot, and now you need to power it. Your ...
- HDU3157 Crazy Circuits(有源汇流量有上下界网络的最小流)
题目大概给一个电路,电路上有n+2个结点,其中有两个分别是电源和负载,结点们由m个单向的部件相连,每个部件都有最少需要的电流,求使整个电路运转需要的最少电流. 容量网络的构建很容易,建好后就是一个有源 ...
- hdu 3157 Crazy Circuits 网络流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3157 You’ve just built a circuit board for your new r ...
- hdu 3157 Crazy Circuits 有源汇和下界的最小费用流
题目链接 题意:有n个节点,m个用电器.之后输入m行每行三个整数a,b,c; 节点a为正极(或者a 为 '+'即总的正极),b为该用电器的负极(b = '-'表示总的负极),c为该用电器要正常工作最小 ...
随机推荐
- [日常] Go语言圣经-指针对象的方法-bit数组习题
练习6.1: 为bit数组实现下面这些方法 func (*IntSet) Len() int // return the number of elements func (*IntSet) Remov ...
- [日常] Go语言圣经-函数多返回值习题
Go语言圣经-函数多返回值1.在Go中,一个函数可以返回多个值2.许多标准库中的函数返回2个值,一个是期望得到的返回值,另一个是函数出错时的错误信息3.如果一个函数将所有的返回值都显示的变量名,那么该 ...
- 【Dubbo&&Zookeeper】1、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
转自:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架 ...
- java图片压缩(Thumbnails)
package com.hzxc.groupactivity.server.util; import java.awt.image.BufferedImage; import java.io.*; i ...
- JS 提升 p4
提示不多说,记住几个要点: 1.变量和函数都会提升,如下 a = 2; var a ; console.log(a); fn(); function fn(){ console.log(1); } 2 ...
- <Android 基础(三十五)> RecyclerView多类型Item的正确实现姿势
简介 RecyclerView是我们开发过程中经常使用到的一个元素,原生的RecyclerView.Adapter基本上可以满足一般的需求,关于RecyclerView的基础介绍请移步: Recycl ...
- PeopleSoft面试题(服务器相关)
如何配置app服务器与web服务器的负载均衡?请详细说明. App Server: 在配置App Server负载均衡时候,通过webserv目录下的configuration.properties文 ...
- hadoop中实现java网络爬虫
这一篇网络爬虫的实现就要联系上大数据了.在前两篇java实现网络爬虫和heritrix实现网络爬虫的基础上,这一次是要完整的做一次数据的收集.数据上传.数据分析.数据结果读取.数据可视化. 需要用到 ...
- React Native - TextInput详细解说
1,TextInput组件介绍 TextInput 组件除了作为输入框实现基本的输入功能外,它还提供了许多其他功能,比如自动校验.占位符以及指定弹出不同的键盘类型等. 2,组件的属性 (1)autoC ...
- 单元测试,模拟用户Get登陆,并携带登录后的token访问接口
HttpClient _httpClient; HttpClient _businessHttpClient; private async Task<string> GetAccessTo ...