zoukankan      html  css  js  c++  java
  • 1000: A+B Problem(NetWork Flow)

    1000: A+B Problem

    Time Limit: 1 Sec  Memory Limit: 5 MB
    Submit: 11814  Solved: 7318
    [Submit][Status][Discuss]

    Description

    Calculate a+b

    Input

    Two integer a,b (0<=a,b<=10)

    Output

    Output a+b

    Sample Input

    1 2

    Sample Output

    3

    HINT

    Q: Where are the input and the output? A: Your program shall always read input from stdin (Standard Input) and write output to stdout (Standard Output). For example, you can use 'scanf' in C or 'cin' in C++ to read from stdin, and use 'printf' in C or 'cout' in C++ to write to stdout. You shall not output any extra data to standard output other than that required by the problem, otherwise you will get a "Wrong Answer". User programs are not allowed to open and read from/write to files. You will get a "Runtime Error" or a "Wrong Answer" if you try to do so. Here is a sample solution for problem 1000 using C++/G++:



    #include
    using namespace std;
    int main()
    {
    int a,b;
    cin >> a >> b;
    cout << a+b << endl;
    return 0;
    }

    It's important that the return type of main() must be int when you use G++/GCC,or you may get compile error. Here is a sample solution for problem 1000 using C/GCC:



    #include

    int main()
    {
    int a,b;
    scanf("%d %d",&a, &b);
    printf("%d ",a+b);
    return 0;
    }

    Here is a sample solution for problem 1000 using PASCAL:



    program p1000(Input,Output);
    var
    a,b:Integer;
    begin
    Readln(a,b);
    Writeln(a+b);
    end.

    Here is a sample solution for problem 1000 using JAVA: Now java compiler is jdk 1.5, next is program for 1000



    import java.io.*;
    import java.util.*;
    public class Main
    {
    public static void main(String args[]) throws Exception
    {
    Scanner cin=new Scanner(System.in);
    int a=cin.nextInt(),b=cin.nextInt();
    System.out.println(a+b);
    }
    }

    Old program for jdk 1.4



    import java.io.*;
    import java.util.*;

    public class Main
    {
    public static void main (String args[]) throws Exception
    {
    BufferedReader stdin =
    new BufferedReader(
    new InputStreamReader(System.in));

    String line = stdin.readLine();
    StringTokenizer st = new StringTokenizer(line);
    int a = Integer.parseInt(st.nextToken());
    int b = Integer.parseInt(st.nextToken());
    System.out.println(a+b);
    }
    }

    Source

    题解:不用说,很多网站上都有的经典题(HansBug:呵呵呵呵呵~~~),直到今天我才第一次用网络流来做它= =
    还是没啥好说的,直接源点与汇点连两条边权分别为A和B的有向边,然后跑sap完事,具体看代码
     1 /**************************************************************
     2     Problem: 1000
     3     User: HansBug
     4     Language: Pascal
     5     Result: Accepted
     6     Time:0 ms
     7     Memory:268 kb
     8 ****************************************************************/
     9  
    10 type
    11     point=^node;
    12     node=record
    13                g,w:longint;
    14                next,anti:point;
    15     end;
    16 var
    17    i,j,k,l,m,n,ans,s,t,x,y:longint;
    18    a:array[0..1000] of point;
    19    d,dv:array[0..1000] of longint;
    20 function min(x,y:longint):longint;
    21          begin
    22               if x<y then min:=x else min:=y;
    23          end;
    24 procedure add(x,y,z:longint);
    25           var p:point;
    26           begin
    27                new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
    28                new(p);p^.g:=x;p^.w:=0;p^.next:=a[y];a[y]:=p;
    29                a[x]^.anti:=a[y];a[y]^.anti:=a[x];
    30           end;
    31 function dfs(x,flow:longint):longint;
    32          var p:point;k:longint;
    33          begin
    34               if x=t then exit(flow);
    35               dfs:=0;p:=a[x];
    36               while p<>nil do
    37                     begin
    38                          if (p^.w<>0) and (d[x]=(d[p^.g]+1)) then
    39                             begin
    40                                  k:=dfs(p^.g,min(flow-dfs,p^.w));
    41                                  if p^.w<>maxlongint then dec(p^.w,k);
    42                                  if p^.anti^.w<>maxlongint then inc(p^.anti^.w,k);
    43                                  inc(dfs,k);if dfs=flow then exit;
    44                             end;
    45                          p:=p^.next;
    46                     end;
    47               if d[s]=n then exit;
    48               dec(dv[d[x]]);
    49               if dv[d[x]]=0 then d[s]:=n;
    50               inc(d[x]);inc(dv[d[x]]);
    51          end;
    52 begin
    53      readln(x,y);n:=2;s:=1;t:=2;
    54      for i:=1 to n do a[i]:=nil;
    55      add(s,t,x);add(s,t,y);
    56      fillchar(d,sizeof(d),0);
    57      fillchar(dv,sizeof(dv),0);
    58      dv[0]:=n;ans:=0;
    59      while d[s]<n do inc(ans,dfs(s,maxlongint));
    60      writeln(ans);
    61      readln;
    62 end.       
  • 相关阅读:
    压测 正则 性能分析
    时间复杂度 根号n
    务端如何防止重复支付 架构文摘 2021-05-02
    工具大于约定和文档
    千亿级公司低代码平台的测试体系介绍
    疑惑 题解
    计算几何相关总结
    树 题解
    矩阵加速相关总结
    loj6274 数字 题解
  • 原文地址:https://www.cnblogs.com/HansBug/p/4433381.html
Copyright © 2011-2022 走看看