【POJ 1273】Drainage Ditches(网络流)
一直不明白为什么我的耗时几百毫秒,明明差不多的程序啊,我改来改去还是几百毫秒。
...一个小时后:明白了,原来把最大值0x3f(77)取0x3f3f3f3f就把时间缩短为16ms了。可是为什么原来那样没有WA呢?哦,明白了,因为最大值写小了,dicnic会多跑几遍。
嗯顺便:0x3f3f3f3f也就是1061109567,也就是10^9级别的,是一个很大的数,而且加上一个10^9级别以下的数字也不会溢出。所以很多时候最大值取这个会比较好。最大的int是0x7fffffff,可以用常量INT_MAX 代替。
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int arc[][], d[], q[];
int m, n, ans,tans;
bool bfs()
{
memset(d, -, sizeof d);
queue<int>q;
q.push();
d[] = ;
while(!q.empty())
{
int i,k=q.front();
q.pop();
for(i = ; i <= n; i++)
if(arc[k][i] > && d[i] == -)
{
d[i] = d[k] + ;
q.push(i);
}
}
return d[n]>;
}
int dinic (int k, int low)
{
if(k == n)return low;
int a,i;
for(i = ; i <= n; i++)
if(d[i] == d[k] + && arc[k][i] > &&(a = dinic(i, min(low, arc[k][i]))))
{
arc[k][i] -= a;
arc[i][k] += a;
return a;
}
return ;
}
int main()
{
while(~scanf("%d%d", &m, &n))
{
memset(arc, , sizeof arc);
for(int i = ; i < m; i++)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
arc[u][v] += c;
}
ans = ;
while(bfs())
while(tans=dinic(, 0X3f3f3f3f)) ans += tans;
printf("%d\n",ans);
}
return ;
}
【POJ 1273】Drainage Ditches(网络流)的更多相关文章
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
随机推荐
- NUL 与 NULL
NUL 与 NULL 在C语言中,字符串表示为字符的数组.字符串最后一个字符为空字符 ('\0'),官方将其定义为 NUL ,而 NULL 是一个宏,其定义如下: #define NULL ((voi ...
- python中如何将str转换成dict
>>>user "{'a':'b'}" >>>b = eval(user) >>>b {'a':'b'}
- Hibernate之Query接口的uniqueResult()方法
如果查询返回多个值用list()方法 public void testQuery(){ Configuration config = new Configuration().configure(); ...
- WPF窗口阴影和夜间模式的实现
窗口阴影 实现 因项目需要给用户一定提示,设计师建议在鼠标进入时显示窗口阴影,离开时取消窗口阴影. 很自然,都会想到直接在窗口的内容或者自定义窗口的最外层元素上加效果.示例如下: <Grid&g ...
- java 16 -4 LinkedList的特有功能
了解 LinkedList的特有功能: A:添加功能 public void addFirst(Object e) public void addLast(Object e) B:获取功能 publi ...
- android strings.xml 报 is not translated in af,
57 down vote In your ADT go to window->Preferences->Android->Lint Error Checking Find there ...
- Android Studio系列教程一--下载和安装
原文链接:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 背景 相信大家对Android Studio已经不陌生 ...
- 继承进一步使用,以及super关键字
目标: 1)掌握子类对象实例化过程 2)掌握方法覆盖概念和实现. 3)掌握super关键字的作用. 一,子类对象实例化过程 子类对象在实例化之前,必须先调用父类中的构造方法,再调用子类中的构造方法. ...
- Gvim使用
VIM删除空白行 命令:g/^\s*$/d :g 代表在全文档范围内 ^代表行的开始 \s*代表空白字符 &代表行的结束 d代表删除 用//将3段代码隔开
- 公钥(Public Key)与私钥(Private Key)
公钥(Public Key)与私钥(Private Key)是通过一种算法得到的一个密钥对(即一个公钥和一个私钥),公钥是密钥对中公开的部分,私钥则是非公开的部分.公钥通常用于加密会话密钥.验证数字签 ...