Battle (hdu 3061 maximum weight closed graph template problem)

Source: Internet
Author: User

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


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
Recommendlcy | We have carefully selected several similar problems for you:3046 1565 1569 1733 1853

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 300005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO  N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;struct edge{int u,v,cap,next;} Edge[mAxn];int head[maxn],cur[maxn],level[maxn];int n,m,num;void init () {num=0; MEM (head,-1);}    void Addedge (int u,int v,int W) {edge[num].u=u; edge[num].v=v; edge[num].cap=w; edge[num].next=head[u]; head[u]=num++; Edge[num].u=v; Edge[num].v=u; Edge[num].cap=0; EDGE[NUM].NEXT=HEAD[V]; head[v]=num++;}    BOOL BFs (int s,int t) {mem (level,-1);    queue<int>q;    level[s]=0;    Q.push (s); while (! Q.empty ()) {int U=q.front ();        Q.pop ();            for (int i=head[u];i+1;i=edge[i].next) {int v=edge[i].v;                if (level[v]==-1&&edge[i].cap>0) {level[v]=level[u]+1;            Q.push (v); }}} return level[t]!=-1;}    int dfs (int u,int t,int f) {if (u==t) return F;        for (int &i=cur[u];i+1;i=edge[i].next) {int v=edge[i].v;            if (edge[i].cap>0&&level[v]==level[u]+1) {int D=dfs (v,t,min (F,edge[i].cap));    if (d>0) {            Edge[i].cap-=d;                Edge[i^1].cap+=d;            return D; }}} return 0;}    int dinic (int s,int t,int nodenum) {int flow=0,f;        while (BFS (s,t)) {for (int i=0;i<=nodenum;i++) cur[i]=head[i];    while ((F=dfs (S,t,inf)) >0) flow+=f; } return flow;}    int main () {//Freopen ("C:/users/asus1/desktop/in.txt", "R", stdin);    int i,j,x,u,v;        while (~SCANF ("%d%d", &n,&m)) {init ();        int all=0;            for (i=1;i<=n;i++) {scanf ("%d", &x);            if (x>0) Addedge (0,i,x), all+=x;        else Addedge (i,n+1,-x);            } for (i=1;i<=m;i++) {scanf ("%d%d", &u,&v);        Addedge (U,v,inf);    } printf ("%d\n", All-dinic (0,n+1,n+2)); } return 0;}


Battle (hdu 3061 maximum weight closed graph template problem)

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.