题意:一个电路板,上面有N个接线柱(标号1~N)   还有两个电源接线柱  +  - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流。

析:这是一个有源汇有上下界的最小流。

有源汇有上下界最大流:

1.构造附加网络

2.对ss、tt求最大流(ss、tt满流则有解)

3.若有解,对s、t求最大流

有源汇有上下界最小流:

1.构造附加网络(不添加[t,s]边)

2.对ss、tt求最大流

3.添加[t,s]边

4.对ss、tt求最大流

5.若ss、tt满流,则[t,s]的流量就是最小流

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 80 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int from, to, cap, flow;
}; struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int d[maxn];
bool vis[maxn];
int cur[maxn]; void init(int n){
this-> n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap){
edges.pb((Edge){from, to, cap, 0});
edges.pb((Edge){to, from, 0, 0});
m = edges.sz;
G[from].pb(m-2); G[to].pb(m-1);
} bool bfs(){
ms(vis, 0); vis[s] = 1;
d[s] = 1;
queue<int> q;
q.push(s); while(!q.empty()){
int x = q.front(); q.pop();
for(int i = 0; i < G[x].sz; ++i){
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
d[e.to] = d[x] + 1;
vis[e.to] = 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i < G[x].sz; ++i){
Edge &e = edges[G[x][i]];
if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
e.flow += f;
edges[G[x][i]^1].flow -= f;
a -= f;
flow += f;
if(a == 0) break;
}
}
return flow;
} int maxflow(int s, int t){
this->s = s; this-> t = t;
int flow = 0;
while(bfs()){ ms(cur, 0); flow += dfs(s, INF); }
return flow;
} void solve(int t){
for(int i = 0; i < G[s].sz; ++i){
Edge &e = edges[G[s][i]];
if(e.cap > e.flow){
puts("impossible");
return ;
}
}
printf("%d\n", edges[*G[t].rbegin()].flow);
}
}; Dinic dinic;
int in[maxn]; int main(){
while(scanf("%d %d", &n, &m) == 2 && n+m){
int s = n + 2, t = n + 3;
dinic.init(n + 10);
char a[15], b[15], c[15];
ms(in, 0);
for(int i = 0; i < m; ++i){
scanf("%s %s %s", a, b, c);
int u = a[0] == '+' ? 0 : atoi(a);
int v = b[0] == '-' ? n+1 : atoi(b);
int val = atoi(c);
in[v] += val;
in[u] -= val;
dinic.addEdge(u, v, INF);
}
for(int i = 0; i <= n+1; ++i){
if(in[i] > 0) dinic.addEdge(s, i, in[i]);
if(in[i] < 0) dinic.addEdge(i, t, -in[i]);
}
dinic.maxflow(s, t);
dinic.addEdge(n+1, 0, INF);
dinic.maxflow(s, t);
dinic.solve(n+1);
}
return 0;
}

  

Crazy Circuits

HDU 3157 Crazy Circuits (有源汇上下界最小流)的更多相关文章

  1. hdu 3157 Crazy Circuits 有源汇和下界的最小费用流

    题目链接 题意:有n个节点,m个用电器.之后输入m行每行三个整数a,b,c; 节点a为正极(或者a 为 '+'即总的正极),b为该用电器的负极(b = '-'表示总的负极),c为该用电器要正常工作最小 ...

  2. BZOJ_2502_清理雪道_有源汇上下界最小流

    BZOJ_2502_清理雪道_有源汇上下界最小流 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...

  3. 【Loj117】有源汇上下界最小流(网络流)

    [Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...

  4. hdu3157有源汇上下界最小流

    题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题 #include<bits/stdc++.h> #define fi first #define se second #defi ...

  5. BZOJ 2502 清理雪道(有源汇上下界最小流)

    题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...

  6. BZOJ 2502 清理雪道/ Luogu P4843 清理雪道 (有源汇上下界最小流)

    题意 有一个有向无环图,求最少的路径条数覆盖所有的边 分析 有源汇上下界最小流板题,直接放代码了,不会的看dalao博客:liu_runda 有点长,讲的很好,静心看一定能看懂 CODE #inclu ...

  7. sgu176 有源汇上下界最小流

    题意:有一堆点和边,1起点,n终点,某些边有可能必须满流,要求满足条件的最小流 解法:按原图建边,满流的即上下界都是容量,但是这样按有源汇上下界可行流求出来的可能不是最小流,那么我们需要开始建边的时候 ...

  8. HDU 3157 Crazy Circuits(有源汇上下界最小流)

    HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...

  9. SGU 176 Flow construction(有源汇上下界最小流)

    Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...

随机推荐

  1. 【linux】head&&tail

    命令:     head[-n][文件名]                                    tail[-n][-f][文件名] 形式:     -n  行数 (显示前几行)    ...

  2. ROS多线接入和多线对外提供服务的完整做法,谁进谁出,电信进电信出,联通进联通出,移动进移动出

    1.网卡接入: 电信移动联通,三线接入,LAN是局域网. 5.从www.tcp5.com,下载联通和移动的路由表,并导入.这边简单说下导入步骤,下载rsc文件,上传到ROS的FTP上,然后用命令imp ...

  3. java之RestTemplate的访问应用

    一.REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.它是 ...

  4. HttpContext.Current并非无处不在

    阅读目录 开始 无处不在的HttpContext HttpContext.Current到底保存在哪里? HttpContext并非无处不在! 如何获取文件绝对路径? 异步调用中如何访问HttpCon ...

  5. modelsim 仿真xilinx fir ip

    到现在不管fir ip 用的对不对,但是在使用modelsim是可以仿真fir ip的. 具体步骤: 1.仿真库,添加到modelsim目录配置文件: 2.将这个文件中的: :List of dyna ...

  6. 利用IOS画图功能画出五角星,并且可以调整五角星的填充范围

    我们要花的为一个黄色的五角星并且其中的填充黄色能够任意调整,比如只填满半个五角星,或者只填满一个角等等. 首先要重写DrawRect 方法,然后在这里实现我们的画图代码. - (void)drawRe ...

  7. bom知识点

    1.BOM输出 所谓BOM指的是浏览器对象模型 Browser Object Model,它的核心就是浏览器 alert(1);//弹出框 调式使用 console.log('路飞学城');//用于浏 ...

  8. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #9 RT Group Scheduling 与RT Throttling

    HACK #9 RT Group Scheduling 与RT Throttling 本节介绍对实时进程所使用的CPU时间进行限制的功能RT Group Scheduling和RT Throttlin ...

  9. 1.1_C语言概述

    C语言概述 1.1 什么是C语言 一提到语言这个词语,自然会想到的是像英语.汉语等这样的自然语言,因为它是人和人交换信息不可缺少的工具. 而今天计算机遍布了我们生活的每一个角落,除了人和人的相互交流之 ...

  10. VsCode中vim插件剪切板等问题

    剪切板共享 这个挺重要的,否则每次右键菜单复制粘贴会奔溃的. 在用户设置中添加: "vim.useSystemClipboard": true, 光标的变化 我觉得这个也重要,毕竟 ...