HDU3549:Flow Problem(最大流入门EK)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
int map[][],v[],pre[];
int n,m;
int bfs(int s,int t)
{
memset(v,,sizeof(v));
memset(pre,-,sizeof(pre));
pre[s]=s;
queue<int>q;
q.push(s);
v[s]=;
while(!q.empty())
{
int tt=q.front();
q.pop();
for(int i=; i<=n; i++)
{
if(map[tt][i]&&v[i]==)
{
v[i]=;
pre[i]=tt;
q.push(i);
if(i==t)
{
return ;
}
}
}
}
return ;
}
int EK(int s,int t)
{
int ans=;
while(bfs(s,t)==)
{
int min1=inf;
for(int i=t; i!=s; i=pre[i])
{
if(min1>map[pre[i]][i])
{
min1=map[pre[i]][i];
}
}
for(int i=t; i!=s; i=pre[i])
{
map[pre[i]][i]-=min1;
map[i][pre[i]]+=min1;
}
ans+=min1;
}
return ans;
}
int main()
{
int T,x,y,z;
scanf("%d",&T);
for(int i=; i<=T; i++)
{
scanf("%d%d",&n,&m);
memset(map,,sizeof(map));
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
map[x][y]+=z;
}
printf("Case %d: %d\n",i,EK(,n));
EK(,n);
}
return ;
}
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,tt;
struct node
{
int x,y,c;
int next;
}edge[10001];
int head[101],dis[101];
void init()
{
memset(head,-1,sizeof(head));
tt=0;
}
void add(int xx,int yy,int zz)
{
edge[tt].x=xx;
edge[tt].y=yy;
edge[tt].c=zz;
edge[tt].next=head[xx];
head[xx]=tt++;
edge[tt].x=yy;
edge[tt].y=xx;
edge[tt].c=0;
edge[tt].next=head[xx];
head[xx]=tt++;
}
int bfs(int s,int t)
{
queue<int>q;
memset(dis,-1,sizeof(dis));
q.push(s);
dis[s]=0;
while(!q.empty())
{
int w=q.front();
q.pop();
for(int i=head[w];i!=-1;i=edge[i].next)
{
if(dis[edge[i].y]==-1&&edge[i].c>0)
{
dis[edge[i].y]=dis[w]+1;
q.push(edge[i].y);
}
}
}
if(dis[t]>=0) return 1;
return 0;
}
int dinic(int x,int maxt)
{
int a;
if(x==n) return maxt;
for(int i=head[x];i!=-1;i=edge[head[x]].next)
{
if(dis[edge[i].y]==x+1&&edge[i].c>0&&(a==min(maxt,edge[i].c)))
{
,mhgr
}
}
}
int main()
{
int T,xx,yy,zz,ans;
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
init();
ans=0;
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&xx,&yy,&zz);
add(xx,yy,zz);
}
while(bfs(1,n)==1)
{
ans+=dinic(1,inf);
}
printf("Case %d: %d\n",i,ans);
}
return 0;
}
HDU3549:Flow Problem(最大流入门EK)的更多相关文章
- [hdu3549]Flow Problem(最大流模板题)
解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...
- Hdu3549 Flow Problem 2017-02-11 16:24 58人阅读 评论(0) 收藏
Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a ...
- Flow Problem(最大流模板)
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- HDU3549 Flow Problem 【最大流量】
Flow Problem Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- hdu-3549 Flow Problem---最大流模板题(dinic算法模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...
- hdu 3549 Flow Problem (最大流)
裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...
- HDU3549 Flow Problem(网络流增广路算法)
题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...
- hdu 3549 Flow Problem 最大流 Dinic
题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...
随机推荐
- element之input输入搜索联想框
1. 模板代码 <el-autocomplete :minlength="2" v-model="searchName" :fetch-suggestio ...
- 第二章、Django以及数据库的配置
目录 第二章.Django以及数据库的配置 一.小白必会三板斧 二.静态文件配置 三.form表单 action和method参数可以写的形式 四.request对象及方法 五.django连接数据库 ...
- 配置Linux内核
- touchgfx -- Integration
将UI连接到系统 在大多数应用程序中,UI需要以某种方式连接到系统的其余部分,并发送和接收数据.这可以与硬件外围设备(传感器数据,A / D转换,串行通信等)接口,也可以与其他软件模块接口. 本文介绍 ...
- 六:MVC数据建模(增删改查)
今天我们来学习mvc增删改查等操作(试着结合前面学习的LINQ方法语法结合查询) 我创建了一个car的数据库,只有一个Cars表 表里面就几个字段 插入了一些数据 想要创建一个ADO.NET实体数据模 ...
- JDBC连接数据库报错:Loading class `com.mysql.jdbc.Driver'. This is deprecated.
使用JDBC连接数据库时出现报错, 报错内容:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver cla ...
- 01 js数据类型
1.不管什么语言,上来就应该是数据类型了.js也不例外.那么基本的数据类型我们有,boolean, number, string, null, undefine, symbol, object, fu ...
- ftp定时下载指定目录或文件脚本
#! /bin/bash rpm -qa lftp &>/dev/null || yum install -y lftp lftp 160.106.0.34 << EOF c ...
- SPOJ 10707 COT2 - Count on a tree II
思路 树上莫队的题目 每次更新(u1,u2)和(v1,v2)(不包括lca)的路径,最后单独统计LCA即可 代码 #include <cstdio> #include <cstrin ...
- Error creating bean with name 'documentationPluginsBootstrapper' defined in URL
启动报错 Error starting ApplicationContext. To display the auto-configuration report re-run your applica ...