Dinic (邻接表实现) + 当前弧优化

HDU 4280

Island Transport

*Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 16651 Accepted Submission(s): 5061 *

Problem Description

  In the vast waters far far away, there are many islands. People are living on the islands, and all the transport among the islands relies on the ships.
  You have a transportation company there. Some routes are opened for passengers. Each route is a straight line connecting two different islands, and it is bidirectional. Within an hour, a route can transport a certain number of passengers in one direction. For safety, no two routes are cross or overlap and no routes will pass an island except the departing island and the arriving island. Each island can be treated as a point on the XY plane coordinate system. X coordinate increase from west to east, and Y coordinate increase from south to north.
  The transport capacity is important to you. Suppose many passengers depart from the westernmost island and would like to arrive at the easternmost island, the maximum number of passengers arrive at the latter within every hour is the transport capacity. Please calculate it.

Input

  The first line contains one integer T (1<=T<=20), the number of test cases.
  Then T test cases follow. The first line of each test case contains two integers N and M (2<=N,M<=100000), the number of islands and the number of routes. Islands are number from 1 to N.
  Then N lines follow. Each line contain two integers, the X and Y coordinate of an island. The K-th line in the N lines describes the island K. The absolute values of all the coordinates are no more than 100000.
  Then M lines follow. Each line contains three integers I1, I2 (1<=I1,I2<=N) and C (1<=C<=10000) . It means there is a route connecting island I1 and island I2, and it can transport C passengers in one direction within an hour.
  It is guaranteed that the routes obey the rules described above. There is only one island is westernmost and only one island is easternmost. No two islands would have the same coordinates. Each island can go to any other island by the routes.

Output

  For each test case, output an integer in one line, the transport capacity.

Sample Input

2
5 7
3 3
3 0
3 1
0 0
4 5
1 3 3
2 3 4
2 4 3
1 5 6
4 5 3
1 4 4
3 4 2
6 7
-1 -1
0 1
0 2
1 0
1 1
2 3
1 2 1
2 3 6
4 5 5
5 6 3
1 4 6
2 5 5
3 6 4

Sample Output

9
6

Source

2012 ACM/ICPC Asia Regional Tianjin Online

思路

裸的最大流问题,题意什么的不同说 ,关键就是用queue会超时 ,用数组模拟吧

关于当前弧优化

其实就是相当于这条边已经遍历过了,下次就不在遍历 ,我得跳过这条边 ,那么 实现:用 cur[ ] 数组,cur[ u ] = i 每次更改里面的值下次就找不到上一条边了 .

好多题解都用到当前弧优化 ,我用了跟没用耗时差不多这样讲也不对 ,因为cur[]数组赋值时候挺耗时的 ,应该是优化的时间跟耗时的时间抵消了。

每次遇到网络流我都怂,其实也不难 ,也就是代码稍微多一点 ,不过还没有线段树多 。不能吓到自己,加油!!!

撸代码:

#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{

    int nex,to,val,from;
}edge[200010];
struct Point
{

    int x,y;
}p[100010];
int n,m,cnt,head[100010],ss,tt;
int book[100010],cur[100010],Q[400010];
void init()
{
    cnt=0;
    for(int i=0;i<=100005;i++)
        head[i]=-1;
}
void addEdge(int u,int v,int w)
{
    edge[cnt].from=u;
    edge[cnt].to=v;
    edge[cnt].val=w;
    edge[cnt].nex=head[u];
    head[u]=cnt++;

    edge[cnt].from=v;
    edge[cnt].to=u;
    edge[cnt].val=w;
    edge[cnt].nex=head[v];
    head[v]=cnt++;

}
bool bfs()
{
    memset(book,-1,sizeof(book));
    int tail=1,head1=0;
    Q[0]=ss;
    book[ss]=0;
    while(tail>head1)
    {
        int k=Q[head1++];
        for(int i=head[k];i+1;i=edge[i].nex)
        {
            int v=edge[i].to;
            if(book[v]<0&&edge[i].val>0)
            {
                book[v]=book[k]+1;
                Q[tail++]=v;
            }
        }
    }
    if(book[tt]>=0)return 1;
    return 0;
}
int dfs(int v,int sum)/*Dinic*/
{

    if(v==tt)
        return sum;
    int t,s,i,use=0;
    for(int i=cur[v];i+1;i=edge[i].nex)
    {
        cur[v]=i;/*!!!当前弧优化,每次走过这条边以后 ,再也不走了(下次跳过)*/
        int to=edge[i].to;
        if(edge[i].val>0&&book[to]==book[v]+1)
        {
            t=dfs(to,min(sum,edge[i].val));
            use+=t;
            edge[i].val-=t;
            edge[i^1].val+=t;
            sum-=t;
        }
        if(sum<=0)
            break;
    }
    if(!use)book[v]=-2;
    return use;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int minn=0x3f3f3f3f,maxx=-0x3f3f3f3f;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            if(minn>p[i].x)
            {
                minn=p[i].x;
                ss=i;
            }
            else if(maxx<p[i].x)
            {
                maxx=p[i].x;
                tt=i;
            }
        }

        int u,v,w;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addEdge(u,v,w);
        }
        int sum=0;
        while(bfs())
        {
            for(int i=1;i<=n;i++)
                cur[i]=head[i];
            sum+=dfs(ss,0x3f3f3f3f);
        }
        printf("%d ",sum);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/HappyKnockOnCode/p/12678452.html