HDU3549Flow Problem(模板题)

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 9769    Accepted Submission(s): 4577

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

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define captype int
const int N  = 210;
const int MAX= 1<<30;

struct EDG{
    int to,nxt;
    captype c;  //每条边的残留量
}edg[N*N];
int head[N],eid;
captype vf[N];     //顶点的剩余流量
int h[N];      //顶点的高度
//int n;         //顶点个总个数,包含源点与汇点

int min(int a,int b){return a>b?b:a; }
void init(){
    memset(head,-1,sizeof(head));
    eid=0;
}
//添加 有向边
void addEdg(int u , int v, captype c){
    edg[eid].to=v; edg[eid].nxt=head[u]; edg[eid].c=c; head[u]=eid++;
    edg[eid].to=u; edg[eid].nxt=head[v]; edg[eid].c=0; head[v]=eid++;
}
captype maxFlow(int sNode,int eNode,int n){//源点与汇点
    captype minh,ans=0;
    queue<int>q;

    memset(h,0,sizeof(h));
    memset(vf,0,sizeof(vf));
    h[sNode]=n+1;   //源点的高度
    vf[sNode]=MAX;  //源点的余流

    q.push(sNode);
    while(!q.empty()){
        int u=q.front(); q.pop();
        minh=MAX;

        for(int i=head[u]; i!=-1; i=edg[i].nxt){
            int v=edg[i].to;

            captype fp;
            if(edg[i].c<vf[u])fp=edg[i].c;
            else fp=vf[u];

            if(fp>0 ){
                minh=min(minh, h[v]);
                if(u==sNode || h[u]==h[v]+1){
                    edg[i].c-=fp;
                    edg[i^1].c+=fp; //反向边,给个反回的通道
                    vf[u]-=fp;
                    vf[v]+=fp;
                    if(v==eNode) ans+=fp;   //当到达汇点时,就加入最大流中
                    if(v!=sNode && v!=eNode )   //只有即不是源点,也不是汇点时才能进队列
                        q.push(v);
                }
            }
            if(vf[u]==0) break; //如果顶点的余流为0,则可以跳出for循环
        }
        //如果不是源点(也非汇点),且顶点仍有余流,则重新标记 高度+1 入队
        //这里赋值为最低的相邻顶点的高度高一个单位,也可以简单的在原高度+1
        if(u!=sNode && vf[u]>0){
            h[u] = minh + 1;
            q.push(u);
        }
    }
    return ans;
}
int main(){
    int T,cas=0,n,m,a,b,c;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        init();
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            addEdg(a,b,c);
        }
        printf("Case %d: %d\n",++cas,maxFlow(1,n,n));
    }
}

时间: 2024-07-28 14:44:43

HDU3549Flow Problem(模板题)的相关文章

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

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

Flow Problem 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 nu

A Simple Problem with Integers——区间查询、去区间修改模板题

题目链接 题意: C a b c  [a,b]区间都加c Q a b   查询[a,b]的区间和 题解: 区间修改+区间查询 模板题 代码: #include<iostream> #include<stdio.h> #include<math.h> #include<algorithm> #include<vector> #include<map> using namespace std; typedef long long ll;

hdu3549--Flow Problem(初识最大流)

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

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

hdu5384 AC自动机模板题,统计模式串在给定串中出现的个数

http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, the series' name is compounded from the Japanese words for "bullet" (dangan) and "refutation&q

HDU 5521.Meeting 最短路模板题

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3361    Accepted Submission(s): 1073 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

POJ 3264:Balanced Lineup(RMQ模板题)

http://poj.org/problem?id=3264 题意:给出n个数,还有q个询问,询问[l,r]区间里面最大值和最小值的差值. 思路:RMQ模板题,开两个数组维护最大值和最小值就行. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 #define N 50010 7 #define

D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 分组背包模板题

http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断. 这样是不对的.因为这样使得同一组里面可能选择了两次. 3 0 2 1 2 3 1 1 3 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa