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. Gitblit-Git版本服务器环境部署

    Gitblit介绍 Gitblit 是一个纯 Java 库用来管理.查看和处理 Git 资料库.相当于 Git 的 Java 管理工具,支持linux系统. Git是分布式版本控制系统,它强调速度.数 ...

  2. scala学习之第三天:数组的特性与使用技巧

    1.数组 Scala数组与Scala序列是兼容的 - 在需要Seq[T]的地方可由Array[T]代替.最后,Scala数组支持所有的序列操作. 隐式转换 方法1:通过scala.collection ...

  3. [转]redis 五种数据类型的使用场景

    FROM : http://blog.csdn.net/gaogaoshan/article/details/41039581#t5 String 1.String 常用命令: 除了get.set.i ...

  4. C#中的bitmap类和图像像素值获取方法

    一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetP ...

  5. 添加web引用和添加服务引用有什么区别?

    添加web引用和添加服务引用有什么区别,Add Service References 和 Add Web References 有啥区别?参考 http://social.microsoft.com/ ...

  6. 在opencv3中进行图片人脸检测

    在opencv中,人脸检测用的是harr或LBP特征,分类算法用的是adaboost算法.这种算法需要提前训练大量的图片,非常耗时,因此opencv已经训练好了,把训练结果存放在一些xml文件里面.在 ...

  7. [git]图解git常用命令

    本文图解git中最常用的命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透彻. 基本用法 约定 命令详解 Diff Commit Checkout Detached HEAD(匿名分支提 ...

  8. matlab建立双坐标

    (1)设定双Y坐标 x=0:0.1:2*pi; y1=sin(x); y2=cos(x); y3=1-sin(x); [AX]=plotyy(x,y1,x,y2); %双Y坐标的建立 hold on; ...

  9. [CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线

    7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in ha ...

  10. 实验一报告 20135238&20135207

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础              班级:1352 姓名:(按贡献大小排名)龚睿  王国伊 学号:(按贡献大小排 ...