Poj 2125 (least point weighted overwrite)

Source: Internet
Author: User
# Include <stdio. h> # Include < String . H> # Include <Iostream> Using   Namespace  STD;  # Define N 250 # Define M 20100 # Define INF 0x3fffffff Struct  Node {  Int   From , To, next, W;} edge [m];  Int  N, m;  Int  CNT, pre [N];  Int  S1, NN, T;  Int  LV [N], gap [N];  Int  Kk;  Int  Mark [N];  Int Save [ 110 ] [ 2 ];  Void Add_edge ( Int U, Int V, Int  W) {edge [CNT].  From = U; edge [CNT]. = V; edge [CNT]. W = W; edge [CNT]. Next = Pre [u]; Pre [u] = CNT ++ ;}  Int Sdfs ( Int S, Int  W ){  If (S = T) Return  W;  Int F = 0  ;  Int Mi = Nn;  For ( Int P = pre [s]; P! =- 1 ; P = Edge [p]. Next ){  Int V = Edge [p].;  If (Edge [p]. W! = 0  ){  If (LV [v] = lv [s]- 1  ){  Int TMP = sdfs (v, min (w- F, edge [p]. W); edge [p]. W -= TMP; edge [p ^ 1 ]. W + = TMP; f + =TMP;  If (F = w | LV [s] = nn) Return  F ;}  If (LV [v] <mi) MI = LV [v] ;}}  If (F = 0  ) {Gap [LV [s] -- ;  If (Gap [LV [s] = 0  ) {LV [s] =Nn;  Return   0  ;} LV [s] = MI + 1  ; Gap [LV [s] ++ ;}  Return  F ;}  Int  SAP () {NN = T + 1  ;  Int Sum = 0 ; Memset (LV,  0 , Sizeof  (LV); memset (gap,  0 , Sizeof  (GAP); Gap [  0 ] = Nn;  While (LV [S1] < Nn) {sum + = Sdfs (S1, INF );}  Return  SUM ;}  Void DFS ( Int  S) {mark [s] = 1  ;  For ( Int P = pre [s]; P! =- 1 ; P = Edge [p]. Next ){  Int V = Edge [p].;  If (MARK [v] = 0 & Edge [p]. W! = 0  ){ If (V> N) kk ++ ;  Else Kk -- ; DFS (v );}}}  Int  Main () {CNT = 0  ; Memset (PRE, - 1 , Sizeof  (Pre); scanf (  "  % D  " , & N ,&M); S1 = 0 ; T = 2 * N + 1  ;  For ( Int I = 1 ; I <= N; I ++ ){  Int  TMP; scanf (  "  % D  " ,& TMP); add_edge (n + I, T, TMP); add_edge (t, n + I, 0  );}  For ( Int I = 1 ; I <= N; I ++ ){  Int  TMP; scanf (  "  % D  " ,& TMP); add_edge (S1, I, TMP); add_edge (I, S1,  0 );}  For ( Int I = 0 ; I <m; I ++ ){  Int  X, Y; scanf (  "  % D  " , & X ,& Y); add_edge (x, n + Y, INF); add_edge (n + Y, X, 0  );} Kk =N; printf (  "  % D \ n  "  , SAP (); memset (mark,  0 , Sizeof  (Mark); DFS (S1); printf (  "  % D \ n  "  , KK );  For ( Int I = 1 ; I <= N; I ++){  If (MARK [I] = 0 ) Printf ( "  % D-\ n  "  , I );  If (MARK [I + N] = 1 ) Printf ( "  % D + \ n  "  , I );}  Return   0  ;} 

 

After thinking about N for a long time, I put this question in the binary chart. I thought of using network streams...

Knowing the use of network streams is the basic minimal cut model.

And then stuck in a place, how to output the smallest cutover set. Best of mind

You only need to use DFS once. However, this DFS method seems to be applicable only to such a graph (Basic minimal cut model ).

 

Destroying the Graph
Time limit: 2000 ms   Memory limit: 65536 K
Total submissions: 6031   Accepted: 1911   Special Judge

Description

Alice and Bob play the following game. first, Alice draws some directed graph with n vertices and M arcs. after that Bob tries to destroy it. in a move he may take any vertex of the graph and remove either all arcs incoming Into This vertex, or all arcs outgoing from this vertex.   Alice assigns two costs to each vertex: WI +   And Wi-. If Bob removes all arcs incoming into the I-th vertex he pays wi +   Dollars to Alice, and if he removes outgoing arcs he pays wi-   Dollars.  Find out what minimal sum Bob needs to remove all arcs from the graph.

Input

Input File describes the graph Alice has drawn. the first line of the input file contains N and M (1 <= n <= 100, 1 <= m <= 5000 ). the second line contains N integer numbers specifying wi +. the third line defines wi-   In a similar way. All costs are positive and do not exceed 106   . Each of the following M lines contains two integers describing the corresponding arc of the graph. graph may contain loops and parallel arcs.

Output

On the first line of the output file print W --- the minimal sum Bob must have to remove all arcs from the graph. on the second line print K --- the number of moves Bob needs to do it. after that print K lines that describe Bob's moves. each line must first contain the number of the vertex and then '+' or '-'character, separated by one space. character '+ 'means that Bob removes all arcs incoming into the specified vertex and'-'that Bob removes all arcs outgoing from the specified vertex.

Sample Input

 
3 61 2 34 2 11 21 13 21 23 12 3

Sample output

 
531 + 2-2 +

Source

Northeastern Europe 2003, northern subregion

 

 

 

 

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.