POJ1459Power Network(dinic模板)
| Time Limit: 2000MS | Memory Limit: 32768K | |
| Total Submissions: 25832 | Accepted: 13481 |
Description

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
Output
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
#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模板)的更多相关文章
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- hdu 1532 Dinic模板(小白书)
hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...
- 最大流算法 ISAP 模板 和 Dinic模板
ISAP // UVa11248 Frequency Hopping:使用ISAP算法,加优化 // Rujia Liu struct Edge { int from, to, cap, flow; ...
- 洛谷P3376【模板】网络最大流 Dinic模板
之前的Dinic模板照着刘汝佳写的vector然后十分鬼畜跑得奇慢无比,虽然别人这样写也没慢多少但是自己的就是令人捉急. 改成邻接表之后快了三倍,虽然还是比较慢但是自己比较满意了.虽然一开始ecnt从 ...
- Power Network POJ - 1459 网络流 DInic 模板
#include<cstring> #include<cstdio> #define FOR(i,f_start,f_end) for(int i=f_startl;i< ...
- HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【网络流#3】hdu 1532 - Dinic模板题
输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...
- 最大流当前弧优化Dinic模板
最大流模板: 普通最大流 无向图限制:将无向图的边拆成2条方向相反的边 无源汇点有最小流限制的最大流:理解为水管流量形成循环,每根水管有流量限制,并且流入量等于流出量 有源汇点的最小流限制的最大流 顶 ...
- 网络流--最大流dinic模板
标准的大白书式模板,除了变量名并不一样……在主函数中只需要用到 init 函数.add 函数以及 mf 函数 #include<stdio.h> //差不多要加这么些头文件 #includ ...
随机推荐
- Gitblit-Git版本服务器环境部署
Gitblit介绍 Gitblit 是一个纯 Java 库用来管理.查看和处理 Git 资料库.相当于 Git 的 Java 管理工具,支持linux系统. Git是分布式版本控制系统,它强调速度.数 ...
- scala学习之第三天:数组的特性与使用技巧
1.数组 Scala数组与Scala序列是兼容的 - 在需要Seq[T]的地方可由Array[T]代替.最后,Scala数组支持所有的序列操作. 隐式转换 方法1:通过scala.collection ...
- [转]redis 五种数据类型的使用场景
FROM : http://blog.csdn.net/gaogaoshan/article/details/41039581#t5 String 1.String 常用命令: 除了get.set.i ...
- C#中的bitmap类和图像像素值获取方法
一.Bitmap类 Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成.因此Bitmap是用于处理由像素数据定义的图像的对象.该类的主要方法和属性如下: 1. GetP ...
- 添加web引用和添加服务引用有什么区别?
添加web引用和添加服务引用有什么区别,Add Service References 和 Add Web References 有啥区别?参考 http://social.microsoft.com/ ...
- 在opencv3中进行图片人脸检测
在opencv中,人脸检测用的是harr或LBP特征,分类算法用的是adaboost算法.这种算法需要提前训练大量的图片,非常耗时,因此opencv已经训练好了,把训练结果存放在一些xml文件里面.在 ...
- [git]图解git常用命令
本文图解git中最常用的命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透彻. 基本用法 约定 命令详解 Diff Commit Checkout Detached HEAD(匿名分支提 ...
- 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; ...
- [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 ...
- 实验一报告 20135238&20135207
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1352 姓名:(按贡献大小排名)龚睿 王国伊 学号:(按贡献大小排 ...