Power Network(POJ 1459)

# Power Network(POJ 1459)
题意:
有N个发电站,NC个用户,NP个中转站,M条传输线,求最大供电量。
思路:
裸的最大流问题;
唯一有搞一点的输入格式,从网上学了一手,过了。
开始Dinic的板子超时了,换SAP就过了,emmmmm。

AC code:

#include
#include
#include
#include
#include
using namespace std;

template<typename T>
T read(T &x){
    x = 0;
    char ch = getchar();
    for(;ch<'0'||ch>'9';ch=getchar());
    for(;ch>='0'&&ch<='9';ch=getchar()) x = x*10+(ch-'0');
    if(ch==10||ch==13) return 0;
    return 1;
}

const int INF = 0x3f3f3f3f;
const int MAXN = 150;
const int MAXM = 30000;
#define captype int
struct SAP_MaxFlow{
    struct Edge{
        int from,to,next;
        captype cap;
    }edges[MAXM];
    int tot,head[MAXN];
    int gap[MAXN];
    int dis[MAXN];
    int cur[MAXN];
    int pre[MAXN];

    void init(){
        tot=0;
        memset(head,-1,sizeof(head));
    }
    void AddEdge(int u,int v,captype c,captype rc=0){
        edges[tot] = (Edge){u,v,head[u],c};  head[u]=tot++;
        edges[tot] = (Edge){v,u,head[v],rc}; head[v]=tot++;
    }
    captype maxFlow_sap(int sNode,int eNode, int n){//n是包括源点和汇点的总点个数,这个一定要注意
        memset(gap,0,sizeof(gap));
        memset(dis,0,sizeof(dis));
        memcpy(cur,head,sizeof(head));
        pre[sNode] = -1;
        gap[0]=n;
        captype ans=0;
        int u=sNode;
        while(dis[sNode]<n){
            if(u==eNode){
                captype Min=INF;
                int inser;
                for(int i=pre[u]; i!=-1; i=pre[edges[i^1].to])
                if(Min>edges[i].cap){
                    Min=edges[i].cap;
                    inser=i;
                }
                for(int i=pre[u]; i!=-1; i=pre[edges[i^1].to]){
                    edges[i].cap-=Min;
                    edges[i^1].cap+=Min;
                }
                ans+=Min;
                u=edges[inser^1].to;
                continue;
            }
            bool flag = false;
            int v;
            for(int i=cur[u]; i!=-1; i=edges[i].next){
                v=edges[i].to;
                if(edges[i].cap>0 && dis[u]==dis[v]+1){
                    flag=true;
                    cur[u]=pre[v]=i;
                    break;
                }
            }
            if(flag){
                u=v;
                continue;
            }
            int Mind= n;
            for(int i=head[u]; i!=-1; i=edges[i].next)
            if(edges[i].cap>0 && Mind>dis[edges[i].to]){
                Mind=dis[edges[i].to];
                cur[u]=i;
            }
            gap[dis[u]]--;
            if(gap[dis[u]]==0) return ans;
            dis[u]=Mind+1;
            gap[dis[u]]++;
            if(u!=sNode) u=edges[pre[u]^1].to;  //退一条边
        }
        return ans;
    }
}F;

/*
struct Dinic{
    struct Edge{
        int to,next;
        captype cap;
    }edge[MAXM];
    int tot;
    int head[MAXN],dis[MAXN];
    void init(){
        tot = 0;
        memset(head,-1,sizeof(head));
    }
    void addedge(int u,int v,captype cap){
        edge[tot]=(Edge){v,head[u],cap},head[u]=tot++;
        edge[tot]=(Edge){u,head[v],0},head[v]=tot++;
    }
    bool bfs(int s,int t){
        memset(dis,-1,sizeof(dis));
        dis[s] = 0;
        std::queueq;
        q.push(s);
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(int i=head[u];~i;i=edge[i].next){
                int v = edge[i].to;
                if(dis[v]==-1&&edge[i].cap>0){
                    dis[v] = dis[u]+1;
                    q.push(v);
                }
            }
        }
        return dis[t]!=-1;
    }
    captype dfs(int u,int t,captype f){
        if(u==t) return f;
        for(int i=head[u];~i;i=edge[i].next){
            int v = edge[i].to;
            if(dis[v]==dis[u]+1&&edge[i].cap>0){
                int d = dfs(v,t,std::min(edge[i].cap,f));
                if(d>0){
                    edge[i].cap -= d;
                    edge[i^1].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    captype max_flow(int s,int t){
        captype flow = 0,f;
        while(1){
            if(!bfs(s,t)) return flow;
            while((f=dfs(s,t,INF))>0){
                flow += f;
            }
        }
        return flow;
    }
}F;*/

int main(){
    int n,m,np,nc,u,v,z;
    while(~scanf("%d%d%d%d",&n,&np,&nc,&m)){
        F.init();
        int s = 0, t = n + 1;
        for(int i = 0; i < m; ++i){
            while(getchar() != '(') ;
            scanf("%d,%d)%d",&u,&v,&z);
            ++u,++v;
            //F.addedge(u,v,z);
            F.AddEdge(u,v,z);
        }
        for(int i = 0; i < np; ++i){
            while(getchar() != '(') ;
            scanf("%d)%d",&u,&z);
            ++u;
            //F.addedge(s,u,z);
            F.AddEdge(s,u,z);
        }
        for(int i = 0; i < nc; ++i){
            while(getchar() != '(') ;
            scanf("%d)%d",&u,&z);
            ++u;
            //F.addedge(u,t,z);
            F.AddEdge(u,t,z);
        }
        //printf("%d\n",F.max_flow(s,t));
        printf("%d\n",F.maxFlow_sap(s,t,n + 2));
    }
    return 0;
}

你可能感兴趣的:(网络流,最大流)