hdu4309枚举+最大流

来源:互联网 发布:金星跟老公 知乎 编辑:程序博客网 时间:2024/06/06 09:35

http://acm.hdu.edu.cn/showproblem.php?pid=4309

看了看别人的报告~~感觉二进制枚举很巧妙,学习了,。。

#include <cstring>#include <cstdio>#include <queue>#define MAXN 2000#define MAXM 20000#define inf 0x3f3f3f3fusing namespace std;struct node{    int u,v,f;};node e[MAXM*3];int first[MAXN],next[MAXM*3];int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];int cc;inline void add_edge(int u,int v,int f){    e[cc].u=u;    e[cc].v=v;    e[cc].f=f;    next[cc]=first[u];    first[u]=cc;    cc++;    e[cc].u=v;    e[cc].v=u;    e[cc].f=0;    next[cc]=first[v];    first[v]=cc;    cc++;}int ISAP(int s,int t,int n){    int cur_flow,flow_ans=0,u,tmp,neck,i,v;    memset(d,0,sizeof(d));    memset(gap,0,sizeof(gap));    memset(pre,-1,sizeof(pre));    for(i=0;i<=n;i++)        curedge[i]=first[i];    gap[0]=n+1;    u=s;    while(d[s]<=n)    {        if(u==t)        {            cur_flow=inf;            for(i=s;i!=t;i=e[curedge[i]].v)            {                if(cur_flow>e[curedge[i]].f)                {                    neck=i;                    cur_flow=e[curedge[i]].f;                }            }            for(i=s;i!=t;i=e[curedge[i]].v)            {                tmp=curedge[i];                e[tmp].f-=cur_flow;                e[tmp^1].f+=cur_flow;            }            flow_ans+=cur_flow;            u=neck;        }        for(i=curedge[u];i!=-1;i=next[i])        {            v=e[i].v;            if(e[i].f&&d[u]==d[v]+1)                break;        }        if(i!=-1)        {            curedge[u]=i;            pre[v]=u;            u=v;        }        else        {            if(0==--gap[d[u]])                break;            curedge[u]=first[u];            for(tmp=n+5,i=first[u];i!=-1;i=next[i])                if(e[i].f)                    tmp=min(tmp,d[e[i].v]);            d[u]=tmp+1;            ++gap[d[u]];            if(u!=s)                u=pre[u];        }    }    return flow_ans;}int city[105];struct node1{    int u,v,w,p;};node1 e1[10005];int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(first,-1,sizeof(first));        memset(next,-1,sizeof(next));        cc=0;        int i;        int cnt=0;        for(i=1;i<=n;i++)            scanf("%d",&city[i]);        int flag=1;        for(i=0;i<m;i++)        {            scanf("%d%d%d%d",&e1[i].u,&e1[i].v,&e1[i].w,&e1[i].p);            if(e1[i].p>0)                cnt++;            if(e1[i].p<0&&e1[i].w!=0)                flag=0;        }        if(flag)        {            printf("Poor Heaven Empire\n");            continue;        }        int s=0,t=n+1;        int k,mm=(1<<cnt);        int maxnum=0,mincost=0;        for(k=0;k<mm;k++)        {            int i,j=0;            memset(first,-1,sizeof(first));            memset(next,-1,sizeof(next));            cc=0;            int cost=0;            for(i=1;i<=n;i++)                add_edge(s,i,city[i]);            for(i=0;i<m;i++)            {                if(e1[i].p<0)                {                    add_edge(e1[i].u,e1[i].v,inf);                    add_edge(e1[i].u,t,e1[i].w);                }                else if(e1[i].p==0)                    add_edge(e1[i].u,e1[i].v,inf);                else                {                    if(k&(1<<j))                    {                        add_edge(e1[i].u,e1[i].v,inf);                        cost+=e1[i].w;                    }                    else                        add_edge(e1[i].u,e1[i].v,1);                    j++;                }            }            int ans=ISAP(s,t,t);            if(ans>maxnum||(ans==maxnum&&cost<mincost))            {                maxnum=ans;                mincost=cost;            }        }        if(maxnum==-inf)            printf("Poor Heaven Empire\n");        else            printf("%d %d\n",maxnum,mincost);    }    return 0;}


原创粉丝点击