Dinic(隣接テーブル実装)+現在のアーク最適化

34829 ワード

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は毎回中の値を変更して次回1本の辺が探し出せません.
多くの問題解は現在のアーク最適化に使われていますが、cur[]配列の付与に時間がかかり、最適化された時間と消費時間が相殺されるはずです.
ネットワークストリームに出会うたびに私は怖くて、実は難しくありません.つまり、コードは少し多いですが、まだセグメントツリーが多くありません.自分を怖がらせないで、がんばれ!!!
コード:
#include
#include
#include
#include
#include
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

            {
                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;
}