hdu3549 flow

水水maxflow~,练练手

#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
using namespace std;
const int MAXN=35,MAXE=1000*10;
const int inf=0x3f3f3f3f;
int e[MAXE][3];int head[MAXN];int nume=0;
void adde(int i,int j,int f)
{
    e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;
    e[nume++][2]=f;
    e[nume][0]=i;e[nume][1]=head[j];head[j]=nume;
    e[nume++][2]=0;
}
int n,m;
int lev[MAXN];int vis[MAXN];
bool bfs()
{
    for(int i=0;i<=n;i++)
    {
        lev[i]=vis[i]=0;
    }
    queue<int>q;
    lev[1]=vis[1]=1;
    q.push(1);
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        for(int j=head[cur];j!=-1;j=e[j][1])
        {
            int to=e[j][0];
            if(!vis[to]&&e[j][2]>0)
            {
                vis[to]=1;
                lev[to]=lev[cur]+1;
                q.push(to);
            }
        }
    }
    return vis[n];
}
int dfs(int u,int minf)
{
    if(u==n||minf==0)return minf;
    int sumf=0,f;
    for(int j=head[u];j!=-1;j=e[j][1])
    {
         int to=e[j][0];
        if(lev[to]==lev[u]+1)
        {
            f=dfs(to,min(minf,e[j][2]));
            sumf+=f;minf-=f;
            e[j][2]-=f;e[j^1][2]+=f;
        }
    }
    if(!sumf)lev[u]=-1;
    return sumf;
}
int dinic()
{
    int sums=0;
    while(bfs())
    {
        sums+=dfs(1,inf);
    }
    return sums;
}

void init()
{
    for(int i=0;i<MAXN-1;i++)
    {
        head[i]=-1;
    }
    nume=0;
}
int main()
{
    int T;int cnts=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int aa,bb,ww;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&aa,&bb,&ww);
            adde(aa,bb,ww);
        }
        printf("Case %d: %d\n",cnts++,dinic());
    }
    return 0;
}
时间: 2024-11-06 23:16:34

hdu3549 flow的相关文章

hdu3549 Flow Problem(裸最大流)

Flow Problem Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted d

HDU3549 Flow Problem 【最大流】

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8387    Accepted Submission(s): 3908 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y

HDU3549 Flow Problem【最大流】【Edmond-Karp】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给你一个N个节点M条边的加权有向图,源点为1,汇点为N,求此图的最大流. 思路: 这道题就是网络流求最大流的裸题.直接用Edmond-Karp算法来做就可以了. AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<

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[50][50],v[50],pre[1000]; int n,m; int bfs(int s,int t) { memset(v,0,sizeof(v));

HDU-3549 最大流模板题

1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://www.cnblogs.com/Lyush/archive/2011/08/08/2130660.html  ,里面有几种模板,没太看懂 题意:给定有向图,求第1到第n个点的最大流. #include<iostream> #include<cstring> #include<cmat

基于git的源代码管理模型——git flow

说明: 本文以nvie的“a successful git branching model”为蓝本,结合我个人理解写成.如有谬误,还请各位指出.多谢! Note: This article is highly based on nvie's a successful git branching model. Thanks nvie. Git Flow 是什么 Git Flow是构建在Git之上的一个组织软件开发活动的模型,是在Git之上构建的一项软件开发最佳实践.Git Flow是一套使用Git

[USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa

Openvswitch原理与代码分析(7): 添加一条流表flow

添加一个flow,调用的命令为 ovs-ofctl add-flow hello "hard_timeout=0 idle_timeout=0 priority=1 table=21 pkt_mark=0x55 tun_id=0x55 actions=mod_nw_dst:192.168.56.101,output:2" 这里调用的是调用ovs/utilities/ovs-ofctl.c的命令行工具 这个命令行工具支持的所有的命令及处理函数定义如下: static const stru

bzoj3396[Usaco2009 Jan]Total flow 水流*

bzoj3396[Usaco2009 Jan]Total flow 水流 题意: 求无环图的最大流.边数≤700. 题解: 管它有没有环.注意本题的节点标号既有大写字母,也有小写字母. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 #de