1000: A+B Problem(NetWork Flow)

时间:2020-12-14 11:16:15

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\n",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完事,具体看代码
 /**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next,anti:point;
end;
var
i,j,k,l,m,n,ans,s,t,x,y:longint;
a:array[..] of point;
d,dv:array[..] of longint;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
procedure add(x,y,z:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
new(p);p^.g:=x;p^.w:=;p^.next:=a[y];a[y]:=p;
a[x]^.anti:=a[y];a[y]^.anti:=a[x];
end;
function dfs(x,flow:longint):longint;
var p:point;k:longint;
begin
if x=t then exit(flow);
dfs:=;p:=a[x];
while p<>nil do
begin
if (p^.w<>) and (d[x]=(d[p^.g]+)) then
begin
k:=dfs(p^.g,min(flow-dfs,p^.w));
if p^.w<>maxlongint then dec(p^.w,k);
if p^.anti^.w<>maxlongint then inc(p^.anti^.w,k);
inc(dfs,k);if dfs=flow then exit;
end;
p:=p^.next;
end;
if d[s]=n then exit;
dec(dv[d[x]]);
if dv[d[x]]= then d[s]:=n;
inc(d[x]);inc(dv[d[x]]);
end;
begin
readln(x,y);n:=;s:=;t:=;
for i:= to n do a[i]:=nil;
add(s,t,x);add(s,t,y);
fillchar(d,sizeof(d),);
fillchar(dv,sizeof(dv),);
dv[]:=n;ans:=;
while d[s]<n do inc(ans,dfs(s,maxlongint));
writeln(ans);
readln;
end.