HDU 3549 Dinic

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 8946    Accepted Submission(s): 4205

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 contains an integer T, denoting the number of test cases.
For
each test case, the first line contains two integers N and M, denoting
the number of vertexes and edges in the graph. (2 <= N <= 15, 0
<= M <= 1000)
Next M lines, each line contains three integers
X, Y and C, there is an edge from X to Y and the capacity of it is C. (1
<= X, Y <= N, 1 <= C <= 1000)

Output

For each test cases, you should output the maximum flow from source 1 to sink N.

Sample Input

2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1

Sample Output

Case 1: 1
Case 2: 2

Author

HyperHexagon

Source

HyperHexagon‘s Summer Gift (Original tasks)

Recommend

zhengfeng   |   We have carefully selected several similar problems for you:  3572 3416 3081 3491 1533

跟上一道题一样。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int inf=0x3fffffff;
const int maxn=207;
struct Edge
{
    int cap,flow;
};
int n,m,s,t;
Edge edges[maxn][maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init()
{
          memset(edges,0,sizeof(edges));
}
bool BFS()
{
    memset(vis,0,sizeof(vis));
    queue<int>Q;
    Q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!Q.empty()){
        int x=Q.front();Q.pop();
        for(int i=1;i<=n;i++){
            Edge &e=edges[x][i];
            if(!vis[i]&&e.cap>e.flow){
                vis[i]=1;
                d[i]=d[x]+1;
                Q.push(i);
            }
        }
    }
    return vis[t];
}
int DFS(int u,int cp)
{
    int tmp=cp;
    int v,t;
    if(u==n)
        return cp;
    for(v=1;v<=n&&tmp;v++)
    {
        if(d[u]+1==d[v])
        {
            if(edges[u][v].cap>edges[u][v].flow)
            {
                t=DFS(v,min(tmp,edges[u][v].cap-edges[u][v].flow));
                edges[u][v].flow+=t;
                edges[v][u].flow-=t;
                tmp-=t;
            }
        }
    }
    return cp-tmp;
}
int Maxflow()
{
    int flow=0;
    while(BFS()){
        memset(cur,0,sizeof(cur));
        flow+=DFS(s,inf);
    }
    return flow;
}
int main()
{
  // freopen("in.txt","r",stdin);
   int tt,cnt=1;
   scanf("%d",&tt);
    int a,b, c;
    while(tt--){
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            edges[a][b].cap+=c;
        }
        s=1;t=n;
        int ans=Maxflow();
        printf("Case %d: %d\n",cnt++,ans);
    }
    return 0;
}
时间: 2024-11-11 11:13:12

HDU 3549 Dinic的相关文章

hdu 3549 Flow Problem (最大流入门题)

增广路: 1 /************************************************************* 2 题目: Flow Problem(HDU 3549) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 4 题意: 给一个单向图,求从1到n的最大流 5 算法: 最大流之增广路(入门) 6 算法思想: 不断用BFS找通路,没每找一条路,记录这条路的最小流, 7 再给这条路上的所有流量减去这个最小值.

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 网络最大流问题 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

网络流 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 3987 &amp;&amp; DINIC

很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流,但有可能是最少边数最少割的边呢?否.因为按照DINIC的求法,每次都是增广容量最少的路,若当前不满流,则必定不是最小割的边,所以只需在满流的边,即可组成最小割的边寻找最少边就可以了. 1 #include <iostream> 2 #include <cstring> 3 #incl

(Dinic) hdu 3549

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

hdu 3549 Flow Problem (Dinic)

Flow ProblemTime Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 21438    Accepted Submission(s): 10081 Problem DescriptionNetwork flow is a well-known difficult problem for ACMers. Given a graph, yo