#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. python实现蓝牙通信

    安装和示例 linux下安装 -dev sudo pip install bluepy 官方示例 import btle class MyDelegate(btle.DefaultDelegate): ...

  2. Python-memcached的使用用法

    Memcached API set(key,val,time=0,min_compress_len=0) 无条件键值对的设置,其中的time用于设置超时,单位是秒,而min_compress_len则 ...

  3. scp上传文件到远程服务器

    scp -P 22 E:/download/2028792_www.yeves.cn_nginx/cloud.pem root@ip:/usr/local/src

  4. VM安装vmtools后centos7无法上网

    先安装VmTools工具 文件 /etc/sysconfig/network-scripts/ifcfg-ens33(这里的enp0s3不是固定的,看你具体情况,但是基本是en开头的) 将 ONBOO ...

  5. 系统模块 sys 函数的调用

    系统模块 sys 运行时系统相关的信息 sys模块的数据 数据 描述 sys.path 模块搜索路径 path[0] 是当前脚本程序的路径名,否则为 '' sys.modules 已加载模块的字典 s ...

  6. 第五章、前端之JQuery

    目录 第五章.前端之JQuery 一.选择器 二.基本筛选器 三.样式操作 四.位置操作 五.文本操作 六.属性操作 七.文档处理 八.事件 九.动画效果 十.补充 第五章.前端之JQuery 一.选 ...

  7. idea启动卡死,项目界面一直processing

    1 原因 因为上次退出项目,非正常退出,导致索引生成有问题. 2 解决办法 删除项目根目录下 .idea文件夹,然后重新打开,重新indexing生成索引文件

  8. shell读取或者修改ini文件

    cfg_find(){ file_name=$1 labelname=$2 key=$3 labelline=$(grep -n "^\[.*\]$" $file_name | a ...

  9. 为什么Microsoft Office 2016安装时不能自选安装组件和安装路径?

    使用特别版本的安装镜像文件 SW_DVD5_Office_Professional_Plus_2016_64Bit_ChnSimp_MLF_X20-42426.iso,请自行搜索和下载 文件: SW_ ...

  10. MODI的OCR接口

    MODI的OCR接口 MODI的OCR接口 MODI的OCR接口