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& ...
随机推荐
- 【转】SpringBoot+SpringCloud实现登录用户信息在微服务之间的传递
实现思路: 1:准备一个ThreadLocal变量,供线程之间共享. 2:每个微服务对所有过来的Feign调用进行过滤,然后从请求头中获取User用户信息,并存在ThreadLocal变量中. 3:每 ...
- 3.第一个MyBatis程序_进化
1.使用工具类 将SqlSession的获取 封装成一个工具 private static SqlSession session = null; static { try { InputStream ...
- 微信公众号开发(三)—— access_token的管理
上一篇 微信公众号开发(二)—— 微信公众平台接入 让我们的本地工程顺利的接入到微信公众号系统, 那么接下啦我们介绍一个很重要的感念——acess_token (access_token是公众号的全局 ...
- GenericJDBCException: could not execute statement 错误解决
"message":"could not execute statement; nested exception is org.hibernate.exception.G ...
- table 随着内容自动适应宽度
td { white-space: nowrap; } 给td加个属性就可以了,如果有th则可以 td,th 本文来自:https://blog.csdn.net/liuhongwei_study/a ...
- HTML中dl元素的高度问题
dl元素通常用来创建一个描述列表,但是在我使用的过程中发现了一个小问题. 定义及用法 在MDN中 <dl> 元素的定义是:一个包含术语定义以及描述的列表,通常用于展示词汇表或者元数据 (键 ...
- Java:应用Observer接口实践Observer模式
本文出自“子 孑” 博客,原文链接:http://zhangjunhd.blog.51cto.com/113473/68949 在Java中通过Observable类和Observer接口实现了观察者 ...
- 用node.js读写文件
node.js没有二进制数据类型,却提供了类似字节数组的“流“数据类型,着一种数据类型在文件系统模块中频频出现 node.js打开文件 fs = require('fs'); console.log( ...
- 2018-2019 ICPC, NEERC J. Streets and Avenues in Berhattan(DP)
题目链接:https://codeforc.es/contest/1070/problem/J 题意:给出一个长度为 k 的字符串,选出 n 个和 m 个不同位置的字符构成两个字符串,使得两个字符串相 ...
- 【细谈Java并发】谈谈LinkedBlockingQueue(转)
最近在看concurrent包的知识,看到LinkedBlockingQueue,发现一篇好文推荐给大家.原文地址:[细谈Java并发]谈谈LinkedBlockingQueue 1.简介 上篇我们介 ...