最大流入门题目

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;
}
				
时间: 2024-10-14 00:19:36

最大流入门题目的相关文章

HDU3549(最大流入门模板题)

public static void main(String[] args) { String a=null; if("aa".equals(a))//这种情形,不出现空指针异常 //if(a.equals("aa"))//出现空指针异常 { System.out.println(true); } else { System.out.println(false); } } 上面的两句不同的比较语句测试,第一句不出现空指针异常,第二句出现. 所以在变量和常量比较的时候

Drainage Ditches 最大流入门练习题,各种算法

Drainage Ditches 题目抽象:n个顶点,m条容量为ci的边组成的图,求源点为1,汇点为n的最大流. 分析:各种最大流算法. 1.ford() 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 #include <cstring> 5 using namespace std; 6 const int INF = 0x5fffffff; 7 const int MS =

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 再给这条路上的所有流量减去这个最小值.

IO流入门-第十一章-PrintStream_PrintWriter

DataInputStream和DataOutputStream基本用法和方法示例 /* java.io.PrintStream:标准的输出流,默认打印到控制台,以字节方式 java.io.PrintWriter:以字符方式 */ import java.io.*; import java.util.Date; import java.text.*; public class PrintStreamTest01 { public static void main(String[] args) t

io流入门级别demo

import java.io.*; //import java.io.BufferedReader; //import java.io.BufferedWriter; //import java.io.File; //import java.io.FileInputStream; //import java.io.FileNotFoundException; //import java.io.FileOutputStream; //import java.io.FileReader; //imp

IO流入门-第十二章-ObjectInputStream_ObjectOutputStream

DataInputStream和DataOutputStream基本用法和方法示例,序列化和反序列化 import java.io.Serializable; //该接口是一个"可序列化"的 ,没有任何方法,是一个标识接口,还有Cloneable /* 标识接口的作用:标识作用,JVM如果看到对象实现了某个标识接口,会对它特殊待遇,会给该类添加一个属性,static final long serialVersionUID=xxx, 最好是自己指定一个唯一的,这样不会产生类的兼容问题.

IO流入门-第八章-BufferedWriter

BufferedWriter基本用法和方法示例 import java.io.*; public class BufferedWriterTest01 { public static void main(String[] args) throws Exception { //创建带有缓冲区的字符输出流 //BufferedWriter bw = new BufferedWriter(new FileWriter("temp04")); BufferedWriter bw = new B

IO流入门-第四部分-FileReader

FileReader基本用法和方法示例 /* java.io.Reader java.io.InputStreamReader 转换流(字节输入流---->字符输入流) java.io.FileReader 文件字符输入流 */ import java.io.*; public class FileReaderTest01 { public static void main(String[] args) throws Exception { //创建文件字符输入流 FileReader fr =

Java8新特性(1)—— Stream集合运算流入门学习

废话,写在前面 好久没写博客了,懒了,以后自觉写写博客,每周两三篇吧! 简单记录自己的学习经历,算是对自己的一点小小的督促! Java8的新特性很多,比如流处理在工作中看到很多的地方都在用,是时候扔掉笨重的for循环了!节省40%-50%代码量!Come on! 如题 (总结要点) Stream API只能被消费一次,后续重复使用已建立的流会报异常!所以stream流是线程安全的! 比如执行"stream.forEach(System.out::println);stream.forEach(S