http://poj.org/problem?id=1459

题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量。

思路:S和发电站相连,边权是产能上限,消费者和T相连,边权是需求上限,边的话就按题意加就好了。难点更觉得在于输入。。加个空格。。边数组要*2,因为有反向边。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define N 110
typedef long long LL;
struct Edge {
int v, cap, nxt;
Edge () {}
Edge (int v, int cap, int nxt) : v(v), cap(cap), nxt(nxt) {}
}edge[N*N*];
int tot, cur[N], dis[N], pre[N], head[N], gap[N], S, T; void Add(int u, int v, int cap) {
edge[tot] = Edge(v, cap, head[u]); head[u] = tot++;
edge[tot] = Edge(u, , head[v]); head[v] = tot++;
} void BFS() {
queue<int> que;
while(!que.empty()) que.pop();
memset(dis, -, sizeof(dis));
memset(gap, , sizeof(gap));
dis[T] = ; gap[]++; que.push(T);
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(dis[v] == -) continue;
dis[v] = dis[u] + ;
gap[dis[v]]++;
que.push(v);
}
}
} int ISAP(int n) {
memcpy(cur, head, sizeof(cur));
BFS();
int u = pre[S] = S, ans = , i;
while(dis[S] < n) {
if(u == T) {
int flow = INF, index;
for(int i = S; i != T; i = edge[cur[i]].v) {
Edge& e = edge[cur[i]];
if(e.cap < flow) {
flow = e.cap; index = i;
}
}
for(int i = S; i != T; i = edge[cur[i]].v) {
edge[cur[i]].cap -= flow;
edge[cur[i]^].cap += flow;
}
ans += flow; u = index;
}
for(i = cur[u]; ~i; i = edge[i].nxt)
if(dis[edge[i].v] == dis[u] - && edge[i].cap > )
break;
if(~i) {
cur[u] = i;
pre[edge[i].v] = u;
u = edge[i].v;
} else {
int md = n;
if(--gap[dis[u]] == ) break;
for(int i = head[u]; ~i; i = edge[i].nxt) {
if(md > dis[edge[i].v] && edge[i].cap > ) {
md = dis[edge[i].v]; cur[u] = i;
}
}
++gap[dis[u] = md + ];
u = pre[u];
}
}
return ans;
} int main() {
int n, nc, np, m;
while(~scanf("%d%d%d%d", &n, &np, &nc, &m)) {
tot = ; memset(head, -, sizeof(head));
S = n, T = n + ;
int a, b, c;
for(int i = ; i <= m; i++) {
scanf(" (%d,%d)%d", &a, &b, &c);
Add(a, b, c);
}
for(int i = ; i <= np; i++) {
scanf(" (%d)%d", &a, &b);
Add(S, a, b);
}
for(int i = ; i <= nc; i++) {
scanf(" (%d)%d", &a, &b);
Add(a, T, b);
}
printf("%d\n", ISAP(T+));
}
return ;
}

POJ 1459:Power Network(最大流)的更多相关文章

  1. POJ 1459 Power Network 最大流(Edmonds_Karp算法)

    题目链接: http://poj.org/problem?id=1459 因为发电站有多个,所以需要一个超级源点,消费者有多个,需要一个超级汇点,这样超级源点到发电站的权值就是发电站的容量,也就是题目 ...

  2. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  3. poj 1459 Power Network

    题目连接 http://poj.org/problem?id=1459 Power Network Description A power network consists of nodes (pow ...

  4. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

  5. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  6. 网络流--最大流--POJ 1459 Power Network

    #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #incl ...

  7. poj 1459 Power Network : 最大网络流 dinic算法实现

    点击打开链接 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20903   Accepted:  ...

  8. poj 1459 Power Network【建立超级源点,超级汇点】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25514   Accepted: 13287 D ...

  9. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

  10. POJ - 1459 Power Network(最大流)(模板)

    1.看了好久,囧. n个节点,np个源点,nc个汇点,m条边(对应代码中即节点u 到节点v 的最大流量为z) 求所有汇点的最大流. 2.多个源点,多个汇点的最大流. 建立一个超级源点.一个超级汇点,然 ...

随机推荐

  1. JDBC详解(转)

    原文链接:http://blog.csdn.net/cai_xingyun/article/details/41482835 什么是JDBC? Java语言访问数据库的一种规范,是一套API JDBC ...

  2. php count()函数用法 及其 一个坑

    用法 count() 函数返回数组中元素的数目. count(array,mode); [mode] 0 - 默认.不计算多维数组中的所有元素. 1 - 递归地计算数组中元素的数目(计算多维数组中的所 ...

  3. imx6 DDR_Stress_Test

    在调试DDR的时候,有时候需要更改参数.今天发现NXP提供了DDR Stress Test工具,用于DDR参数的校准. 参考链接 http://blog.csdn.net/qq405180763/ar ...

  4. 使用HttpClient连接池进行https单双向验证

    https单双向验证环境的搭建参见:http://www.cnblogs.com/YDDMAX/p/5368404.html 一.单向握手 示例程序: package com.ydd.study.he ...

  5. xargs -I

    xargs  -i 参数或者-I参数配合{}即可进行文件的操作.   -I replace-str              Replace  occurrences  of  replace-str ...

  6. AFN和ASI区别

    AFN和ASI区别 一.AFN和ASI的区别 1.底层实现1> AFN的底层基于OC的NSURLConnection和NSURLSession2> ASI的底层基于纯C语言的CFNetwo ...

  7. Python之路----------random模块

    随机数模块: import random #随机小数 print(random.random()) #随机整数 print(random.randint(1,5))#他会打印5 #随机整数 print ...

  8. https 与http 的坑

    网页报这种错误: (blocked:mixed-content) 使用了https就不能夹生http       jquery.min.js:4Mixed Content: The page at ' ...

  9. 【转】Apache Digest验证

    Apache默认使用basic模块验证,都是明文传输,不太安全,所以本文使用Digest来验证,以提高安全性. 1.Apache配置: A.目录权限配置 Alias /nagios "/us ...

  10. 初识Redis(1)

    Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store). 它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希( ...