HDU 3061 Battle (minimum cutting power closure) Classic

Source: Internet
Author: User

BattleTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 983 Accepted Submission (s): 477


Problem description Because of small white classmate recent martial arts very hard, soon be promoted to the commander of the day of the army. On his first day in office, he faced an extremely difficult battle:
According to the Scout return, a total of n in front of the city, taking into account the reasons for the topography, finally got a conclusion: Some cities must be captured before the conquest of other cities.
In fact, it is possible to think of a map as a topological map, and the conquest of a certain fortress means that all its precursor nodes must be captured first.
The small white also did a survey, got the conquest of each town will be the strength of his forces to produce how much consumption (of course, may also be increased, because each conquest of a town, it can reorganize the army, expand the strength, the days of the army is very large, if not consider the proceeds, they can take all the cities).
Now please help little white commander to make a battle plan, choose which cities to attack, so that the grooming after the Battle of the most powerful.

Input first enters an n for n cities (1<= n <= 500)
Then enter a m, which represents the number of topological relationships between the castle and the castle.
Then enter n numbers representing the battle consumption from 1 to n numbered cities (negative numbers represent the amount of days that will be consumed by the military, and a positive number indicates that the day's army can gain the corresponding fighting proceeds)
The last M line is 2 digits A, B, representing the number of the corresponding fortress.
It means that after capturing B, a will be captured.

Output the maximum number of battle gains the day's military can gain
Sample Input
5 5 8-8-10 12-10 1 2 2 5 1 4 3 4 4 5

Sample Output
2

Source2009 multi-university Training Contest 16-host by NIT problem Solving: In a graph, that is, there is revenue and cost, then you can use the most powerful closure, the model: the point of revenue and the source is connected, edge right for the profit size, The points spent are connected to the sinks, the edge weights are spent (if negative numbers become positive), and the Benquan of the edges of the other originals are INF. After ans = total gain-minimum cut capacity.
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std   ; #define Captype intconst int maxn = 100010;    Total number of points const int MAXM = 400010;    Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap,flow;}  Edg[maxm];int Eid,head[maxn];int GAP[MAXN];  The number of points for each distance (or can be considered a height) int DIS[MAXN];  The shortest distance from each point to the end Enode int CUR[MAXN];    Cur[u] indicates that from the U point can flow through the cur[u] number side int pre[maxn];void init () {eid=0; memset (head,-1,sizeof (Head));}    There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) without a forward edge {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0;    head[u]=eid++; Edg[eid].to=u;    EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0; head[v]=eid++;}    Captype maxflow_sap (int snode,int enode, int n) {//n is the total number of points including the source and sink points, this must be noted memset (Gap,0,sizeof (GAP));    memset (dis,0,sizeof (dis));    memcpy (cur,head,sizeof (head));    Pre[snode] =-1;    Gap[0]=n;  Captype ans=0;    Max Stream int U=snode; while (Dis[snoDe]<n) {//Judging from the snode point there is no flow to the next adjacent point if (u==enode) {//Find a way to increase the flow captype min=inf;            int inser;                for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to])//To find the maximum amount of traffic Min if (min>edg[i].cap-edg[i].flow) {                Min=edg[i].cap-edg[i].flow;            Inser=i;                } for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=min;  Edg[i^1].flow-=min;            Flow of the side that can be recycled} ans+=min;            u=edg[inser^1].to;        Continue  } bool flag = FALSE;        It is possible to determine whether an int v can flow toward the neighboring point from the U point;            for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to;                if (edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of the adjacent flow point +1 intmind= N;            for (int i=head[u]; i!=-1; i=edg[i].next) if (edg[i].cap-edg[i].flow>0 && mind>dis[edg[i].to]) {            MIND=DIS[EDG[I].TO];        Cur[u]=i;        } gap[dis[u]]--;  if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find        Can flow to the point, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++;  if (U!=snode) u=edg[pre[u]^1].to; Return an Edge} return ans;    int main () {int n,m,u,v,c;        while (scanf ("%d%d", &n,&m) >0) {init ();        int vs=0,vt=n+1, ans=0;            for (int i=1; i<=n; i++) {scanf ("%d", &c);            if (c>=0) ans+=c,addedg (VS, I, c);        else ADDEDG (I, VT,-C);            } while (m--) {scanf ("%d%d", &u,&v);        ADDEDG (U, V, INF);    } ANS-=MAXFLOW_SAP (VS, VT, vt+1);    printf ("%d\n", ans); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3061 Battle (minimum cutting power closure) Classic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.