码迷,mamicode.com
首页 > 其他好文 > 详细

最大流入门题目

时间:2015-01-03 00:54:36      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

HDU 3549

Flow Problem

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


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
 
POJ 1273
HDU 1532
Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 58288   Accepted: 22394

Description

Every time it rains on Farmer John‘s fields, a pond forms over Bessie‘s favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie‘s clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

这两题是一样的、只是输入和节点数量有点差异。

下面给出第一题代码:

EK算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
#define N 30

int src;
int des;
int n,m;
int pre[N];
int mpt[N][N];
queue<int> q;

int bfs()
{
    while(!q.empty()) q.pop();
    memset(pre,-1,sizeof(pre));
    pre[src]=0;
    q.push(src);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int v=1;v<=n;v++)
        {
            if(pre[v]==-1 && mpt[u][v]>0)
            {
                pre[v]=u;
                if(v==des) return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
int EK()
{
    int maxflow=0;
    while(bfs()) 
    {
        int minflow=INF;
        for(int i=des;i!=src;i=pre[i])
            minflow=min(minflow,mpt[pre[i]][i]);
        maxflow+=minflow;
        for(int i=des;i!=src;i=pre[i]) 
        {
            mpt[i][pre[i]]+=minflow;
            mpt[pre[i]][i]-=minflow;
        }
    }
    return maxflow;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(mpt,0,sizeof(mpt));
        src=1;
        des=n;
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            mpt[u][v]+=w;
        }
        printf("%d\n",EK());
    }
    return 0;
}

FF算法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
#define N 210

int flag;
int src;
int des;
int n,m;
int pre[N];
int mpt[N][N];

void dfs(int u)
{
    if(flag || u==des)
    {
        flag=1;
        return;
    }
    for(int v=1;v<=n;v++)
    {
        if(pre[v]==-1 && mpt[u][v]>0)
        {
            pre[v]=u;
            dfs(v);
        }
    }
}
int FF()
{
    int maxflow=0;
    while(1)
    {
        flag=0;
        memset(pre,-1,sizeof(pre));
        pre[src]=0;
        dfs(src);
        if(!flag) break; 
        int minflow=INF;
        for(int i=des;i!=src;i=pre[i])  
            minflow=min(minflow,mpt[pre[i]][i]);
        maxflow+=minflow; 
        for(int i=des;i!=src;i=pre[i]) 
        {
            mpt[i][pre[i]]+=minflow;
            mpt[pre[i]][i]-=minflow;
        }
    }
    return maxflow;
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(mpt,0,sizeof(mpt));
        src=1;
        des=n;
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            mpt[u][v]+=w;
        }
        printf("%d\n",FF());
    }
    return 0;
}

 

POJ 1459

Power Network
Time Limit: 2000MS   Memory Limit: 32768K
Total Submissions: 23684   Accepted: 12379

Description

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= lmax(u,v) of power delivered by u to v. Let Con=Σuc(u) be the power consumed in the net. The problem is to compute the maximum value of Con. 
技术分享

An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6. 

Input

There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of cmax(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

Sample Input

2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
         (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
         (0)5 (1)2 (3)2 (4)1 (5)4

Sample Output

15
6

哎、英语是硬伤、
上面的问题都是单源单汇问题,这题是多源多汇,关键在于把多源多汇问题转化为单源单汇,只要构造一个超级源点和一个超级汇点就行了,把超级源点和各个源点之间加一条边,把各个汇点和超级汇点之间加一条边。
EK算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
#define N 110

int n;
int src;
int des;
int p,c,l;
int pre[N];
int mpt[N][N];
queue<int> q;

int bfs()
{
    while(!q.empty()) q.pop();
    memset(pre,-1,sizeof(pre));
    pre[src]=0;
    q.push(src);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int v=1;v<=n;v++)
        {
            if(pre[v]==-1 && mpt[u][v]>0)
            {
                pre[v]=u;
                if(v==des) return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
int EK()
{
    int maxflow=0;
    while(bfs())
    {
        int minflow=INF;
        for(int i=des;i!=src;i=pre[i])
            minflow=min(minflow,mpt[pre[i]][i]);
        maxflow+=minflow;
        for(int i=des;i!=src;i=pre[i])
        {
            mpt[i][pre[i]]+=minflow;
            mpt[pre[i]][i]-=minflow;
        }
    }
    return maxflow;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        n+=2;
        src=1;
        des=n;
        memset(mpt,0,sizeof(mpt));
        scanf("%d%d%d",&p,&c,&l);
        for(int i=1;i<=l;i++)
        {
            int u,v,w;
            scanf(" (%d,%d)%d",&u,&v,&w);
            if(u==v) continue;
            u+=2;
            v+=2; 
            mpt[u][v]+=w;
        }
        for(int i=1;i<=p;i++)  //超级起点
        {
            int v,w;
            scanf(" (%d)%d)",&v,&w);
            v+=2;
            mpt[1][v]+=w;
        }
        for(int i=1;i<=c;i++)  //超级汇点
        {
            int u,w;
            scanf(" (%d)%d)",&u,&w);
            u+=2;
            mpt[u][n]+=w;
        }
        printf("%d\n",EK());
    }
    return 0;
}

最大流入门题目

标签:

原文地址:http://www.cnblogs.com/hate13/p/4199028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!