文章标题 HDU 4280 :Island Transport (最大流--ISAP)

题目链接

一开始用的dinic 发现T了,然后换了个IS AP的模板

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
typedef long long ll;

const int mod=1e9+7;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
const int maxm=1000006;

struct Edge{
    int to,nex,cap,flow;
};

struct ISAP{
    Edge edge[maxm];
    int tol;
    int head[maxn];
    int gap[maxn],dep[maxn],cur[maxn];
    int n,m;

    void init() {
        tol=0;
        memset(head,-1,sizeof(head));
    }

    void addedge(int u,int v,int w,int rw=0) {
        edge[tol].to=v;
        edge[tol].cap=w;
        edge[tol].flow=0;
        edge[tol].nex=head[u];
        head[u]=tol++;
        edge[tol].to=u;
        edge[tol].cap=rw;
        edge[tol].flow=0;
        edge[tol].nex=head[v];
        head[v]=tol++;
    }

    int Q[maxn];

    void BFS(int start,int end) {
        memset(dep,-1,sizeof(dep));
        memset(gap,0,sizeof(gap));
        gap[0]=1;
        int front=0, rear=0;
        dep[end]=0;
        Q[rear++]=end;
        while(front!=rear){
            int u=Q[front++];
            for(int i=head[u];i!=-1;i=edge[i].nex){
                int v=edge[i].to;
                if(dep[v]!=-1)continue;
                Q[rear++]=v;
                dep[v]=dep[u]+1;
                gap[dep[v]]++;
            }
        }
    }
    int S[maxn];

    int sap(int start,int end,int n) {
        BFS(start,end);
        memcpy(cur,head,sizeof(head));
        int top=0;
        int u=start;
        int ans=0;
        while(dep[start]<n) {
            if(u==end) {
                int Min=inf;
                int inser;
                for(int i=0;i<top;i++)
                    if(Min>edge[S[i]].cap-edge[S[i]].flow) {
                        Min=edge[S[i]].cap-edge[S[i]].flow;
                        inser=i;
                    }
                for(int i=0;i<top;i++) {
                    edge[S[i]].flow+=Min;
                    edge[S[i]^1].flow-=Min;
                }
                ans+=Min;
                top=inser;
                u=edge[S[top]^1].to;
                continue;
            }
            bool flag=false;
            int v;
            for(int i=cur[u];i!=-1;i=edge[i].nex) {
                v=edge[i].to;
                if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]) {
                    flag=true;
                    cur[u]=i;
                    break;
                }
            }
            if(flag){
                S[top++]=cur[u];
                u=v;
                continue;
            }
            int Min=n;
            for(int i=head[u];i!=-1;i=edge[i].nex)
                if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min) {
                    Min=dep[edge[i].to];
                    cur[u]=i;
                }
            gap[dep[u]]--;
            if(!gap[dep[u]])return ans;
            dep[u]=Min+1;
            gap[dep[u]]++;
            if(u!=start)u=edge[S[--top]^1].to;
        }
        return ans;
    }

}isap;
int N,M;

struct point {
    int x,y;
}p[maxn];
int main()
{
    int T;
    scanf ("%d",&T);
    while (T--){
        scanf ("%d%d",&N,&M);
        isap.init();
        for (int i=1;i<=N;i++){
            scanf ("%d%d",&p[i].x,&p[i].y);
        }
        int u,v,c;
        for (int i=1;i<=M;i++){
            scanf ("%d%d%d",&u,&v,&c);
            isap.addedge(u,v,c);
            isap.addedge(v,u,c);
        }
        int st=1,ed=1;
        for (int i=2;i<=N;i++){
            if (p[st].x>p[i].x)st=i;
            if (p[ed].x<p[i].x)ed=i;
        }
        int ans=isap.sap(st,ed,N);
        printf ("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值