网络流 最大流HDU 3549


//////////

在这幅图中我们首先要增广1->2->4->6,这时可以获得一个容量为2的流,但是如果不建立4->2反向弧的话,则无法进一步增广,最终答案为2,显然是不对的,然而如果建立了反向弧4->2,则第二次能进行1->3->4->2->5->6的增广,最大流为3.

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<string.h>
  4 #include<queue>
  5 typedef long long LL;
  6
  7 using namespace std;
  8 int n,m;
  9 #define inf 100000000
 10 #define MAXN 5000
 11 #define MAXN1 50
 12 struct edg
 13 {
 14     int w,to,next;
 15 }x[MAXN+10];
 16 int head[MAXN1],cnt;
 17 int vis[MAXN1];
 18
 19 void add(int u,int v,int w)
 20 {
 21     x[cnt].next=head[u];
 22     x[cnt].to=v;
 23     x[cnt].w=w;
 24     head[u]=cnt++;
 25     x[cnt].next=head[v];
 26     x[cnt].to=u;
 27     x[cnt].w=0;
 28     head[v]=cnt++;
 29 }
 30 queue<int>q1;
 31
 32 int bfs()  //层次网络
 33 {
 34     memset(vis,-1,sizeof(vis));
 35     vis[1]=0;
 36     q1.push(1);
 37     while(!q1.empty())
 38     {
 39         int now=q1.front();
 40         q1.pop();
 41         int j;
 42         for(j=head[now];j!=-1;j=x[j].next)
 43         {
 44             if(vis[x[j].to]<0&&x[j].w)
 45             {
 46                 vis[x[j].to]=vis[now]+1;
 47                 q1.push(x[j].to);
 48             }
 49         }
 50     }
 51     if(vis[n]<0) //汇点不在网络 结束
 52         return 0;
 53     return 1;
 54 }
 55 int dfs(int u,int w)
 56 {
 57     if(u==n)
 58         return w;
 59     int j;
 60     int ans=0;
 61
 62     for(j=head[u];j!=-1&&ans<=w;j=x[j].next)
 63     {
 64         if(vis[x[j].to]==vis[u]+1&&x[j].w)
 65         {
 66             int b=dfs(x[j].to,min(w-ans,x[j].w)); //流进去的有2个限制 min(总流量减去已经流掉的,可以流进去的)
 67             x[j].w-=b;
 68             x[j^1].w+=b;                          //cnt=0 开始 反向边下标=j^1  可以自己试试
 69             ans+=b;
 70         }
 71     }
 72     return ans;
 73 }
 74 int main()
 75 {
 76     int t,ca;
 77
 78     scanf("%d",&t);
 79     ca=1;
 80     while(t--)
 81     {
 82         scanf("%d%d",&n,&m);
 83         cnt=0;
 84         memset(head,-1,sizeof(head));
 85         int i,j;
 86         for(i=1;i<=m;i++)
 87         {
 88             int u,v,w;
 89             scanf("%d%d%d",&u,&v,&w);
 90             add(u,v,w); //建边 有反向边
 91         }
 92         int ans=0;
 93         while(bfs()) //Dinic bfs+dfs
 94             ans+=dfs(1,inf);
 95         printf("Case %d: %d\n",ca++,ans);
 96     }
 97
 98     return 0;
 99 }
100   /*By--MJY*/
时间: 2025-01-05 22:34:36

网络流 最大流HDU 3549的相关文章

【网络流#1】hdu 3549

因为坑了无数次队友 要开始学习网络流了,先从基础的开始,嗯~ 这道题是最大流的模板题,用来测试模板好啦~ Edmonds_Karp模板 with 前向星 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<iostream> 5 #include<queue> 6 #define eps 0.000001 7 #define MAXN 20 8 #define M

hdu 4289 Control(网络流 最大流+拆点)(模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1545    Accepted Submission(s): 677 Problem Description You, the head of Department o

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define

网络流 HDU 3549 Flow Problem

网络流 HDU 3549 Flow Problem 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法进行求解,注意的问题有两个: 1. 每次增广的时候,反向流量也要进行更行,一开始没注意,WA了几次 ORZ 2. 对于输入的数据,容量要进行累加更新. // 邻接矩阵存储 #include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const i

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem 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 directed graph. Input The first line of input

hdu 3549 Flow Problem (网络最大流)

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

HDU 4309 Seikimatsu Occult Tonneru(网络流-最大流)

Seikimatsu Occult Tonneru Problem Description During the world war, to avoid the upcoming Carpet-bombing from The Third Reich, people in Heaven Empire went to Great Tunnels for sheltering. There are N cities in Heaven Empire, where people live, with

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

[笔记] 网络流-最大流 POJ-1273\HDU-4240

[1] POJ-1273 题目:http://poj.org/problem?id=1273 最直接的最大流问题,用了Ford-Fulkerson方法,DFS随机搜索增广路. 算法原理参考:http://blog.csdn.net/smartxxyx/article/details/9293665 /************************ POJ-1273* Ford-Fulkerson***********************/#include <stdio.h> #inclu