前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1000: A+B Problem(NetWork Flow)

1000: A+B Problem(NetWork Flow)

作者头像
HansBug
发布2018-04-11 10:25:41
6500
发布2018-04-11 10:25:41
举报
文章被收录于专栏:HansBug's LabHansBug's Lab

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++:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#include 



int main()

{

    int a,b;

    scanf("%d %d",&a, &b);

    printf("%d\n",a+b);

    return 0;

}

Here is a sample solution for problem 1000 using PASCAL:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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完事,具体看代码

代码语言:javascript
复制
 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.       
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1000: A+B Problem
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档