#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)的更多相关文章

  1. [hdu3549]Flow Problem(最大流模板题)

    解题关键:使用的挑战程序设计竞赛上的模板,第一道网络流题目,效率比较低,且用不习惯的vector来建图. 看到网上其他人说此题有重边,需要注意下,此问题只在邻接矩阵建图时会出问题,邻接表不会存在的,也 ...

  2. 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 ...

  3. Flow Problem(最大流模板)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  4. HDU3549 Flow Problem 【最大流量】

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  5. POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

    http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...

  6. hdu-3549 Flow Problem---最大流模板题(dinic算法模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...

  7. hdu 3549 Flow Problem (最大流)

    裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...

  8. HDU3549 Flow Problem(网络流增广路算法)

    题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...

  9. hdu 3549 Flow Problem 最大流 Dinic

    题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...

随机推荐

  1. WeakReference 与 SoftReference 区别

    装载自:http://flyneil.iteye.com/blog/1345177 WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. Wea ...

  2. DevExpress VCL 19.2.3 Skin找不到皮肤

    The location where the DX Designtime Loader is looking for the skin packages has changed again ! For ...

  3. 文件 file open函数的打开及 函数的调用

    文件 mode 模式字符的含义 字符 含义 'r' 以只读方式打开(默认) 'w' 以只写方式打开,删除原有文件内容(如果文件不存在,则创建该文件并以只写方式打开) 'x' 创建一个新文件, 并以写模 ...

  4. 高级IO——文件锁

    文件锁也被称为记录所,文件锁如果深讲的话,内容不少(比如文件锁最起码分为了建议锁和强制性锁,暂时挖坑,后面填). 文件锁作用 顾名思义,就是用来保护文件数据的.当多个进程共享读写同一个文件时,为了不让 ...

  5. 利用 sendBeacon 发送统计信息

    我们经常会在网站追踪用户的信息,比如记录用户的停留时间. window.addEventListener("unload", () => { // sendHTTP }); ...

  6. SpringCloud01——服务的注册和发现

    SpringCloud01--服务的注册和发现 一.微服务的注册和发现 我们在微服务中,往往有服务提供者,服务消费者和服务注册中心.我们之前学习的Zookeeper就是一个注册中心.但是在官方的Spr ...

  7. Caffe---自带工具进行网络结构(xxx.prototxt)可视化

    Caffe---自带绘图工具(draw_net.py)绘制网络结构图(xxx.prototxt) 目录: 一,安装依赖库. 二,draw_net.py使用说明. 正文: 一,安装依赖库. 在绘制之前, ...

  8. E - GCD HDU - 2588

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  9. Appium Desired Capabilities-iOS Only

    Appium Desired Capabilities-iOS Only These Capabilities are available only on the XCUITest Driver an ...

  10. python 前置程序窗口,还原最小化的窗口

    python 前置程序窗口,还原最小化的窗口 在网上找了比较久,大多是: win32gui.FindWindow(class_name, window_name) win32gui.SetForegr ...