HDU 3061 Battle

Battle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 628 Accepted Submission(s): 304


Problem Description
由於小白同學近期習武十分刻苦,很快被晉升爲天策軍的統帥。而他上任的第一天,就面對了一場極其困難的戰鬥:
據偵查兵回報,前方共有N座城池,考慮到地勢原因,最終得到一個結論:攻佔某些城池之前必須攻佔另外一些城池。
事實上,可以把地圖看做是一張拓撲圖,而攻佔某個城池,就意味着必須先攻佔它的所有前驅結點。
小白還做了一份調查,得到了攻佔每個城池會對他的兵力產生多少消耗(當然也可能會得到增長,因爲每攻佔一個城池,便可以整頓軍隊,擴充兵力,天策軍的兵力十分龐大,如果不考慮收益,他們可以攻取所有的城池)。
現在請你幫小白統帥做一份戰鬥計劃,挑選攻打哪些城市,使得天策軍在戰鬥過後軍容最爲壯大。

Input
首先輸入一個N 代表有N個城池(1<= n <= 500)
接着輸入一個M,代表城池和城池之間的拓撲關係數。
接着輸入N個數字 代表從1 到 N 編號城池的戰鬥消耗(負數代表將要消耗天策軍兵力,正數表示天策軍可以獲得相應的戰鬥收益)
最後M行 每行2個數字 a,b,代表相應城池的編號。
表示攻佔b之後纔可以攻佔a;

Output
天策軍最大能獲得多少戰鬥收益

Sample Input
5 5 8 -8 -10 12 -10 1 2 2 5 1 4 3 4 4 5

Sample Output
2

Source

Recommend
lcy

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>

#define INT_INF 0x3fffffff
#define LL_INF 0x3fffffffffffffff
#define EPS 1e-12
#define MOD 1000000007
#define PI 3.141592653579798
#define N 600
#define E 600000

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;

struct Edge
{
    int en,cap,flow,next;
} edge[E];
int head[N] , tot , now[N];
int source,sink,tot_num;
int pre[N] , dis[N] , gap[N];

void add_edge(int st,int en,int cap)
{
    edge[tot].en=en;
    edge[tot].cap=cap;
    edge[tot].flow=0;
    edge[tot].next=head[st];
    head[st]=tot++;

    edge[tot].en=st;
    edge[tot].cap=0;
    edge[tot].flow=0;
    edge[tot].next=head[en];
    head[en]=tot++;
}

void augment(int flow)
{
    for(int i=source;i!=sink;i=edge[now[i]].en)
    {
        edge[now[i]].flow+=flow;
        edge[now[i]^1].flow-=flow;
    }
}

int sap()
{
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(int i=0;i<=tot_num;i++)
        now[i]=head[i];
    gap[0]=tot_num;
    int point=source,flow=0,min_flow=INT_INF;
    while(dis[source]<tot_num)
    {
        bool fg=false;
        for(int i=now[point];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow>0 && dis[point]==dis[edge[i].en]+1)
            {
                min_flow=min(min_flow,edge[i].cap-edge[i].flow);
                now[point]=i;
                pre[edge[i].en]=point;
                point=edge[i].en;
                if(point==sink)
                {
                    flow+=min_flow;
                    augment(min_flow);
                    point=source;
                    min_flow=INT_INF;
                }
                fg=true;
                break;
            }
        if(fg) continue;
        if(--gap[dis[point]]==0) break;
        int Min=tot_num;
        for(int i=head[point];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow>0 && Min>dis[edge[i].en])
            {
                Min=dis[edge[i].en];
                now[point]=i;
            }
        gap[dis[point]=Min+1]++;
        if(point!=source) point=pre[point];
    }
    return flow;
}

int build(int n,int m)
{
    int sum=0;
    memset(head,-1,sizeof(head));
    tot=0;
    source=0; sink=n+1; tot_num=n+2;
    for(int i=1,a;i<=n;i++)
    {
        scanf("%d",&a);
        if(a>0)
        {
            add_edge(source,i,a);
            sum+=a;
        }
        else add_edge(i,sink,-a);
    }
    for(int i=1,a,b;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        add_edge(a,b,INT_INF);
    }
    return sum;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int sum=build(n,m);
        int ans=sap();
        printf("%d\n",sum-ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章