首页 > 代码库 > HDU 3549 Dinic

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
23 21 2 12 3 13 31 2 12 3 11 3 1
 

 

Sample Output
Case 1: 1Case 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;}

 

HDU 3549 Dinic