最大流的增广路算法(KM算法).

最大流的增广路算法(KM算法).
最大流的增广路算法(KM算法).

1459:Power Network

Time Limit: 2000MS Memory Limit: 32768K

Total Submissions: 17697 Accepted: 9349 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) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(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) <= l max(u,v) of power delivered by u to v. Let

Con=Σu c(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

p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(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 l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(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

c max(u). All input numbers are integers. Except the (u,v)z triplets an

d th

e (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end o

f 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

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.

Source

Southeastern Europe 2003

2426:ACM Computer Factory

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 3986 Accepted: 1325 Special Judge Description

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.

Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.

Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.

Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.

Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.

The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.

After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.

As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

Input

Input file contains integers P N, then N descriptions of the machines. The description of i th machine is represented as by 2 P + 1 integers Q i S i,1Si,2...S i,P D i,1D i,2...D i,P, where Q i specifies performance, S i,j— input specification for part j, D i,k— output specification for part k.

Constraints

1 ≤ P≤ 10, 1 ≤ N ≤ 50, 1 ≤ Q i≤ 10000

Output

Output the maximum possible overall performance, then M— number of connections that must be made, then M descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.

If several solutions exist, output any of them.

Sample Input

Sample input 1

3 4

15 0 0 0 0 1 0

10 0 0 0 0 1 1

30 0 1 2 1 1 1

3 0 2 1 1 1 1

Sample input 2

3 5

5 0 0 0 0 1 0

100 0 1 0 1 0 1

3 0 1 0 1 1 0

1 1 0 1 1 1 0

300 1 1 2 1 1 1

Sample input 3

2 2

100 0 0 1 0

200 0 1 1 1

Sample Output

Sample output 1

25 2

1 3 15

2 3 10

Sample output 2

4 5

1 3 3

3 5 3

1 2 1

2 4 1

4 5 1

Sample output 3

0 0

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

Source

Northeastern Europe 2005, Far-Eastern Subregion

最大流的增广路算法(KM算法).

1459:Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 17697 Accepted: 9349 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) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(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) <= l max(u,v) of power delivered by u to v. Let Con=Σu c(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 p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(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 l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(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

分支限界法实现单源最短路径问题

实验五分支限界法实现单源最短路径 一实验题目:分支限界法实现单源最短路径问题 二实验要求:区分分支限界算法与回溯算法的区别,加深对分支限界法的理解。 三实验内容:解单源最短路径问题的优先队列式分支限界法用一极小堆来存储活结点表。其优先级是结点所对应的当前路长。算法从图G的源顶点s和空优先队列开始。 结点s被扩展后,它的儿子结点被依次插入堆中。此后,算法从堆中取出具有最小当前路长的结点作为当前扩展结点,并依次检查与当前扩展结点相邻的所有顶点。如果从当前扩展结点i到顶点j有边可达,且从源出发,途经顶点i再到顶点j的所相应的路径的长度小于当前最优路径长度,则将该顶点作为活结点插入到活结点优先队列中。这个结点的扩展过程一直继续到活结点优先队列为空时为止。 四实验代码 #include using namespace std; const int size = 100; const int inf = 5000; //两点距离上界 const int n = 6; //图顶点个数加1 int prev[n]; //图的前驱顶点 int dist[] = {0,0,5000,5000,5000,5000}; //最短距离数组 int c[n][n] = {{0,0,0,0,0,0},{0,0,2,3,5000,5000}, //图的邻接矩阵 {0,5000,0,1,2,5000},{0,5000,5000,0,9,2}, {0,5000,5000,5000,0,2},{0,5000,5000,5000,5000,0}}; const int n = 5; //图顶点个数加1 int prev[n]; //图的前驱顶点 int dist[] = {0,0,5000,5000,5000}; int c[][n] = {{0,0,0,0,0},{0,0,2,3,5000},{0,5000,0,1,2},{0,5000,5000,0,9}, {0,5000,5000,5000,0}};

算法分析与设计(最大流问题)

算法分析与设计题目:最大流算法 院系:软件工程 班级:软件11-2班 姓名:慕永利 学号:23 号

目录 1算法提出背景............................................................................................................................- 3 - 2 问题实例及解决.......................................................................................................................- 3 - 3算法论述....................................................................................................................................- 4 - 3.1、可行流..........................................................................................................................- 4 - 3.2 最大流..........................................................................................................................- 5 - 3.3最大流算法.....................................................................................................................- 6 - 3.3.1 增广路径.......................................................................................................- 6 - 3.3.2沿增广路径增广..................................................................................................- 7 - 3.3.3样例:..................................................................................................................- 8 - 3.3.4定理:............................................................................................................... - 13 - 3.3.5算法的实现:................................................................................................... - 13 - 3.3.6 优化.................................................................................................................. - 16 - 4算法应用................................................................................................................................. - 18 -

单源最短路径 贪心算法

实验三单源最短路径 一、实验目的及要求 掌握贪心算法的基本思想 用c程序实现单源最短路径的算法 二、实验环境 Window下的vc 2010 三、实验内容 1、有向图与单源点最短路径 2、按路径长度非降的次序依次求各节点到源点的最短路径 3、Dijkstra算法 四、算法描述及实验步骤 设给定源点为Vs,S为已求得最短路径的终点集,开始时令S={Vs} 。当求得第一条最短路径(Vs ,Vi)后,S为{Vs,Vi} 。根据以下结论可求下一条最短路径。 设下一条最短路径终点为Vj ,则Vj只有:源点到终点有直接的弧 ;从Vs 出发到Vj 的这条最短路径所经过的所有中间顶点必定在S中。即只有这条最短路径的最后一条弧才是从S内某个顶点连接到S外的顶点Vj 。 若定义一个数组dist[n],其每个dist[i]分量保存从Vs 出发中间只经过集合S中的顶点而到达Vi的所有路径中长度最小的路径长度值,则下一条最短路径的终点Vj必定是不在S中且值最小的顶点, 即:dist[i]=Min{ dist[k]| Vk∈V-S } 利用公式就可以依次找出下一条最短路径。 在程序中c[][]表示带权邻接矩阵, dist[]表示顶点到源点的最短路径, p[]记录顶点到源点最短路径的前驱节点, u源点,函数Way是递归的构造出最短路径的次序。 五、实验结果 程序执行的结果: 六、源代码 #include #include using namespace std;

#define MAX 999 void getdata(int **c,int n) { int i,j; int begin,end,weight; for (i=1;i<=n;i++) { for (j=1;j<=n;j++) { if(i==j) c[i][j]=0; else c[i][j]=MAX; } } do { cout<<"请输入起点终点权值(-1退出):"; cin>>begin; if(begin==-1) break; cin>>end>>weight; c[begin][end]=weight; } while(begin!=-1); } void Dijkstra(int n,int v ,int *dist,int *prev,int **c) { bool s[MAX]; int i,j; for (i=1;i<=n;i++) { dist[i]=c[v][i]; //从源点到各点的值 s[i]=false; if(dist[i]==MAX) prev[i]=0; //最大值没有路径 else prev[i]=v; //前驱为源点 } dist[v]=0;s[v]=true; for (i=1;i<=n;i++) { int temp=MAX; int u=v; for(j=1;j<=n;j++) if((!s[j])&&(dist[j]

单源最短路径问题

实验四单源最短路径问题 一、实验目的: 1、理解分支限界法的剪枝搜索策略; 2、掌握分支限界法的算法柜架; 3、掌握分支限界法的算法步骤; 4、通过应用范例学习动态规划算法的设计技巧与策略; 二、实验内容及要求: 1、使用分支限界法解决单源最短路径问题。 2、通过上机实验进行算法实现。 3、保存和打印出程序的运行结果,并结合程序进行分析,上交实验报告。 三、实验原理: 分支限界法的基本思想: 1、分支限界法与回溯法的不同: 1)求解目标:回溯法的求解目标是找出解空间树中满足约束条件的所有解,而分支限界法的求解目标则是找出满足约束条件的一个解,或是在满足约束条件的解中找出在某种意义下的最优解。 2)搜索方式的不同:回溯法以深度优先的方式搜索解空间树,而分支限界法则以广度优先或以最小耗费优先的方式搜索解空间树。 2、分支限界法基本思想: 分支限界法常以广度优先或以最小耗费(最大效益)优先的方式搜索问题的解空间树。 在分支限界法中,每一个活结点只有一次机会成为扩展结点。活结点一旦成为扩展结点,就一次性产生其所有儿子结点。在这些儿子结点中,导致不可行解或导致非最优解的儿子结点被舍弃,其余儿子结点被加入活结点表中。

此后,从活结点表中取下一结点成为当前扩展结点,并重复上述结点扩展过程。这个过程一直持续到找到所需的解或活结点表为空时为止。 3、常见的两种分支限界法: 1)队列式(FIFO)分支限界法 按照队列先进先出(FIFO)原则选取下一个节点为扩展节点。 2)优先队列式分支限界法 按照优先队列中规定的优先级选取优先级最高的节点成为当前扩展节点。 四、程序代码 #include using namespace std; int matrix[100][100]; // 邻接矩阵 bool visited[100]; // 标记数组 int dist[100]; // 源点到顶点i的最短距离 int path[100]; // 记录最短路的路径 int source; // 源点 int vertex_num; // 顶点数 int edge_num; // 边数 int destination; // 终结点 void Dijkstra(int source) { memset(visited, 0, sizeof(visited)); // 初始化标记数组 visited[source] = true; for (int i = 0; i < vertex_num; i++) { dist[i] = matrix[source][i]; path[i] = source; } int min_cost; // 权值最小 int min_cost_index; // 权值最小的下标 for (int i = 1; i < vertex_num; i++) // 找到源点到另外 vertex_num-1 个点的最短路径{ min_cost = INT_MAX;

单源最短路径的Dijkstra算法

单源最短路径的Dijkstra算法: 问题描述: 给定一个带权有向图G=(V,E),其中每条边的权是非负实数。另外,还给定V中的一个顶点,称为源。现在要计算从源到所有其他各顶点的最短路长度。这里路的长度是指路上各边权之和。这个问题通常称为单源最短路径问题。 算法描述: Dijkstra算法是解单源最短路径的一个贪心算法。基本思想是:设置顶点集合S并不断地做贪心选择来扩充这个集合。一个顶点属于S当且仅当从源到该顶点的最短路径长度已知。初始时,S中仅含有源。设u是G的某一个顶点,把从源到u且中间只经过S中顶点的路称为从源到u的特殊路径,并用数组dist记录当前每个顶点所对应的最短特殊路径长度。Dijkstra算法每次从V-S中取出具有最短特殊路长度的顶点u,将u添加到S中,同时对数组dist做必要的修改。一旦S包含了所有V中顶点,dist就记录了从源到所有其他顶点之间的最短路径长度。 源代码: #include #define MAX 1000 #define LEN 100 int k=0, b[LEN];

using namespace std; //-------------------------------------数据声明------------------------------------------------//c[i][j]表示边(i,j)的权 //dist[i]表示当前从源到顶点i的最短特殊路径长度 //prev[i]记录从源到顶点i的最短路径上的i的前一个顶点 //--------------------------------------------------------------------------------------------- void Dijkstra(int n, int v, int dist[], int prev[], int c[][LEN]) { bool s[LEN]; // 判断是否已存入该点到S集合中 for (int i = 1; i <= n; i++) { dist[i] = c[v][i]; s[i] = false; //初始都未用过该点 if (dist[i] == MAX) prev[i] = 0; //表示v到i前一顶点不存在 else prev[i] = v; } dist[v] = 0; s[v] = true;

图论算法 最大流算法和最大匹配算法

最大流算法 clc,clear,M=1000; c(1,2)=3;c(1,4)=3; c(2,3)=1;c(2,4)=20; c(3,6)=3; c(4,5)=10; c(5,1)=4;c(5,3)=2;c(5,6)=13; n=length(u); list=[]; maxf=zeros(1:n);maxf(n)=1; while maxf(n)>0 maxf=zeros(1,n);pred=zeros(1,n); list=1;record=list;maxf(1)=M; while (~isempty(list))&(maxf(n)==0) flag=list(1);list(1)=[]; index1=(find(u(flag,:)~=0)); label1=index1(find(u(flag,index1)... -f(flag,index1)~=0)); label1=setdiff(label1,record); list=union(list,label1); pred(label1(find(pred(label1)==0)))=flag; maxf(label1)=min(maxf(flag),u(flag,label1)... -f(flag,label1)); record=union(record,label1); label2=find(f(:,flag)~=0); label2=label2'; label2=setdiff(label2,record); list=union(list,label2); pred(label2(find(pred(label2)==0)))=-flag; maxf(label2)=min(maxf(flag),f(label2,flag)); record=union(record,label2); end if maxf(n)>0 v2=n; v1=pred(v2); while v2~=1 if v1>0

单源最短路径

单源最短路径问题 I 用贪心算法求解 贪心算法是一种经典的算法,通常以自顶向下的方式进行,以迭代的方式作出相继的贪心选择,每作一次贪心选择就将所求问题简化为规模更小的子问题。一般具有2个重要的性质:贪心选择性质和最优子结构性质。 一、问题描述与分析 单源最短路径问题是一个经典问题,给定带权有向图G =(V,E),其中每条边的权是非负实数。另外,还给定V中的一个顶点,称为源。现在要计算从源到所有其他各顶点的最短路长度。这里路的长度是指路上各边权之和。这个问题通常称为单源最短路径问题。 分析过程:运用Dijkstra算法来解决单源最短路径问题。 具备贪心选择性质 具有最优子结构性质 计算复杂性 二、算法设计(或算法步骤) 用贪心算法解单源最短路径问题: 1.算法思想: 设置顶点集合S并不断地作贪心选择来扩充这个集合。一个顶点属于集合S当且仅当从源到该顶点的最短路径长度已知。初始时,S中仅含有源。设u是G的某一个顶点,把从源到u且中间只经过S中顶点的路称为从源到u的特殊路径,并用数组dist记录当前每个顶点所对应的最短特殊路径长度。Dijkstra算法每次从V-S中取出具有最短特殊路长度的顶点u,将u添加到S中,同时对数组dist作必要的修改。一旦S包含了所有V中顶点,dist就记录了从源到所有其他顶点之间的最短路径长度。 2.算法步骤: (1) 用带权的邻接矩阵c来表示带权有向图, c[i][j]表示弧上的权值. 若?V,则置c[i][j]为∞。设S为已知最短路径的终点的集合,它的初始状态为空集。 从源点v到图上其余各点vi的当前最短路径长度的初值为:dist[i]=c[v][i] vi∈V。 (2) 选择vj, 使得dist[j]=Min{dist[i] | vi∈V-S},vj就是长度最短的最短路径的终点。令

算法导论第二章答案

第二章算法入门 由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。 给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。 插入排序算法伪代码 INSERTION-SORT(A) 1 for j ← 2 to length[A] 2 do key ←A[j] 3 Insert A[j] into the sorted sequence A[1..j-1] 4 i ←j-1 5 while i > 0 and A[i] > key 6 do A[i+1]←A[i] 7 i ←i ? 1 8 A[i+1]←key C#对揑入排序算法的实现: public static void InsertionSort(T[] Input) where T:IComparable { T key; int i; for (int j = 1; j < Input.Length; j++) { key = Input[j]; i = j - 1; for (; i >= 0 && Input[i].CompareTo(key)>0;i-- ) Input[i + 1] = Input[i]; Input[i+1]=key; } } 揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j] 这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1. 如果按照大部分计算机编程语言的思路,修改为: INSERTION-SORT(A) 1 for j ← 1 to length[A] 2 do key ←A[j] 3 i ←j-1

最大流算法小结

网络最大流的算法 网络最大流的算法分类: 一、Ford-Fulkerson增广路方法 1、Ford-Fulkerson标号算法(最简单的实现) 分别记录这一轮扩展过程中的每个点的前驱与到该节点的增广最大流量,从源点开始扩展,每次选择一个点(必须保证已经扩展到这个点),检查与它连接的所有边,并进行扩展,直到扩展到t。 2、最大容量增广路算法 每次找一条容量最大的增广路来增广,找的过程类似Dijkstra,实现起来相当简单。 3、Edmonds-Karp,最短路增广算法的BFS实现 每次找一条最短的增广路,BFS是一个可以很方便的实现思想。 4、距离标号算法 最短路增广的O(n)寻找实现,使用距离函数d:d[t]=0;d<=d[j]+1若存在(i,j)∈E;只有路径上满足d=d[i+1]+1的增广路才为满足要求的,一开始我们初始化使标号恰好满足要求,之后不断更改标号使其可以使增广继续。 5、Dinic,分层思想 对网络分层(按照距t的距离),保留相邻层之间的边,然后运用一次类似于距离标号的方法(其实质是DFS)进行增广。 二、预留与推进算法 1、一般性算法 随便找个点,要么将他的盈余推出去,要么对他进行重标记,直至无活跃点为止。 2、重标记与前移算法 维护一个队列,对一个点不断进行推进与重标记操作,直至其盈余为0,若过程中他没有被重标记过,则可出列,否则加入队头,继续等待检查。 3、最高标号预留与推进算法 记录d值,然后优先处理d值较高的,直至没有盈余。

网络最大流的算法实现 一、Edmonds-Karp(EK)算法 就是用广度优先搜索来实现Ford-Fulkerson方法中对增广路径的计算,时间复杂度为O(VE2),Shortest Augmenting Path (SAP) 是每次寻找最短增广路的一类算法,Edmonds - Karp 算法以及后来著名的Dinic 算法都属于此。SAP 类算法可统一描述如下: Shortest Augmenting Path { x <-- 0 while 在残量网络Gx 中存在增广路s ~> t do { 找一条最短的增广路径P delta <-- min{rij:(i,j) 属于P} 沿P 增广delta 大小的流量 更新残量网络Gx } return x } 在无权边的有向图中寻找最短路,最简单的方法就是广度优先搜索(BFS),E-K 算法就直接来源于此。每次用一遍BFS 寻找从源点s 到终点t 的最短路作为增广路径,然后增广流量 f 并修改残量网络,直到不存在新的增广路径。 E-K 算法的时间复杂度为O(V*E^2),由于BFS 要搜索全部小于最短距离的分支路径之后才能找到终点,因此可以想象频繁的BFS 效率是比较低的。实践中此算法使用的机会较少。代码如下: #define VMAX 201 int n, m; //分别表示图的边数和顶点数 int c[VMAX][VMAX]; //容量 int Edmonds_Karp( int s, int t ) //输入源点和汇点 { int p, q, queue[VMAX], u, v, pre[VMAX], flow= 0, aug; while(true) { memset(pre,-1,sizeof(pre)); //记录父节点 for( queue[p=q=0]=s; p<=q; p++ ) //广度优先搜索 { u= queue[p]; for( v=0; v0 && pre[v]<0 ) pre[v]=u, queue[++q]=v; if( pre[t]>=0 ) break; } if( pre[t]<0 ) break; //不存在增广路 aug= 0x7fff; //记录最小残留容量 for( u=pre[v=t]; v!=s; v=u,u=pre[u] ) if(c[u][v]

单源最短路径(两种方法)

单源最短路径 计科一班李振华 2012040711 1、问题描述 给定带权有向图G=(V,E),其中每条边的权是非负实数。另外,还给定V中的一个顶点,称为源。现在要计算从源到其他所有顶点的最短路长度。这里路的长度是指路上各边权之和。这个问题通常称为单源最短路径问题。 2、问题分析 推导过程(最优子结构证明,最优值递归定义) 1、贪心算法 对于图G,如果所有Wij≥0的情形下,目前公认的最好的方法是由Dijkstra 于1959年提出来的。 已知如下图所示的单行线交通网,每弧旁的数字表示通过这条单行线所需要的费用,现在某人要从v1出发,通过这个交通网到v8去,求使总费用最小的旅行路线。 Dijkstra方法的基本思想是从vs出发,逐步地向外探寻最短路。执行过程中,与每个点对应,记录下一个数(称为这个点的标号),它或者表示从vs 到该点的最短路的权(称为P标号)、或者是从vs到该点的最短路的权的上界(称为T标号),方法的每一步是去修改T标号,并且把某一个具T标号的改变为具 P标号的点,从而使G中具P标号的顶点数多一个,这样至多经过n-1(n为图G的顶点数)步,就可以求出从vs到各点的最短路。 在叙述Dijkstra方法的具体步骤之前,说明一下这个方法的基本思想。s=1。因为所有Wij≥0,故有d(v1, v1)=0。这时,v1是具P标号的点。现在考察从v1发出的三条弧,(v1, v2), (v1, v3)和(v1, v4)。 (1)如果某人从v1出发沿(v1, v2)到达v2,这时需要d(v1, v1)+w12=6单位的费用; (2)如果他从v1出发沿(v1, v3)到达v3,这时需要d(v1, v1)+w13=3单位的费用; (3)若沿(v1, v4)到达v4,这时需要d(v1, v1)+w14=1单位的费用。 因为min{ d(v1, v1)+w12,d(v1, v1)+w13,d(v1, v1)+w14}= d(v1, v1)+w14=1,可以断言,他从v1到v4所需要的最小费用必定是1单位,即从v1到v4的最短路是(v1, v4),d(v1, v4)=1。这是因为从v1到v4的任一条路P,如果不是(v1, v4),则必是先从v1沿(v1, v2)到达v2,或者沿(v1, v3)到达v3。但如上所说,这时他已需要6单位或3单位的费用,不管他如何再从v2或从v3到达v4,所需要的总费用都不会比1小(因为所有wij≥0)。因而推知d(v1, v4)=1,这样就可以使v4变成具P标号的点。 (4)现在考察从v1及v4指向其余点的弧,由上已知,从v1出发,分别沿(v1, v2)、(v1, v3)到达v2, v3,需要的费用分别为6与3,而从v4出发沿(v4, v6)到达v6所需的费用是d(v1, v4)+w46=1+10=11单位。因min{ d(v1, v1)+w12,d(v1, v1)+w13,d(v1, v4)+w46}= d(v1, v1)+w13=3。基于同样的理由可以断言,从v1

从一道题目的解法试谈网络流的构造与算法

从一道题目的解法试谈网络流的构造与算法 福建师大附中江鹏 1. 引论 A. 对网络流算法的认识 网络流算法是一种高效实用的算法,相对于其它图论算法来说,模型更加复杂,编程复杂度也更高,但是它综合了图论中的其它一些算法(如最短路径),因而适用范围也更广,经常能够很好地解决一些搜索与动态规划无法解决的,看似NP的问题。 B. 具体问题的应用 网络流在具体问题中的应用,最具挑战性的部分是模型的构造。这没用现成的模式可以套用,需要对各种网络流的性质了如指掌(比如点有容量、容量有上下限、多重边等等),并且归纳总结一些经验,发挥我们的创造性。

2. 例题分析 【问题1】项目发展规划(Develop) Macrosoft?公司准备制定一份未来的发展规划。公司各部门提出的发展项目汇总成了一张规划表,该表包含了许多项目。对于每个项目,规划表中都给出了它所需的投资或预计的盈利。由于某些项目的实施必须依赖于其它项目的开发成果,所以如果要实施这个项目的话,它所依赖的项目也是必不可少的。现在请你担任Macrosoft?公司的总裁,从这些项目中挑选出一部分,使你的公司获得最大的净利润。 ●输入 输入文件包括项目的数量N,每个项目的预算Ci和它所依赖的项目集合Pi。格式如下:第1行是N; 接下来的第i行每行表示第i个项目的信息。每行的第一个数是Ci,正数表示盈利,负数表示投资。剩下的数是项目i所依赖的项目的编号。 每行相邻的两个数之间用一个或多个空格隔开。 ●输出 第1行是公司的最大净利润。接着是获得最大净利润的项目选择方案。若有多个方案,则输出挑选项目最少的一个方案。每行一个数,表示选择的项目的编号,所有项目按从小到大的顺序输出。 ●数据限制 0≤N≤1000 -1000000≤Ci≤1000000 ●输入输出范例

算法-单源点最短路径-Dijkstra算法

实验单源点最短路径

一、实验目的 1、深入理解贪心策略的基本思想。 2、能正确采用贪心策略设计相应的算法,解决实际问题。 3、掌握贪心算法时间空间复杂度分析,以及问题复杂性分析方法 二、实验内容 单源最短路径 三、设计分析 单源点最短路径 Dijkstra算法是解单源点最短路径的一个贪心算法。其基本思想是,设置顶点集合S并不断地做贪心选择来扩充这个集合。一个顶点属于集合当且仅当从源点到该顶点的最短路径长度已知。初始时,S中仅含有源。设u是G的某一个顶点,把从源到u且中间只经过S中顶点的路称为从源到u的特殊路径,并用数组dist记录当前每个顶点所对应的最短路径特殊长度。Dijkstra算法每次从V-S中取出具有最短特殊路长度的顶点u,将u添加到S中,同时对数组dist做必要的修改。一旦S包括了所有V中顶点,dist就记录了从源到所有其它顶点之间的最短路径长度。存在一个带权有向图。 四、算法描述及程序 #include "stdafx.h" #include "iostream" using namespace std; #define N 5 #define MAX 1000 int edge1[7] = { 1, 1, 1, 2, 3, 4, 4 }; int edge2[7] = { 2, 5, 4, 3, 5, 3, 5 }; int length[7] = { 10, 100, 30, 50, 10, 20, 60 }; int c[N][N]; template void Dijkstra(int n, int v, T dist[], int prev[]) { bool s[MAX]; for (int i = 1; i <= n; i++) { dist[i] = c[v][i];

实验1.-贪心法求解单源最短路径问题

实验1. 贪心法求解单源最短路径问题 实验内容 本实验要求基于算法设计与分析的一般过程(即待求解问题的描述、算法设计、算法描述、算法正确性证明、算法分析、算法实现与测试)。应用贪心策略求解有向带权图的单源最短路径问题。 实验目的 通过本次实验,掌握算法设计与分析的一般过程,以及每个步骤的基本方法。并应用贪心法求解单源最短路径问题。 环境要求 对于环境没有特别要求。对于算法实现,可以自由选择C, C++, Java,甚至于其他程序设计语言。 实验步骤 步骤1:理解问题,给出问题的描述。 步骤2:算法设计,包括策略与数据结构的选择 步骤3:描述算法。希望采用源代码以外的形式,如伪代码、流程图等; 步骤4:算法的正确性证明。需要这个环节,在理解的基础上对算法的正确性给予证明; 步骤5:算法复杂性分析,包括时间复杂性和空间复杂性; 步骤6:算法实现与测试。附上代码或以附件的形式提交,同时贴上算法运行结果截图; 步骤7:技术上、分析过程中等各种心得体会与备忘,需要言之有物。 说明:步骤1-6在“实验结果”一节中描述,步骤7在“实验总结”一节中描述。 实验结果 1.问题描述 给定一个有向带全图G=(V,E),其中每条边的权是一个非负实数。另外,给定V中的一个顶点,称为源点。现在要计算源点到所有其他各个顶点的最短路径长度,这里的路径长度是指路径上所有经过边的权值之和。这个问题通常称为单源最短路径问题。 2.(1)Dijkstra算法思想 按各个结点与源点之间路径长度的非减次序,生成源点到各个结点的最短路径的方法。

即先求出长度最短的一条路径,再参照它求出长度次短的一条路径。依此类推,直到从源点到其它各结点的最短路径全部求出为止。 1959年提出的,但当时并未发表。因为在那个年代,算法基本上不被当做一种科学研究的问题。 (2)Dijkstra算法设计 集合S与V-S的划分:假定源点为u。集合S中的结点到源点的最短路径的长度已经确定,集合V-S中所包含的结点到源点的最短路径的长度待定。 特殊路径:从源点出发只经过S中的结点到达V-S中的结点的路径。 贪心策略:选择特殊路径长度最短的路径,将其相连的V-S中的结点加入到集合S中。3、描述算法 Dijkstra算法的伪代码: DIJKSTRA(G, w, s) INITIALIZE-SINGLE-SOURCE(G, s) S = Φ Q = G.V //V-S中的结点按特殊路径长度非减排序 while Q ≠Φ u = EXTRACT-MIN(Q) S = S ∪{u} for each v∈G.Adj[u] RELAX(u, v, w) 4、Dijkstra算法的求解步骤: 步骤1:设计合适的数据结构。带权邻接矩阵C记录结点之间的权值,数组dist来记录从源点到其它顶点的最短路径长度,数组p来记录最短路径。u为源点; 步骤2:初始化。令集合S={u},对于集合V-S中的所有顶点x,设置dist[x]=C[u][x]。如果顶点x与源点相邻,设置p[x]=u;否则,p[x]=-1; 步骤3:贪心选择结点。在集合V-S中依照贪心策略来寻找使得dist[x]具有最小值的顶点t,t就是集合V-S中距离源点u最近的顶点。 步骤4:更新集合S和V-S。将顶点t加入集合S中,同时更新集合V-S; 步骤5:判断算法是否结束。如果集合V-S为空,算法结束。否则,转步骤6; 步骤6:对相关结点做松弛处理。对集合V-S中的所有与顶点t相邻的顶点x,如dist[x]>dist[t]+C[t][x],则dist[x]=dist[t]+C[t][x]并设置p[x]=t。转步骤3。 5、Dijkstra算法的正确性证明–贪心选择性质: 采用归纳法。当S={s, p}时,则除源结点s之外的所有结点中,结点p到源点s的距离最短。这是显然的。 假设当S={s, p1, …, pk}时,即k个结点p1, …, pk到源点s的距离最短。当S={s, p1, …, pk, pk+1}时,很显然结点pk+1到源点s的距离是最短的。需证明:此时结点p1, …, pk到源点s的距离仍然是最短的。用反证法假设当结点pk+1加入到S后,pi结点经由结点pk+1到源点s的距离更短,即d(s, pk+1) + d(pk+1, pi) < d(s, pi),有d(s, pk+1) < d(s, pi) ,则结点pk+1

(毕业设计论文)最大流问题及应用

山东科技大学 本科毕业设计(论文)题目最大流问题以及应用 学院名称数学与系统科学学院 专业班级信息与计算科学2011级2班学生姓名吕永强 学号201101051416

摘要 网络流问题是运筹学的重要研究课题。最大流问题是网络流问题的一个重要的内容,应用极为广泛。研究最大流问题并将其应用到工业、工程、商业、农业,运输业等领域可给我们的生活带来很大方便。 本论文讨论最大流问题,综述图论的历史背景、基本概念和基本知识;阐述网络的基本概念;介绍最大流问题的核心依据——Ford-Fulkerson最大流最小割定理;综述解决最大流问题的几种算法Ford-Fulkerson标号法、Edmonds-Karp修正算法、Dinic算法,并比较各算法在解决不同问题中的优劣。 为了更加明确的展现最大流问题在生产生活中的应用,本文例举了一个实际生活中的问题——铁路货运列车的最优调度来突出研究最大流问题的重要意义,此实例需要求解的是在一定的限制条件下,设计出一个在一昼夜间能通过某段铁路的最多的货运列车数量并列出每辆列车开出的时 刻表。在此实例中,通过从实际问题中抽象出网络图,将实际问题转化为最大流问题并应用图的性质和Ford-Fulkerson标号法的算法依据,最终解决了问题。 本文采用理论与实例相结合,重在应用理论依据解决实际问题,具有较强的实践性,突出的是应用。

Abstract The network flow problem is an important operational research subject. The maximum flow problem is an important content of network flow problem, which has widely applications. The research of maximum flow problem and its applications to industry, engineering,commerce, agriculture, transportation and other areas can bring us great convenience. The paper discusses the maximum flow problem, and summarizes the historical background of graph theory, basic concepts, basic knowledge and describes the basic concept of the network. The core basis of the maximum flow problem -- Ford-Fulkerson maximum flow minimum cut theorem is introduced. Several algorithms for solving maximal-flow problem like Ford-Fulkerson labeling algorithm, Edmonds-Karp correct algorithm, Dinic algorithm are summarized in this paper. It also compares various algorithms to solve different problems in the pros and cons. In order to more clearly show the application of the maximum flow problem in the production life, the paper illustrates a real-life problem - -The optimal scheduling of railway freight train to highlight the importance of maximum flow. This instance is to be solved under certain constraints , to design the most freight train numbers through the railway in a day and night and to list out the schedules for each train. In this instance, by abstracting the network diagram from the real problems, transform the actual problem into the maximum flow problem, and use the properties of graph and Ford-Fulkerson labeling algorithm, and ultimately solve the problem. In this paper, the combination of theory and examples focus on solving practical problems by applying theoretical basis. It has strong practicality and

相关文档
最新文档