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\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完事,具体看代码

 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.       
时间: 2024-10-01 05:00:16

1000: A+B Problem(NetWork Flow)的相关文章

HDU - 1532 - Drainage Ditches &amp;&amp; 3549 - Flow Problem (网络流初步)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10875    Accepted Submission(s): 5131 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie'

HDU 3549 Flow Problem(最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 12625    Accepted Submission(s): 6004 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph,

HDU3549Flow Problem(模板题)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 9769    Accepted Submission(s): 4577 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

FOJ有奖月赛-2016年8月 Problem A Daxia &amp; Wzc&#39;s problem(找规律)

Problem A Daxia & Wzc's problem Accept: 42    Submit: 228Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description Daxia在2016年5月期间去瑞士度蜜月,顺便拜访了Wzc,Wzc给他出了一个问题: Wzc给Daxia等差数列A(0),告诉Daxia首项a和公差d; 首先让Daxia求出数列A(0)前n项和,得到新数列A(1); 然后让Daxia求出数列A(

Swift学习笔记 - 教程学习四 控制流(Control Flow)

4 控制流(Control Flow) 4.1 For-In Loops 用for-in循环遍历一个序列,如一组连续数.数组元素.字符串中字符等. for index in 1...5 { print("\(index) times 5 is \(index * 5)") } // 1 times 5 is 5 // 2 times 5 is 10 // 3 times 5 is 15 // 4 times 5 is 20 // 5 times 5 is 25 index是一个在每次循

Joseph Problem(详细解法)

前言 这几天学习了队列,于是尝试了一下Joseph Problem,然后一直有一个地方没有通过,终于经点拨后,通过了.对于队列的使用还是加深了印象.建议如果队列的练手可以去尝试解决这个问题. 正文 Joseph Problem(35分) 铺垫: 假设有N个人围成一圈,然后第一个人把第二个人杀了,刀子给第三个人,第三个人把第四个人杀了,以此类推...... 首先注意:难点1:围成一圈,我们会想用什么数据结构呢?这么多人,首先应该想到用数组对不对,可是怎么表示围成一圈呢?重点来了,其实我们可以不断的

背包DP 方案数---P1832 A+B Problem(再升级)

P1832 A+B Problem(再升级) 题面描述 给定一个正整数n,求将其分解成若干个素数之和的方案总数. 题解 我们可以考虑背包DP实现 背包DP方案数板子题 f[ i ] = f[ i ] + f[ i - a[j] ] f[ j ] 表示数字 j 用若干个素数表示的方案总数 注意 1.线性筛不要写错: 1)not_prime[maxn] maxn>=n   2)memset not_prime 数组之后,0,1初始化不是素数 2.方案数 DP 数组要开 long long 代码 #i

[ACM] 1016 Prime Ring Problem (深度优先搜索)

Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle