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. linux下清除Squid缓存的方法记录

    在日常运维工作中,只要用到squid缓存服务,就会常常被要求清理squid缓存.比如公司领导要求删一篇新闻,新闻是生成的静态.运维人员把服务器上静态的新闻页面删除了后,不料代理服务器上缓存还有.缓存服 ...

  2. WPF登陆窗口、主窗口切换问题

    代码 MainWindow mwin = new MainWindow(); Application.Current.MainWindow = mwin; this.Close(); mwin.Sho ...

  3. usb驱动开发1之学习准备

    此系列是http://blog.csdn.net/fudan_abc/博文的整理,同时加入了自己的理解.很敬佩fudan_abc的文章,仔细学习和分析受益很多.注:fundan_abc所分析linux ...

  4. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  5. Linux Linux程序练习十二(select实现QQ群聊)

    //头文件--helper.h #ifndef _vzhang #define _vzhang #ifdef __cplusplus extern "C" { #endif #de ...

  6. no.5.print sum

    #-*-coding=utf-8-*- for a in range(1,50,1): for b in range(1,50,1): for c in range(1,50,1): if a+b+c ...

  7. win7(X64)系统下cuda7.5和VS2013的配置

    &1 安装 cuda7.5文件:链接:http://pan.baidu.com/s/1bU2zIQ 密码:nvyw &2 环境变量 注意:CUDA_PATH是安装好cuda7.5之后会 ...

  8. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  9. TPLINK GPL code 简要分析

    从TPLINK官网下载了GPL code,下载后文件名是wr841nv9_en_gpl.tar.gz, 但是无论是linux还是windows下解压都提示压缩包有问题,不过还是可以解压出完整的目录的. ...

  10. 20145215《Java程序设计》第8周学习总结

    20145215<Java程序设计>第八周学习总结 教材学习内容总结 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以设定缓冲区(Bu ...