hdu4309 Seikimatsu Occult Tonneru

wa了一上午,午睡的時候突然想到數組大小,改大一倍竟然就過了,看來以後在內存允許的情況下,開數組還是別卡數據範圍比較好。。。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=222;
const int maxe=3333;
const int inf=0x3fffffff;
struct edge
{
    int to,ec,next,rev;
}ee[maxe],oee[maxe];
int e[maxn],ecnt;
int bcnt,b[maxn],ct[maxn];
int n,m;
int pep[maxn];
bool hash[maxe];
void addedge(int u,int v,int c)
{
    oee[ecnt].to=v;oee[ecnt].ec=c;oee[ecnt].next=e[u];oee[ecnt].rev=ecnt+1;e[u]=ecnt++;
    oee[ecnt].to=u;oee[ecnt].ec=0;oee[ecnt].next=e[v];oee[ecnt].rev=ecnt-1;e[v]=ecnt++;
}
int d[maxn],gap[maxn],st,ed;
int sap(int u,int flow)
{
    if(u==ed)
        return flow;
    int i,v,c,res=flow,t;
    for(i=e[u];i!=-1;i=ee[i].next)
    {
        v=ee[i].to;c=ee[i].ec;
        if(c>0&&d[u]==d[v]+1)
        {
            t=sap(v,min(res,c));
            ee[i].ec-=t;ee[ee[i].rev].ec+=t;
            if(!(res-=t))
                return flow;
        }
    }
    if(!(--gap[d[u]]))
        d[st]=n;
    ++gap[++d[u]];
    return flow-res;
}
int maxflow()
{
    st=0;ed=n-1;
    int ret=0;
    memset(d,0,sizeof(d));
    memset(gap,0,sizeof(gap));
    for(gap[0]=n;d[st]<n;)
        ret+=sap(st,inf);
    return ret;
}
int mf,mc;
void solve(int now)
{
    int i,j,cost=0,flow;
    if(now==bcnt)
    {
//        cout<<"*******now=="<<now<<endl;
        memcpy(ee,oee,sizeof(oee));
        for(i=0;i<bcnt;++i)
        {
            if(hash[i])
            {
                cost+=ct[i];
                ee[b[i]].ec=inf;
            }
        }
        for(i=0;i<ecnt;i+=2)
        {
            ee[i].ec+=ee[i^1].ec;
            ee[i^1].ec=0;
        }
        flow=maxflow();
        if(flow>mf)
        {
            mf=flow;mc=cost;
            return ;
        }
        if(flow==mf&&cost<mc)
        {
            mc=cost;
            return ;
        }
        return ;
    }
    hash[now]=true;
    solve(now+1);
    hash[now]=false;
    solve(now+1);
}
int main()
{
    int i,j,u,v,c,t,w;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(e,-1,sizeof(e));ecnt=0;
        bcnt=0;
        for(i=1;i<=n;++i)
        {
            scanf("%d",&pep[i]);
            addedge(0,i,pep[i]);
        }
        for(i=1;i<=m;++i)
        {
            scanf("%d%d%d%d",&u,&v,&w,&t);
            if(t<0)
            {
                addedge(u,v,inf);
                addedge(u,n+1,w);
            }
            else if(t==0)
            {
                addedge(u,v,inf);
            }
            else
            {
                b[bcnt]=ecnt;ct[bcnt++]=w;
                addedge(u,v,1);
            }
        }
        n+=2;
        memset(hash,false,sizeof(hash));
        mf=0;mc=0;
        solve(0);
        if(mf==0)
            printf("Poor Heaven Empire\n");
        else
            printf("%d %d\n",mf,mc);
    }
    return 0;
}


 

發佈了88 篇原創文章 · 獲贊 2 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章