【Topic】Network Stream

What is network flow?

Network-flows are a problem-solving method analogous to water flow and are closely related to linear programming. The theory and application of network flow are developing continuously, and new topics such as flow with gain, multi-terminal flow, multi-commodity flow, and decomposition and synthesis of network flow have appeared. The application of network flow has spread to many fields such as communication, transportation, electric power, engineering planning, task assignment, equipment updating and computer-aided design.

A theory and method in graph theory that studies a class of optimization problems on networks. In 1955, TE Harris first proposed the problem of seeking the maximum transportation volume between two points on a given network when he studied the maximum flux of railways. In 1956, LR Ford and DR Fulkerson and others gave algorithms to solve such problems, thus establishing the network flow theory. The so-called network or capacity network refers to a connected weighted directed graph D = (V, E, C), where V is the vertex set of the graph, E is the directed edge (ie arc) set, and C is the arc capacity. In addition, the vertex set includes a start point and an end point. The flow on the network is the feasible flow from the starting point to the ending point. This is a non-negative function defined on the network. On the one hand, it is limited by the capacity. On the other hand, except for the starting point and the ending point, it is required to maintain the inflow and Outflow is balanced. If the following figure is regarded as a road network, the vertices v1...v6 represent 6 towns, and the weight on each edge represents the length of the road between the two towns. Now we need to ask: If the materials are transported from the starting point v1 to the end point v6, which route should be selected to make the total transport distance the shortest? Such a type of problem is called the shortest path problem. If the above figure is regarded as an oil pipeline network, v1 represents the sending point, v6 represents the receiving point, and the other points represent the transfer station. The weight of each side represents the maximum transportation volume of the pipeline. Now we need to ask how to arrange the oil pipeline to maximize the total transportation volume from v1 to v6. Such a problem is called a maximum flow problem.
It can be said that the above is nonsense~

Maximum flow-minimum split

1.Dinic

The Dinic algorithm is one of the optimization algorithms for the maximum flow of the network flow. At each step, the original image is layered, and then DFS is used to find the augmentation path. The time complexity is O(n^2*m). The Dinic algorithm can be divided into n stages at most, and each stage includes two parts: building a hierarchical network and finding an augmentation path.
The idea of ​​the Dinic algorithm is to expand in a hierarchical network in stages. It differs from the shortest augmenting path algorithm in that: after the shortest augmenting path performs a BFS augmentation in each stage, the BFS needs to be restarted to find another augmenting path from the source point Vs; while in the Dinic algorithm, only Multiple augmentations can be achieved with one BFS process.

Hierarchy

A hierarchical graph is a graph in which the points in the original graph are divided into "layers" according to the distance from the point to the source, and only the edges between different layers are retained.

Algorithm flow

1. Calculate the hierarchical graph according to the residual network.
2. Use DFS to augment the hierarchical graph until there is no augmentation path.
3. Repeat the above steps until the augmentation cannot be achieved.

time complexity

Because in the execution process of Dinic, each time the layers are re-layered, the layers where the sinks are located are strictly increasing, and the hierarchical graph of n points has at most n layers, so the layers are re-layered at most n times. In the same hierarchical graph, because each augmentation path has a bottleneck, and the bottlenecks of two augmentations cannot be the same, there are at most m augmentation paths. When searching each augmenting path, both forward and backtrack are at most n times, so the time complexity caused by both is O(nm); and it is impossible to enumerate twice along the same edge (i, j), because When enumerating for the first time, either the capacity of this edge has been exhausted, or there is no path from point j to the sink, so it can be deleted from this hierarchical graph. In summary, the theoretical bound on the time complexity of the Dinic algorithm is O ( n 2 m )

#include<cstdio>
#include<iostream>
#include<cstring>
#define INF 1e10
using namespace std;
struct moon{
    long long next,last,len,tov;
};
moon edge[1001];
long long n,m,ans,tot,sum;
int dis[201];//最短路(用于优化)
int f[10001];//队列
void insert(int x,int y,int z)
{
    edge[++tot].tov=y;
    edge[tot].len=z;
    edge[tot].next=edge[x].last;
    edge[x].last=tot;
}
bool bfs()
{
    int head=0,tail=1,x,y;
    memset(dis,0xff,sizeof(dis));//初值为-1
    f[1]=1;dis[1]=0;
    while(head<tail)
    {
        x=f[++head];
        for (int i=edge[x].last;i;i=edge[i].next)
        {
            y=edge[i].tov;
            if(dis[y]<0&&edge[i].len>0)
            {
                dis[y]=dis[x]+1;
                f[++tail]=y;
            }   
        }   
    }
    return dis[m]>0;//看看是否还能到达汇点
}
int dfs(int t,long long s)
{
    if(t==m) return s;
    int y;
    long long flow=0;
    for (int i=edge[t].last;i;i=edge[i].next)
    {
        y=edge[i].tov;
        if(edge[i].len>0&&dis[y]==dis[t]+1)
        {
            flow=dfs(y,min(s,edge[i].len))//最可行流量
            if(flow)
            {
                edge[i].len-=flow;
                edge[i^1].len+=flow;//i^1表示这条边的反向边
                return flow;
            }
        }
    }
}
int main()
{
    scanf("%lld%lld",&n,&m);
    int i,j,x,y,z;tot=1;
    for (i=1;i<=n;++i)
    {
        scanf("%d%d%d",&x,&y,&z);
        insert(x,y,z);//正向边
        insert(y,x,0);//反向边
    }
    while(bfs())
    {
        sum=dfs(1,INF);
        while (sum) 
        {
            ans+=sum;
            sum=dfs(1,INF);
        }
    }
    printf("%lld\n",ans);
}

2.SAP

A relatively good algorithm.
The difference between it and dinic is that dinic needs to find out whether there is an augmentation path every time bfs, but SAP does not use it, and its distance label will be added directly every time it is updated. The idea of ​​answering questions is similar to that of Dinic.

time complexity

The theoretical time complexity is O ( n 2 m ) , but it is far from this complexity in practical applications.

optimization:

  • The cur (current arc) is optimized, and the arc that has been full every time will not go away.
  • GAP optimization, every time if the distance label shows a fault, it means that there is no more flow to reach the back, and exit directly.
#include<cstdio>
#include<iostream>
#define INF 1<<30
using namespace std;
int n,m,x,y,z,tot;
int b[1001];//距离标号
int gap[1001];//gap[i]表示距离标号为i的点的个数 
int cur[1001];//当前弧优化
int last[1001],tov[1000010],next[1000010],len[1000010];
int ans,S/*源*/,T/*汇*/;
void insert(int x,int y,int z)
{
    tov[++tot]=y;
    len[tot]=z;
    next[tot]=last[x];
    last[x]=tot;
}
int flow(int t,int Flow)
{
    if(t==T) return Flow;
    int have=0;
    for (int i=cur[t];i;i=next[i])
    {
        int y=tov[i];
        if(len[i]>0&&b[t]==b[y]+1)
        {
            cur[t]=i;
            int now=flow(y,min(Flow-have,len[i]));
            len[i]-=now;len[i^1]+=now;
            have+=now;
        }
        if(Flow==have) return have;
    }
    cur[t]=last[t];
    if(--gap[b[t]]==0) b[S]=n;
    ++gap[++b[t]];
    return have;
}
int main()
{
    scanf("%d%d",&n,&m);
    int i,j;tot=1;
    for (i=1;i<=n;++i)
    {
        scanf("%d%d%d",&x,&y,&z);
        insert(x,y,z);
        insert(y,x,0);
    }   
    S=1,T=gap[0]=m;
    while (b[S]<m) 
        ans+=flow(S,INF);
    printf("%d\n",ans);
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326046328&siteId=291194637