HDU 4280Island Transport(网络流之最大流)
题目地址: pid=4280">http://acm.hdu.edu.cn/showproblem.php? pid=4280
这个题是一个纯最大流模板题。。就是用来卡时间的。。
还好我会isap算法。。可是坑爹的是一直WA。最后加了个输入优化就过了。。。(不过把输入改了改而已。
。
)至今不知道为什么。。请好心的看到此博客的大神赐教。。
代码例如以下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int head[210000], s, t, nv, maxint=0x3f3f3f3f, cnt;
int cur[210000], pre[210000], q[5000000], d[210000], num[210000];
struct node
{
int u, v, cap, next;
}edge[11000000];
void add(int u, int v, int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;
edge[cnt].next=head[u];
head[u]=cnt++; edge[cnt].v=u;
edge[cnt].cap=cap;
edge[cnt].next=head[v];
head[v]=cnt++;
}
void bfs()
{
memset(num,0,sizeof(num));
memset(d,-1,sizeof(d));
int f1=0, f2=0, i;
q[f1++]=t;
d[t]=0;
num[0]=1;
while(f1>=f2)
{
int u=q[f2++];
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(d[v]==-1)
{
d[v]=d[u]+1;
num[d[v]]++;
q[f1++]=v;
}
}
}
}
void isap()
{
memcpy(cur,head,sizeof(cur));
int flow=0, u=pre[s]=s, i;
bfs();
while(d[s]<nv)
{
if(u==t)
{
int f=maxint, pos;
for(i=s;i!=t;i=edge[cur[i]].v)
{
if(f>edge[cur[i]].cap)
{
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=s;i!=t;i=edge[cur[i]].v)
{
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
//printf("--");
u=pos;
}
for(i=cur[u];i!=-1;i=edge[i].next)
{
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
{
break;
}
}
if(i!=-1)
{
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
}
else
{
if(--num[d[u]]==0) break;
int mind=nv+1;
for(i=head[u];i!=-1;i=edge[i].next)
{
if(mind>d[edge[i].v]&&edge[i].cap)
{
mind=d[edge[i].v];
cur[u]=i;
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}
}
printf("%d\n",flow);
}
int read(){
int flag = 0, x = 0;
char ch = ' ';
while(ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
if(ch == '-') flag = 1, ch = getchar();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return flag ? -x : x;
}
int main()
{
int T, n, m, i, j, a, b, c, x, y, min1, max1;
scanf("%d",&T);
while(T--)
{
memset(head,-1,sizeof(head));
cnt=0;
n=read();m=read();
min1=999999;
max1=-999999;
for(i=1;i<=n;i++)
{
x=read();y=read();
if(min1>x)
{
min1=x;
s=i;
}
if(max1<x)
{
max1=x;
t=i;
}
}
while(m--)
{
a=read();
b=read();
c=read();
add(a,b,c);
}
nv=n+1;
isap();
}
return 0;
}
HDU 4280Island Transport(网络流之最大流)的更多相关文章
- HDU 3667 Transportation(网络流之费用流)
题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...
- HDU 4280Island Transport(Dinc非STL 模板)
题意: n岛m条路,然后是 n个岛的坐标,然后是m条双向路,包括 岛和 岛 之间 最大客流量,让求 最左边的岛 到右边的岛 最大客流量 分析: 建图 以 左边的岛为原点,最右边的为终点求最大客流量. ...
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- HDU 4289 Control (网络流,最大流)
HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU 3338 Kakuro Extension (网络流,最大流)
HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- HDU 4292 Food (网络流,最大流)
HDU 4292 Food (网络流,最大流) Description You, a part-time dining service worker in your college's dining ...
随机推荐
- Highcharts构建分组分类坐标轴
Highcharts构建分组分类坐标轴 分组分类坐标轴是将坐标轴的类别标签进行进一步分组,从而形成双层.多层结构. 这样更利于数据分组展现. 实现分组分类坐标轴须要借助第三方插件Grouped-Cat ...
- 支持Tasker控制的app合集
跟各种Tasker插件打交道,原因有两点: 1.站在开发者的角度:Tasker虽为神器,也不能面面俱到,一个原因就是Android自身过于分裂化造成的,不可能兼顾全平台和机型:个人开发者精力有限,也满 ...
- printf回到上一行开头以及回到本行开头的方法
回到上一行开头 #include <stdio.h> #include <unistd.h> int main(void) { ; ){ printf("%d\n&q ...
- debian下安装mysql
apt-get install mysql-client mysql-server 中间会要你设置password,设置后后就自己主动启动mysql了 能够用ps -ef|grep mysql 这样能 ...
- booksleeve 使用
By offering pipelined, asynchronous, multiplexed and thread-safe access to redis, BookSleeve enables ...
- 3DPrint ABS和PLA代码比较
PLA ABS
- ES6 中 Symbol.split的用法
class Split1 { constructor(value) { this.value = value; } [Symbol.split](string) { var index = strin ...
- 深入一点 让细节帮你和Fragment更熟络
有一段时间没有写博客了.作为2017年的第一篇,初衷起始于前段时间一个接触安卓开发还不算太长时间的朋友聊到的一个问题: "假设,想要对一个Fragment每次在隐藏/显示之间做状态切换时进行 ...
- angularjs库及ionic库下载地址
angularjs库下载地址: 下载地址:https://code.angularjs.org/ 引用地址:http://www.bootcdn.cn/angular.js/ ionic库下载地址: ...
- Project Euler 001-006 解法总结
Problem 1. Find the sum of all the multiples of 3 or 5 below 1000. 题目要求找出所有1000以下的3或者5的倍数之和. 最简便 ...