Linear Programming and 9-square fetch of Network Flow

Source: Internet
Author: User

Algorithm Implementation Question 8-9 getting the number of squares (Exercise 8-20)
? Problem description:
In a checker with M * n squares, each square has a positive integer. We need to take the number from the square
The square where the number 2 is located does not have a public edge, and the sum of the retrieved numbers is the largest. Design a number fetch algorithm that meets the requirements.
? Programming task:
For a given checker, program the number to find the maximum sum.
? Data input:
Input data is provided by the file input.txt. There are two positive integers m and n in the row 1st of the file, indicating the number of rows on the board respectively.
And the number of columns. In the next m row, each row has n positive integers, indicating the number in the checker square.
? Result output:
When the program ends, the maximum sum of the number is output to the output.txt file.
Input File example
Output file example
Input.txt
Output.txt
3 3
1 2 3
3 2 3
2 3 1
11

Question:

Maximum vertex weight independent set = total vertex weight-minimum vertex weight overwrite set = total vertex weight-minimum cut over ......

Code:

  1 const inf=maxlongint;  2 type node=record  3      from,go,next,v:longint;  4      end;  5 var  tot,i,j,n,m,maxflow,l,r,s,t,x,y,sum:longint;  6      h,head,q,cur:array[0..1000] of longint;  7      e:array[0..50000] of node;  8      num:array[0..50,0..50] of longint;  9      function min(x,y:longint):longint; 10       begin 11       if x<y then exit(x) else exit(y); 12       end; 13 procedure ins(x,y,z:longint); 14  begin 15  inc(tot); 16  e[tot].from:=x;e[tot].go:=y;e[tot].v:=z;e[tot].next:=head[x];head[x]:=tot; 17  end; 18 procedure insert(x,y,z:longint); 19  begin 20  if y>100000 then exit; 21  ins(x,y,z);ins(y,x,0); 22  end; 23 function bfs:boolean; 24  var i,x,y:longint; 25  begin 26  fillchar(h,sizeof(h),0); 27  l:=0;r:=1;q[1]:=s;h[s]:=1; 28  while l<r do 29   begin 30   inc(l); 31   x:=q[l]; 32   i:=head[x]; 33   while i<>0 do 34    begin 35    y:=e[i].go; 36    if (e[i].v<>0) and (h[y]=0) then 37     begin 38      h[y]:=h[x]+1; 39      inc(r);q[r]:=y; 40     end; 41    i:=e[i].next; 42    end; 43   end; 44  exit (h[t]<>0); 45  end; 46 function dfs(x,f:longint):longint; 47  var i,y,used,tmp:longint; 48  begin 49  if x=t then exit(f); 50  used:=0; 51  i:=cur[x]; 52  while i<>0 do 53   begin 54   y:=e[i].go; 55   if (h[y]=h[x]+1) and (e[i].v<>0) then 56    begin 57    tmp:=dfs(y,min(e[i].v,f-used)); 58    dec(e[i].v,tmp);if e[i].v<>0 then cur[x]:=i; 59    inc(e[i xor 1].v,tmp); 60    inc(used,tmp); 61    if used=f then exit(f); 62    end; 63   i:=e[i].next; 64   end; 65  if used=0 then h[x]:=-1; 66  exit(used); 67  end; 68 procedure dinic; 69  begin 70  while bfs do 71   begin 72   for i:=s to t do cur[i]:=head[i]; 73   inc(maxflow,dfs(s,inf)); 74   end; 75  end; 76 procedure init; 77  begin 78  tot:=1; 79  readln(n,m);sum:=0;s:=0;t:=n*m+1; 80  fillchar(num,sizeof(num),60); 81  for i:=1 to n do for j:=1 to m do num[i,j]:=(i-1)*m+j; 82  for i:=1 to n do 83   begin 84    for j:=1 to m do 85     begin 86      read(x);inc(sum,x); 87      if odd(i+j) then insert(s,num[i,j],x) else insert(num[i,j],t,x); 88     end; 89   readln; 90   end; 91  end; 92 procedure main; 93  begin 94  for i:=1 to n do 95   for j:=1 to m do 96    if odd(i+j) then 97     begin 98      insert(num[i,j],num[i,j+1],inf); 99      insert(num[i,j],num[i,j-1],inf);100      insert(num[i,j],num[i-1,j],inf);101      insert(num[i,j],num[i+1,j],inf);102     end;103  maxflow:=0;104  dinic;105  writeln(sum-maxflow);106  end;107 begin108  assign(input,‘grid.in‘);assign(output,‘grid.out‘);109  reset(input);rewrite(output);110  init;111  main;112  close(input);close(output);113 end.  
View code

 

 

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.