Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №6 / MDLab6-MaxFlow
.C#include <stdio.h>
#include <alloc.h>
#include <conio.h>
struct List{
int v;
int w;
int f;
struct List *next;
};
struct Graph{
int p;
int c;
struct List *first;
struct List *last;
}*G;
int N;
int BFS();
void MinCut();
void MaxFlow();
void Menu();
void ListAdj();
void FreeList();
void main()
{
Menu();
}
void MaxFlow()
{
int u,v,addtoflow,maxflow=0;
struct List *c;
while(addtoflow=BFS()){
v=N-1;
u=G[v].p;
while(u!=-1){
c=G[u].first;
while(c->v!=v)
c=c->next;
c->f+=addtoflow;
v=u;
u=G[u].p;
}
}
c=G[0].first;
while(c!=G[0].last){
maxflow+=c->f;
c=c->next;
}
printf("Maximum flow is %d",maxflow);
MinCut();
}
void MinCut()
{
/* int i;
struct List *c;
printf("\nMinumum cut: ");
for(i=0;i<N;i++)
if(!G[i].c){
c=G[i].first;
while(c!=G[i].last){
if(G[c->v].c)
printf("%d-%d ",i+1,c->v+1);
c=c->next;
}
} */
}
int BFS()
{
int i,mw=0,v,u,qbeg=0,qend,*Q=(int *)malloc(N*sizeof(int ));
struct List *c;
for(i=0;i<N;i++){
G[i].c=1;
G[i].p=-1;
}
Q[qbeg]=0;
qend=1;
while(qbeg!=qend){
u=Q[qbeg++];
c=G[u].first;
while(c!=G[u].last){
if(G[c->v].c && c->w!=c->f){
G[c->v].c=0;
G[c->v].p=u;
Q[qend++]=c->v;
}
c=c->next;
}
}
v=N-1;
u=G[v].p;
while(u!=-1){
c=G[u].first;
while(c->v!=v)
c=c->next;
if(!mw || mw>c->w-c->f)
mw=c->w-c->f;
v=u;
u=G[u].p;
}
return mw;
}
void Menu()
{
int i;
char ch;
clrscr();
printf("1.To enter the graph\n");
printf("2.Maximum flow by Ford-Fulkerson's algorithm\n");
printf("3.Exit\n");
do
ch=getch();
while(ch<'1' || ch>'3');
clrscr();
switch (ch)
{
case '1': ListAdj();break;
case '2': MaxFlow();getch();break;
case '3': FreeList();return;
}
Menu();
}
void ListAdj()
{
int i,v,w;
struct List *c;
if(G)
FreeList();
printf("Enter number of vertexes of graph: ");
scanf("%d",&N);
G=(struct Graph *)malloc(N*sizeof(struct Graph));
printf("\n");
for(i=0;i<N;i++){
printf("%d->",i+1);
G[i].first=(struct List*)malloc(sizeof(struct List));
G[i].last=G[i].first;
G[i].last->next=NULL;
G[i].last->v=-1;
scanf("%d",&v);
while(v){
scanf("%d",&w);
G[i].last->v=v-1;
G[i].last->w=w;
G[i].last->f=0;
G[i].last->next=(struct List*)malloc(sizeof(struct List));
G[i].last=G[i].last->next;
G[i].last->next=NULL;
G[i].last->v=-1;
scanf("%d",&v);
}
}
}
void FreeList()
{
struct List *c,*t;
while(N--){
c=G[N].first;
while(c!=G[N].last){
t=c->next;
free(c);
c=t;
}
}
free(G);
}
Соседние файлы в папке Лабораторная работа №6