【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 ...
随机推荐
- WWW压缩解压缩
unity的WWW参考文档:http://game.ceeger.com/Script/WWW/WWW.html 在unity中把资源打包成Assetbundle其实把资源通过 LZMA 压缩成二进制 ...
- 【markdown】markdown常用语法
标题 用1~6个# 标题1 标题2 标题3 标题4 标题5 标题6 源码: # 标题1 ## 标题2 ### 标题3 #### 标题4 ##### 标题5 ###### 标题6 引用 引用使用 引用使 ...
- UltraISO制作U盘启动盘安装Win7/10系统攻略
UltraISO制作U盘启动盘安装Win7/9/10系统攻略 U盘安装好处就是不用使用笨拙的光盘,光盘还容易出现问题,无法读取的问题.U盘体积小,携带方便,随时都可以制作系统启动盘. U盘建议选择8G ...
- Jsp c标签数值格式化
整数带千分符显示:<fmt:formatNumber value="${num}" type="number"/> 整数显示:<fmt:for ...
- CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… 第1种效果: 代码如下: <div class="loading"> ...
- extjs ajax请求与struts2进行交互
sencha extjs 5 增加一个struts2的配置,这样可以在设置好前台布局之后,与后台交互获取数据显示.现在有一个问题是struts2对于url的跳转action支 持比较良好,但是对于像E ...
- WPF数据绑定Binding(二)
WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...
- C语言 函数理解(以数组做参数)
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int run(int *p){ // ...
- iBatis.Net(C#)数据库查询
引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html 摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...
- Android Drawable的9种子类 介绍
原文: Android Drawable的9种子类 介绍 Drawable 在android里面 就是代表着图像,注意是图像 而不是图片. 图片是图像的子集.图像除了可以包含图片以外 还可以包含颜 ...