Minimum cost maximum flow

Source: Internet
Author: User

I recently learned the minimum cost maximum flow;

I'd like to talk a little about the maximum flow of minimum cost.

What is the minimum cost maximum flow?

We can think of the minimum cost as the shortest way;

Simple!!!

That is, an edge has two weights;

One for the edge, the other for the flow value;

What is the maximum flow?

When we find the minimum cost;

In essence, the shortest path is obtained;

At this time the maximum flow is the minimum value of the flow value for each edge of the shortest path;

At this time the minimum cost maximum flow is the shortest length * maximum flow;

How to find the minimum cost?

Very simple!!!!!

We are seeking the shortest path of single source;

We can write SPFA Dijkstra Bellman-ford Floyd (This is dead)

Each time we find the maximum flow of the minimum cost;

We should reverse all sides of the shortest road;

The Benquan of the opposite edge is the inverse number of the forward Benquan corresponding to it;

The flow value of the forward edge should be subtracted from the maximum stream, and the flow value of the reverse edge should be the maximum stream (the stream value starts at 0);

Oh! Don't forget that the weighted value of the edge with the flow value of 0 for the forward edge should be infinity; (prevent the shortest path of repetition);

We always do this until the starting point is not connected to the end, at the exit, the minimum cost of each time the maximum flow Plus is the total minimum cost maximum flow;


For reference:

#include <stdio.h>

#include <stdlib.h>
#define MAX1 1000000000
int a[1000][1000],f[1000][1000];
int d[1000];//d indicates a specific edge distance
int p[1000],h[1000];//p represents the permanent edge distance
int i,j,sum=0,ans=0;
int m;//m represents the number of sides
int n,g;//n Representative points
int dfs (int k,int H1)
{
Int J;
if (f[h[k]][k]

A[K][H[K]]=-A[H[K]][K];
printf ("%d", h[k]);
if (h[k]==1) return H1;
J=dfs (H[K],H1);
Return J;
}
int Cherk (int t,int k)
{
F[h[t]][t]-=k;f[t][h[t]]+=k;
if (f[h[t]][t]==0) a[h[t]][t]=max1;
if (h[t]==1) return 0;
Cherk (H[T],K);
return 0;
}
int main ()
{
Freopen ("Dj.in", "R", stdin);
Freopen ("Dj.out", "w", stdout);
scanf ("%d%d", &n,&m);
int min1,x,y,z,w,k,o=0;
for (i=1;i<=m;i++)
{
scanf ("%d%d%d%d", &x,&y,&z,&w);//z is the stream, W is the cost;
A[x][y]=w;f[x][y]=z;
A[y][x]=-w;
f[y][x]=0;
}
for (O=1;;o ++)
{
k=0;
for (i=1;i<=n;i++)
D[I]=MAX1;
d[1]=0;
for (i=1;i<=n;i++)
{h[i]=0;p[i]=0;}
for (i=1;i<=n;i++)
{
MIN1=MAX1;
for (j=1;j<=n;j++)
if (!p[j]&&d[j]<min1)
{
MIN1=D[J];
K=j;
}
P[k]=1;
for (j=1;j<=n;j++)
if (A[k][j]!=0&&!p[j]&&d[j]>d[k]+a[k][j])
{d[j]=d[k]+a[k][j];h[j]=k;}
}
if (D[N]&GT;=MAX1)
Break
k=548566;
for (i=1;i<=n;i++)
printf ("%d", h[i]);
printf ("%d", &h[i]);
K=dfs (N,K);
Cherk (N,K);
Sum+=k*d[n];ans+=k;
printf ("%d%d\n", k,d[n]);
}
printf ("%d%d", ans,sum);
for (i=1;i<=n;i++)
printf ("%d", d[i]);
Fclose (stdin);
Fclose (stdout);
while (1);
return 0;
}

Minimum cost maximum flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.