Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 25832   Accepted: 13481

Description

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= lmax(u,v) of power delivered by u to v. Let Con=Σuc(u) be the power consumed in the net. The problem is to compute the maximum value of Con. 

An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6. 

Input

There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of cmax(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

Sample Input

2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4

Sample Output

15
6

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.
 
主要是学习dinic算法
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
const int MAX = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,cap;
Edge(int v,int w):to(v),cap(w) {}
};
int n,m,np,nc,s,t;
vector<int> g[MAX];
vector<Edge> edge;
int d[MAX],cur[MAX];
void AddEdge(int from,int to,int cap)
{
edge.push_back(Edge(to,cap));
edge.push_back(Edge(from,));
int id = edge.size();
g[from].push_back(id - );
g[to].push_back(id - ); }
bool bfs()
{
memset(d,,sizeof(d));
queue<int> q;
q.push(s);
d[s] = ;
while(!q.empty())
{
int x = q.front();
q.pop();
if(x == t)
return true;
int len = g[x].size();
for(int i = ; i < len; i++)
{
Edge e = edge[ g[x][i] ];
if(d[e.to] == && e.cap > )
{
d[e.to] = d[x] + ;
q.push(e.to);
}
}
}
return false;
}
int dfs(int x, int a)
{
if(x == t || a == )
return a;
int flow = ,f;
for(int& i = cur[x]; i < (int) g[x].size(); i++)
{
Edge& e = edge[ g[x][i] ]; //这里要是引用
if(d[x] + == d[e.to] && (f = dfs(e.to,min(a,e.cap))) > )
{
e.cap -= f;
edge[ g[x][i] ^ ].cap += f;
flow += f;
a -= f;
if(a == )
{
break;
}
}
}
return flow;
}
int MaxFlow()
{
int flow = ;
while(bfs())
{
memset(cur,,sizeof(cur));
flow += dfs(s,INF);
}
return flow;
}
int main()
{
char str[];
int u,v,w;
while(scanf("%d%d%d%d",&n,&np,&nc,&m) != EOF)
{
s = n + ;
t = n + ;
for(int i = ; i < n + ; i++)
g[i].clear();
edge.clear();
for(int i = ; i <= m; i++)
{
scanf("%s",str);
sscanf(str,"%*c%d%*c%d%*c%d",&u,&v,&w);
AddEdge(u,v,w);
}
for(int i = ; i < np; i++)
{
scanf("%s",str);
sscanf(str,"%*c%d%*c%d",&u,&w);
AddEdge(s,u,w);
}
for(int i = ; i < nc; i++)
{
scanf("%s",str);
sscanf(str,"%*c%d%*c%d",&u,&w);
AddEdge(u,t,w);
}
printf("%d\n",MaxFlow());
} return ;
}

POJ1459Power Network(dinic模板)的更多相关文章

  1. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  2. hdu 1532 Dinic模板(小白书)

    hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...

  3. 最大流算法 ISAP 模板 和 Dinic模板

    ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...

  4. 洛谷P3376【模板】网络最大流  Dinic模板

    之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...

  5. Power Network POJ - 1459 网络流 DInic 模板

    #include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...

  6. HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 【网络流#3】hdu 1532 - Dinic模板题

    输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...

  8. 最大流当前弧优化Dinic模板

    最大流模板: 普通最大流 无向图限制:将无向图的边拆成2条方向相反的边 无源汇点有最小流限制的最大流:理解为水管流量形成循环,每根水管有流量限制,并且流入量等于流出量 有源汇点的最小流限制的最大流 顶 ...

  9. 网络流--最大流dinic模板

    标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...

随机推荐

  1. [原创]CI持续集成系统环境---部署Gitlab环境完整记录

    Gitlab是一个代码托管平台,在实际工作中,对代码管理十分有用. 废话不多说,下面是对我自己搭建的Gitlab环境做一记录: (1)安装 ------------------------------ ...

  2. Python-json 和 pickle

    这是用于序列化的两个模块 json:用于字符串和python数据类型间进行转换 pickle:用于python特有的类型和python的数据类型间进行转换 json模块提供了四个功能:dumps du ...

  3. 你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  4. svn使用过程forMac

    在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...

  5. 使用ajax跨域withCredentials的作用

    默认情况下,跨源请求不提供凭据(cookie.HTTP认证及客户端SSL证明等).通过将withCredentials属性设置为true,可以指定某个请求应该发送凭据.如果服务器接收带凭据的请求,会用 ...

  6. QuickFIX/J常见问题汇总

    最近在搞QuickFIX/J,网上的资料不算很多,遇到一些简单的问题都需要google一阵才能找到解决方法,因此做点记录: 错误:Rejecting invalid message: quickfix ...

  7. Java集合---面试题

    HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此 ...

  8. Linux常用的基本命令

    man命令:查看帮助信息                       格式:man  需要查看的命令 date命令:显示时间                            格式:# date ...

  9. Java实验四 TCP客户端和服务器的应用

    实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全 4.对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送: 发送方A——————>接收方B A加密时,用B ...

  10. mysql使用基础 sql语句与数据完整性(二)

    二.DML:Data Manipulation Language 数据操作语言 作用:操作表中的数据的. 关键:INSERT UPDATE DELETE 注意:日期或字符串.字符要使用单引号引起来. ...