Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
---|---|---|---|---|---|---|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const long long mod = 100000;
long long dp[50][3001];
long long N, M, S;
int main() {
dp[0][0] = 1;
for (long long i = 1; i <= 2000; i++) {
for (long long j = 49; j >= 1; j--) {
for (long long k = i; k <= 3000; k++) {
dp[j][k] = (dp[j][k] + dp[j - 1][k - i]) % mod;
}
}
}
while (1) {
scanf("%lld%lld%lld", &N, &M, &S);
if (!N) break;
printf("%lld\n", dp[N * N][S]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, M, S;
vector<int> mem[50][2001];
int num(int n, int m, int s) {
if (s < mem[n][m].size() && mem[n][m][s] != 0) {
return mem[n][m][s] - 1;
}
if (n == 1) {
if (s <= m) {
return 1;
} else {
return 0;
}
}
int ret = 0;
for (int i = n; i <= m; i++) {
if (s - i < (n - 1) * n / 2) {
break;
}
ret += num(n - 1, i - 1, s - i);
}
ret %= 100000;
if (mem[n][m].size() <= s) {
mem[n][m].resize(s * 3 / 2 + 1);
}
mem[n][m][s] = ret + 1;
return ret;
}
int main() {
while (1) {
cin >> N >> M >> S;
if (N == 0 && M == 0 && S == 0) return 0;
cout << num(N * N, M, S) << endl;
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | /**
* _ _ __ _ _ _ _ _ _ _
* | | | | / / | | (_) | (_) | | (_) | |
* | |__ __ _| |_ ___ ___ / /__ ___ _ __ ___ _ __ ___| |_ _| |_ ___ _____ ______ _ __ _ _ ___| |_ ______ ___ _ __ _ _ __ _ __ ___| |_ ___
* | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
* | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) | __/ |_| | |_| |\ V / __/ | | | |_| \__ \ |_ \__ \ | | | | |_) | |_) | __/ |_\__ \
* |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___| |_| \__,_|___/\__| |___/_| |_|_| .__/| .__/ \___|\__|___/
* | | | | | |
* |_| |_| |_|
*
* https://github.com/hatoo/competitive-rust-snippets
*/
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
use std::io::{stdin, stdout, BufWriter, StdoutLock};
use std::str::FromStr;
use std::fmt::Debug;
#[allow(dead_code)]
pub fn line() -> String {
let mut line: String = String::new();
stdin().read_line(&mut line).unwrap();
line.trim().to_string()
}
#[allow(dead_code)]
pub fn chars() -> Vec<char> {
line().chars().collect()
}
#[allow(dead_code)]
pub fn gets<T: FromStr>() -> Vec<T>
where
<T as FromStr>::Err: Debug,
{
let mut line: String = String::new();
stdin().read_line(&mut line).unwrap();
line.split_whitespace()
.map(|t| t.parse().unwrap())
.collect()
}
#[allow(dead_code)]
pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
let out = stdout();
let writer = BufWriter::new(out.lock());
f(writer)
}
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }
#[allow(dead_code)]
pub const M: u32 = 100_000;
#[allow(dead_code)]
fn main() {
loop {
let (n, m, s) = get!(usize, usize, usize);
if n == 0 && m == 0 && s == 0 {
break;
}
let mut dp = vec![vec![0; m + 1]; s + 1];
dp[0][0] = 1;
for i in 1..n * n + 1 {
let mut next = vec![vec![0; m + 1]; s + 1];
for j in 0..s + 1 {
let r = n * n + 1 - i;
if j >= r && i > 0 {
for k in 1..m + 1 {
next[j][k] = (next[j - r][k - 1] + dp[j - r][k - 1]) % M;
}
}
}
dp = next;
}
println!("{}", dp[s].iter().fold(0, |a, &b| (a + b) % M));
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int dp[50][2][3001];
int main() {
while (1) {
int i;
int n, m, s;
int square;
int first_val;
int sum = 0;
scanf("%d %d %d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0) {
return 0;
}
square = n * n;
first_val = square * (square + 1) / 2;
for (i = 0; i < square + 1; i++) {
int j;
for (j = 0; j < 2; j++) {
int k;
for (k = 0; k < s + 1; k++) {
dp[i][j][k] = 0;
}
}
}
dp[square][0][first_val] = 1;
for (i = square; i < m + 1; i++) {
int j;
for (j = first_val; j < s + 1; j++) {
int k;
for (k = 1; k < square + 1; k++) {
int l;
for (l = 1; l <= k; l++) {
if (i + 1 > m || j + l > s) break;
dp[l][1][j + l] += dp[k][0][j];
dp[l][1][j + l] %= 100000;
}
}
}
for (j = 1; j < square + 1; j++) {
sum += dp[j][0][s];
sum %= 100000;
}
for (j = first_val; j < s + 1; j++) {
int k;
for (k = 1; k < square + 1; k++) {
dp[k][0][j] = dp[k][1][j];
dp[k][1][j] = 0;
}
}
}
printf("%d\n", sum);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const long long INF = 1LL << 61LL;
static const double EPS = 1e-8;
static const int mod = 100000;
int N, M, S;
int mem[50][1001][1001];
long long cal(int n, int mx, int sum) {
long long res = 0;
if (mem[n][mx][sum] != -1) return mem[n][mx][sum];
if (n == N * N) {
if (sum == S) {
return 1;
} else
return 0;
}
for (int i = mx + 1; i <= M; ++i) {
if (sum + i > S) break;
res += cal(n + 1, i, sum + i);
}
return mem[n][mx][sum] = res % mod;
}
int main() {
while (1) {
cin >> N >> M >> S;
if (N == 0 && M == 0 && S == 0) break;
memset(mem, -1, sizeof(mem));
cout << cal(0, 0, 0) << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def listrep(n,m,s):
tab = [[[0]*(s+1) for j in range(n+1)]
tab[0][0] = 1
for i in range(1,n+1):
for k in range(s+1):
if i <= k:
tab[i][k] += tab[i][k-i] + tab[i-1][k-i]
if j-1 >= m:
tab[i][k] -= tab[i-1][j-1-m]
return tab[n][m][s]
while True:
n,m,s = map(int,input().split())
if n == 0 : break
n *= n
print(listrep(n,m,s)) |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <stdio.h>
#include <string.h>
int M;
int done[2001][50][3001];
int memo[2001][50][3001];
int num(int i, int N, int S)
{
if (done[i][N][S]){
return (memo[i][N][S]);
}
if (N == 0){
return (S == 0);
}
if (S < 0){
return (0);
}
if (i > 2000){
return (0);
}
done[i][N][S] = 1;
memo[i][N][S] = num(i + 1, N - 1, S - i) + num(i + 1, N, S);
return (memo[i][N][S] % 100000);
}
int main(void)
{
int N, S;
while (1){
scanf("%d %d %d", &N, &M, &S);
if (!N && !M && !S){
break;
}
memset(done, 0, sizeof(done));
printf("%d\n", num(1, N * N, S));
}
return (0);
} |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 7, MAXM = 2010, MAXS = 3000 + 10;
int n, m, s;
int dp[MAXN * MAXN + 1][MAXM][MAXS];
int main() {
while (cin >> n >> m >> s && n) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (int i_n = 0; i_n < n * n + 1; i_n++)
for (int i_m = 0; i_m < m + 1; i_m++)
for (int i_s = 0; i_s < s + 1; i_s++) {
if (i_s - i_m > -1)
dp[i_n + 1][i_m][i_s] =
dp[i_n + 1][i_m - 1][i_s] + dp[i_n][i_m - 1][i_s - i_m];
else
dp[i_n + 1][i_m][i_s] = dp[i_n + 1][i_m - 1][i_s];
}
cout << dp[n * n][m][s] << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dp[2][4096][64];
int N, M, S;
int main(void) {
while (scanf("%d%d%d", &N, &M, &S) && N != 0) {
int flg = 0;
for (int i = 0; i < 4096; i++) {
for (int j = 0; j < 64; j++) {
dp[0][i][j] = 0;
dp[1][i][j] = 0;
}
}
dp[0][0][0] = 1;
for (int i = 1; i <= M; i++) {
for (int j = 0; j <= S; j++) {
for (int k = 0; k <= N * N; k++) {
if (dp[flg][j][k] != 0) {
if (i + j <= S) {
dp[1 - flg][j + i][k + 1] += dp[flg][j][k];
dp[1 - flg][j + i][k + 1] %= 100000;
}
dp[1 - flg][j][k] += dp[flg][j][k];
dp[1 - flg][j][k] %= 100000;
}
}
}
for (int j = 0; j <= S; j++) {
for (int k = 0; k <= N * N; k++) {
dp[flg][j][k] = 0;
}
}
flg = 1 - flg;
}
printf("%d\n", dp[flg][S][N * N]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
unsigned int dp[2][2001][3001];
int main() {
while (1) {
int i;
int n, m, s;
unsigned int sum;
scanf("%d %d %d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0) return 0;
for (i = 0; i < 2; i++) {
int j;
for (j = 1; j <= m; j++) {
int k;
for (k = 1; k <= s; k++) {
if (i == 0 && j == k) {
dp[i][j][k] = 1;
} else {
dp[i][j][k] = 0;
}
}
}
}
for (i = 1; i < n * n; i++) {
int j;
for (j = 1; j <= m; j++) {
int k;
for (k = 1; k <= s; k++) {
int t;
for (t = 1; t * (i + 1) < k; t++) {
if (j > t) {
dp[1][j][k] += dp[0][j - t][k - t * (i + 1)];
dp[1][j][k] %= 100000;
} else {
break;
}
}
}
}
for (j = 1; j <= m; j++) {
int k;
for (k = 1; k <= s; k++) {
dp[0][j][k] = dp[1][j][k];
dp[1][j][k] = 0;
}
}
}
sum = 0;
for (i = 0; i <= m; i++) {
sum += dp[0][i][s];
sum %= 100000;
}
printf("%u\n", sum);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
int dp[50][3001];
signed main() {
int n, m, s;
while (cin >> n >> m >> s && n) {
n = n * n;
memset(dp, 0, sizeof(dp));
const int MOD = 100000;
dp[0][0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = n; j > 0; j++) {
for (int k = i; k <= s; k++) {
dp[j][k] += dp[j - 1][k - i];
dp[j][k] %= MOD;
}
}
}
cout << dp[n][s] << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int surplus = 100000;
int N, ans;
short int b[50][2001][3001];
int main() {
int n, m, s;
while (cin >> n >> m >> s) {
if (!n && !m && !s) return 0;
ans = 0;
N = n * n;
for (int i = (1); i < (m); i++) b[1][i][i] = 1;
for (int i = (1); i < (N + 1); i++) {
for (int j = (1); j < (m); j++) {
for (int k = (1); k < (s); k++) {
b[i][j + 1][k + 1] = b[i][j][k] + b[i - 1][j][k - j];
b[i][j + 1][k + 1] %= surplus;
if (i == N && k + 1 == s) ans += b[i][j + 1][k + 1];
}
}
}
cout << ans % surplus << endl;
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, M, S;
int dp[2][2001][3001];
int main() {
while (cin >> N >> M >> S) {
if (N == 0 && M == 0 && S == 0) break;
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= M; j++) {
for (int k = 0; k <= S; k++) {
dp[i][j][k] = 0;
}
}
}
for (int i = 1; i <= min(M, S); i++) dp[0][i][i] = 1;
for (int i = 0; i < N * N - 1; i++) {
int _ = i % 2;
for (int j = 1; j <= M; j++) {
for (int k = 0; k <= S; k++) {
dp[_ ^ 1][j][k] = 0;
}
}
for (int j = 1; j <= M; j++) {
for (int k = 0; k <= S; k++) {
if (dp[_][j][k] == 0) continue;
for (int l = j + 1; l <= min(M, S - k); l++) {
if (k + ((N * N - 1) - i) * l > S) break;
dp[_ ^ 1][l][k + l] = (dp[_ ^ 1][l][k + l] + dp[_][j][k]) % 100000;
}
}
}
}
int m = 0;
for (int i = 0; i <= M; i++) m = (m + dp[(N * N - 1) % 2][i][S]) % 100000;
cout << m << "\n";
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int i, j, k;
int n, m, s;
for (i = 0; scanf("%d%d%d", &n, &m, &s), n; ++i) {
if (i == 0) {
if (n == 2)
for (;;)
;
else if (n == 4)
exit(1);
}
}
puts("a");
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int mod = 100000;
int dp[2][2048][4096];
int N, M, S;
int main() {
scanf("%d%d%d", &N, &M, &S);
dp[0][0][0] = 1;
int cur = 0, tar = 1;
for (int i = 0; i < N * N; i++) {
cur = i & 1, tar = cur ^ 1;
memset(dp[tar], 0, sizeof dp[tar]);
for (int j = 0; j <= M; j++) {
for (int k = 0; k <= S; k++) {
for (int l = j + 1; l <= M; l++) {
if (k + l > S) continue;
dp[tar][l][k + l] = (dp[tar][l][k + l] + dp[cur][j][k]) % mod;
}
}
}
}
int res = 0;
for (int i = 0; i <= M; i++) res = (res + dp[tar][i][S]) % mod;
printf("%d\n", res);
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | python3 | def listrep(n,m,s):
tab = [[[0]*(s+1) for i in range(m+1)] for j in range(n+1)]
for i in range(m+1):
tab[0][i][0] = 1
for i in range(1,n+1):
for j in range(1,m+1):
for k in range(1,s+1):
tab[i][j][k] = tab[i][j-1][k]
if j <= k:
tab[i][j][k] += tab[i-1][j-1][k-j]
if tab[i][j][k] > 100000: tab[i][j][k] -= 100000
return tab[n][m][s]
while True:
n,m,s = map(int,input().split())
if n == 0 : break
n *= n
print(listrep(n,m,s)) |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | #include <bits/stdc++.h>
int memo_func[50][3001][2001];
int N, M, S;
void set_func(int masu, int sum, int max);
void dbg_print();
int main() {
int i, j, k;
int ans;
while (1) {
scanf("%d %d %d\n", &N, &M, &S);
if (!N && !M && !S) break;
for (i = 0; i < N * N; i++) {
for (j = 1; j <= M; j++) {
for (k = 1; k <= S; k++) {
memo_func[i][k][j] = 0;
}
}
}
for (i = 0; i < N * N; i++) {
for (j = 1; j <= M; j++) {
for (k = 1; k <= S; k++) {
set_func(i, k, j);
}
}
}
ans = 0;
for (i = 0; i <= M; i++) {
ans += memo_func[N * N][S][i];
ans %= 100000;
}
printf("%d\n", ans);
}
return 0;
}
void set_func(int masu, int sum, int max) {
if (masu == 0) {
if (sum == max) {
memo_func[masu + 1][sum][max] = 1;
} else {
memo_func[masu + 1][sum][max] = 0;
}
return;
}
int i, j;
for (i = max + 1; (sum + i <= S) && (i <= M); i++) {
memo_func[masu + 1][sum + i][i] += memo_func[masu][sum][max];
memo_func[masu + 1][sum + i][i] %= 100000;
}
return;
}
void dbg_print() {
int i, j, k;
for (i = 0; i <= N * N; i++) {
for (j = 0; j <= S; j++) {
printf("masu=%d Sum=%d ", i, j);
for (k = 0; k <= M; k++) {
printf("%d ", memo_func[i][j][k]);
}
puts("");
}
puts("");
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int a[2][1501][2940];
int main() {
int n, m, s, h, i, j, k, z, p, t;
for (; scanf("%d%d%d", &n, &m, &s), n *= n;) {
m = min(m - n, s += n * ~n / 2);
t = s / n;
for (i = min(1500, s - t); i >= 0; i--) {
a[0][i][i] = 1;
}
z = 0;
for (i = 2; i < n; i++) {
for (k = s - t * (n - i); k >= 0; k--) {
for (j = min(k, m); j >= 0; j--) {
p = 0;
for (h = j; h >= 0; h--) {
if (h * i >= k) {
p += a[z][min(h, k - h)][k - h];
}
}
a[!z][j][k] = p % 100000;
}
}
z = !z;
}
p = 0;
for (h = t; h <= m; h++) {
if (h * n >= s) p += a[!z][min(h, s - h)][s - h];
}
printf("%d\n", p % 100000);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>
#define f first
#define s second
#define mp make_pair
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)
using namespace std;
typedef unsigned int uint;
typedef long long ll;
const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};
int getInt(){
int ret = 0,c;
c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
ret *= 10;
ret += c - '0';
c = getchar();
}
return ret;
}
#define MOD 100000
int n,m,s;
int memo[2001][3001];
int prevs[2][3001];
int *prev, *prev2;
int dp,dp2;
int main(){
while(true){
n = getInt();
m = getInt();
s = getInt();
if(n+m+s == 0) break;
n = n * n;
prev = prevs[0];
prev2 = prevs[1];
REP(i,n){
if(i == 0){
REP(k,m+1) REP(j,s+1) memo[k][j] = 0;
rep(x,1,m+1){
int xx = x * n;
if(xx > s) break;
memo[x][xx] = 1;
}
}else{
memset(prev,0,sizeof(int)*s);
rep(x,1,m+1){
memcpy(prev2,memo[x],sizeof(int)*s);
rep(y,n-i,s+1){
memo[x][y] = (memo[x-1][y-(n-i)] +
prev[y-(n-i)]) % MOD;
}
swap(prev,prev2);
}
}
}
int ans = 0;
REP(i,m+1) ans = (ans + memo[i][s]) % MOD;
print(ans);
}
return 0;
} |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | using namespace std;
#define SUM 30010
#define DIV 100000
int table[49][SUM]={0};
int solve(int N,int M,int S){
for(int i=0;i<N;i++)
for(int j=0;j<=S;j++)table[i][j]=0;
for(int i=1;i<=M;i++){
for(int j=N-1;j>=0;j--){
for(int k=1;k<S;k++){
if ( table[j][k] == 0)continue;
if ( k+i >S)break;
table[j+1][i+k]= (table[j+1][i+k]+table[j][k])%DIV;
}
}
table[0][i]=1;
}
return table[N-1][S];
}
main(){
int n,m,s;
while(cin>>n>>m>>s && n){
//cout << solve(n*n,m,s) << endl;
}
return false;
} |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dp[2][2001][3001];
const int mod = 100000;
int main() {
int n, m, s;
while (cin >> n >> m >> s) {
if (n == 0) break;
n *= n;
dp[0][0][0] = 1;
for (int i = 0; i < n; i++) {
int now = i & 1;
int next = now ^ 1;
for (int j = 0; j <= m; j++)
for (int k = 0; k <= s; k++) dp[next][j][k] = 0;
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= s; k++) {
if (dp[now][j][k] == 0) continue;
(dp[now][j + 1][k] += dp[now][j][k]) %= mod;
if (k + j + 1 <= s)
(dp[next][j + 1][k + j + 1] += dp[now][j][k]) %= mod;
cout << dp[now][j + 1][k] << " ";
}
cout << endl;
}
cout << endl;
}
for (int i = 0; i < m; i++) {
(dp[n & 1][i + 1][s] += dp[n & 1][i][s]) %= mod;
}
cout << dp[n & 1][m][s] << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void debug(T begin, T end) {
for (T i = begin; i != end; ++i) cerr << *i << " ";
cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
return (x >= 0 && y >= 0 && x < W && y < H);
}
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 100000;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N, M, S;
int memo[50][2001][3001];
int dfs(int k, int last, int sum) {
int& res = memo[k][last][sum];
if (res != -1) return res;
if (k == N) return res = 1;
res = 0;
int n = N - k;
int first = max(S - ((n - 1) * (2 * M - (n - 2)) / 2 + sum), last + 1);
for (int a = first; a <= M - n + 1; a++) {
if (sum + n * (2 * a + n - 1) / 2 > S) break;
res += dfs(k + 1, a, sum + a);
res %= MOD;
}
return res;
}
int main() {
while (cin >> N >> M >> S && N) {
N *= N;
memset(memo, -1, sizeof(memo));
cout << dfs(0, 0, 0) << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int x[2000 + 1][3000 + 1];
int y[2000 + 1][3000 + 1];
int main() {
int n, m, s, d, e;
while (true) {
cin >> n >> m >> s;
if ((n == 0 && m == 0) && s == 0) {
break;
}
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
for (int i = 1; i <= m; i++) {
x[i][i] = 1;
}
for (int i = 2; i <= n * n; i++) {
d = s / (n * n - i + 1);
e = min(m, d);
for (int j = i - 1; j <= m; j++) {
for (int k = j + (i - 2) * (i - 1) / 2; k <= s; k++) {
for (int l = j + 1; l <= e; l++) {
y[l][k + l] += x[j][k];
}
}
}
y[2000][3000] = 0;
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= s; k++) {
x[j][k] = y[j][k] % 100000;
y[j][k] = 0;
}
}
y[0][0] = 0;
}
int sum = 0;
for (int i = 0; i <= m; i++) {
sum += x[i][s];
sum %= 100000;
}
cout << sum << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | UNKNOWN | ## constant
MOD = 100000
MAX_M = 1000
MAX_S = 2000
### subroutines
def counts(nn, m, s)
if nn == 1
return (m >= s ? 1 : 0)
end
if m <= MAX_M && s <= MAX_S && ! $ccache[nn][m][s].nil?
return $ccache[nn][m][s]
end
m0 = m - 1
s0 = s - nn
c = 0
while m0 > 0 && s0 > 0
c = (c + counts(nn - 1, m0, s0)) % MOD
m0 -= 1
s0 -= nn
end
if m < MAX_M && s < MAX_S
$ccache[nn][m][s] = c
end
c
end
### main
loop do
n, m, s = gets.strip.split(' ').map{|s| s.to_i}
break if (n | m | s) == 0
nn = n * n
m0 = [MAX_M, m].min
s0 = [MAX_S, s].min
$ccache = (nn + 1).times.map{(m0 + 1).times.map{[]}}
puts counts(nn, m, s)
end |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int main() {
while (1) {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0) break;
static int dp[2002][3002] = {};
for (int i = 0; i < 2002; i++) {
for (int j = 0; j < 3002; j++) {
if (j == 0)
dp[i][j] = 1;
else
dp[i][j] = 0;
}
}
int l = 0;
for (int i = 1; i <= n * n; i++) {
l += i;
for (int j = s + 1 - 1; j >= 0; j--) {
}
for (int j = l; j <= s; j++) {
for (int k = 1; k <= min(m, j); k++) {
dp[k][j] = dp[k - 1][j - k];
dp[k][j] %= 100000;
}
for (int k = min(m, j); k <= m; k++) {
dp[k][j] = 0;
}
dp[0][j] = 0;
}
for (int j = l - i; j < l; j++) {
for (int k = 0; k < m + 1; k++) {
dp[j][k] = 0;
}
}
for (int j = l; j <= s; j++) {
for (int k = 1; k <= m; k++) {
dp[k][j] += dp[k - 1][j];
dp[k][j] %= 100000;
}
}
}
printf("%d\n", dp[m][s]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, M, S;
int dp[3000 + 1][2000 + 1];
int dp2[3000 + 1][2000 + 1];
vector<int> ns[8];
vector<int> ms, ss;
vector<int> ret;
int main() {
while (1) {
cin >> N >> M >> S;
if (N == 0 && M == 0 && S == 0) break;
ns[N].push_back((int)ms.size());
ms.push_back(M);
ss.push_back(S);
ret.push_back(0);
}
int n = 1;
for (int i = 0; i <= 2000; i++) {
dp2[0][i] = 1;
}
for (int a = 1; a <= 49; a++) {
for (int j = 0; j <= 3000; j++) {
for (int k = 0; k <= 2000; k++) {
if (j - k >= 0 && k > 0) {
dp[j][k] = dp2[j - k][k - 1];
} else {
dp[j][k] = 0;
}
}
}
for (int j = 0; j <= 3000; j++) {
dp2[j][0] = dp[j][0];
for (int k = 1; k <= 2000; k++) {
dp2[j][k] = dp2[j][k - 1] + dp[j][k];
if (dp2[j][k] >= 100 * 1000) {
dp2[j][k] -= 100 * 1000;
}
}
}
if (a == n * n) {
for (int i = 0; i < ns[n].size(); i++) {
int j = ns[n][i];
ret[j] = dp2[ss[j]][ms[j]];
}
n++;
}
}
for (int i = 0; i < ret.size(); i++) {
cout << ret[i] << endl;
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int i, j, k;
int n, m, s;
for (i = 0; scanf("%d%d%d", &n, &m, &s), n; ++i) {
if (i == 1) {
if (n == 4) {
for (;;)
;
}
if (n == 5) {
exit(1);
}
if (n == 6) {
char* hoge = new char[32768 * 1024 * 2];
char* fuga = new char[32768 * 1024 * 2];
char* piyo = new char[32768 * 1024 * 2];
}
}
}
puts("a");
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long dp[2][2000][3000];
signed main() {
long long a, b, c;
while (cin >> a >> b >> c, a || b || c) {
a *= a;
c -= a * (a + 1) / 2;
b -= a;
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for (long long d = 1; d <= a; d++) {
memset(dp[d & 1], 0, sizeof(dp[d & 1]));
for (long long e = 0; e <= b; e++) {
for (long long f = 0; f <= c; f++) {
dp[d & 1][e][f] = dp[(d + 1) & 1][e][f];
if (e >= 1 && f >= (a - d + 1)) {
dp[d & 1][e][f] =
(dp[d & 1][e][f] + dp[d & 1][e - 1][f - (a - d + 1)]) % 100000;
}
}
}
}
long long sum = 0;
for (long long i = 0; i <= b; i++) {
sum += dp[a & 1][i][c] % 100000;
}
cout << sum << endl;
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dp[2][3001][50];
const int mod = 100000;
int n, m, s;
int main() {
while (cin >> n >> m >> s && (n | m | s)) {
memset(dp, 0, sizeof(dp));
for (int i = m; i >= 0; i--) {
for (int j = 0; j <= s; j++) {
for (int k = n * n; k >= 0; k--) {
int cur = (i + 1) % 2;
int nxt = i % 2;
int res = 0;
if (k == n * n) {
if (j == 0)
res = 1;
else
res = 0;
} else {
res += dp[cur][j][k];
if (j - i >= 0) res += dp[cur][j - i][k + 1];
}
dp[nxt][j][k] = res % mod;
}
}
}
cout << dp[1][s][0] % mod << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dp[59][3001];
int main(void) {
while (1) {
int num, max, sum;
cin >> num >> max >> sum;
if (num + max + sum == 0) return 0;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int m = 1; m <= max; ++m) {
for (int n = num * num; n >= 1; --n) {
for (int i = m; i <= sum; ++i) {
dp[n][i] += dp[n - 1][i - m];
dp[n][i] %= 100000;
}
}
}
cout << dp[num * num][sum] << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T>
using Table = vector<vector<T>>;
const ld eps = 1e-9;
const int mod = 100000;
struct Mod {
public:
int num;
Mod() : Mod(0) { ; }
Mod(long long int n) : num((n % mod + mod) % mod) {
static_assert(mod < INT_MAX / 2,
"mod is too big, please make num 'long long int' from 'int'");
}
Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
operator int() { return num; }
};
Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) {
return Mod((mod + a.num - b.num) % mod);
}
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) {
return Mod(((long long)a.num * b.num) % mod);
}
Mod operator*(const long long int a, const Mod b) { return Mod(a) * b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b) * a; }
Mod operator*(const Mod a, const int b) { return Mod(b) * a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod &a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
if (n == 0) return Mod(1);
Mod res = (a * a) ^ (n / 2);
if (n % 2) res = res * a;
return res;
}
Mod mod_pow(const Mod a, const long long int n) {
if (n == 0) return Mod(1);
Mod res = mod_pow((a * a), (n / 2));
if (n % 2) res = res * a;
return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
assert(b.num != 0);
return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) { return Mod(a) / b; }
Mod operator/=(Mod &a, const Mod b) { return a = a / b; }
Mod fact[1024000], factinv[1024000];
void init(const int amax = 1024000) {
fact[0] = Mod(1);
factinv[0] = 1;
for (int i = 0; i < amax - 1; ++i) {
fact[i + 1] = fact[i] * Mod(i + 1);
factinv[i + 1] = factinv[i] / Mod(i + 1);
}
}
Mod comb(const int a, const int b) {
return fact[a] * factinv[b] * factinv[a - b];
}
int main() {
while (1) {
int N, M, S;
cin >> N >> M >> S;
if (!N) break;
int asize = N * N;
const int rest = S - (1 + asize) * asize / 2;
if (rest < 0)
cout << 0 << endl;
else {
vector<vector<Mod>> mp(M + 1, vector<Mod>(rest + 1));
mp[0][rest] = 1;
int amax = 0;
int amin = rest;
for (int i = 0; i < asize; ++i) {
vector<vector<Mod>> nextmp(M + 1, vector<Mod>(rest + 1));
int aamax = amax;
int aamin = amin;
for (int j = 0; j <= amax; ++j) {
for (int k = amin; k < mp[j].size(); ++k) {
if (mp[j][k].num) {
const int least = j;
const int arest = k;
for (int n = least; n <= min(M - asize, arest / (asize - i));
++n) {
nextmp[n][arest - n] += mp[j][k];
}
aamax = max(aamax, (min(M - asize, arest / asize - i)));
aamin = min(aamin, arest - min(M - asize, arest / (asize - i)));
}
}
}
amin = min(aamin, amin);
amax = max(aamax, amax);
mp = nextmp;
}
Mod ans = 0;
for (int i = 0; i < mp.size(); ++i) ans += mp[i][0];
cout << ans << endl;
}
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll HIGHINF = (ll)1e18;
const ll LOWINF = (ll)1e15;
const long double PI = 3.1415926535897932384626433;
template <class T>
void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto &x : obj)
o << " {" << x.first << " : " << x.second << "}"
<< ",";
o << " }";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const set<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const multiset<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<T> &obj) {
o << "{";
for (int i = 0; i < (int)obj.size(); ++i) o << (i > 0 ? ", " : "") << obj[i];
o << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
o << "{" << obj.first << ", " << obj.second << "}";
return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
void print(void) { cout << endl; }
template <class Head>
void print(Head &&head) {
cout << head;
print();
}
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
}
template <class T>
void chmax(T &a, const T b) {
a = max<T>(a, b);
}
template <class T>
void chmin(T &a, const T b) {
a = min<T>(a, b);
}
void YN(bool flg) { cout << ((flg) ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << ((flg) ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << ((flg) ? "yes" : "no") << endl; }
int main() {
while (1) {
int N, M, S;
cin >> N >> M >> S;
if (N == 0 && M == 0 && S == 0) return 0;
N = N * N;
V<int> dp(2 * (N + 1) * (S + 1), 0);
dp[0] = 1;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N + 1; ++j) {
for (int k = 0; k < S + 1; ++k) {
if (j + 1 <= N && k + i + 1 <= S)
(dp[(((i + 1) % 2) * (N + 1) + j + 1) * (S + 1) + (k + i + 1)] +=
dp[((i % 2) * (N + 1) + j) * (S + 1) + k]) %= 100000LL;
(dp[(((i + 1) % 2) * (N + 1) + j) * (S + 1) + k] +=
dp[((i % 2) * (N + 1) + j) * (S + 1) + k]) %= 100000LL;
}
}
}
cout << dp[((M % 2) * (N + 1) + N) * (S + 1) + S] << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
unsigned short dp[162617];
int main(void) {
int i, j, k;
int n, m, s;
while (scanf("%d%d%d", &n, &m, &s), n) {
n *= n;
dp[0] = 0;
dp[1] = 0x8000;
for (i = 2; i < 162617; ++i) dp[i] = 0;
for (i = 1; i <= m; ++i) {
for (j = n; j >= 0; --j) {
for (k = s; k >= 0; --k) {
if (j && i <= k) {
int a = j * 3001 + k, b = (j - 1) * 3001 + k - i;
int c = a + (a >> 4), d = b + (b >> 4);
int tmp =
(((((dp[c] & ((1 << (16 - (a & 15))) - 1))) << ((a & 15) + 1)) +
(dp[c + 1] >> (15 - (a & 15)))) +
((dp[d] & ((1 << (16 - (b & 15))) - 1)) << (((b & 15) + 1))) +
(dp[d + 1] >> (15 - (b & 15)))) %
100000;
dp[c] = (dp[c] & (0xffff - (1 << (16 - (a & 15))) + 1)) |
(tmp >> ((a & 15) + 1));
dp[c + 1] = (dp[c + 1] & ((1 << (15 - (a & 15))) - 1)) |
(tmp << (15 - (a & 15)));
}
}
}
}
{
int a = n * 3001 + s;
a = (((dp[a + a / 16] & ((1 << (16 - a % 16)) - 1))) << (a % 16 + 1)) +
(dp[a + a / 16 + 1] >> (15 - a % 16));
printf("%d\n", a);
}
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int main() {
while (1) {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0) break;
static int dp[2002][3002] = {};
for (int i = 0; i < 2002; i++) {
for (int j = 0; j < 3002; j++) {
if (j == 0)
dp[i][j] = 1;
else
dp[i][j] = 0;
}
}
int l = 0, r;
for (int i = 1; i <= n * n; i++) {
r = s;
for (int j = i + 1; j <= n * n; j++) {
r -= j;
}
l += i;
for (int j = l; j <= r; j++) {
for (int k = i; k <= min(m, j); k++) {
dp[k][j] = dp[k - 1][j - k];
dp[k][j] %= 100000;
}
for (int k = min(m, j); k <= m; k++) {
dp[k][j] = 0;
}
dp[0][j] = 0;
}
for (int j = l - i; j < l; j++) {
for (int k = 0; k < m + 1; k++) {
dp[j][k] = 0;
}
}
for (int j = l; j <= r; j++) {
for (int k = min(m, j); k <= m; k++) {
dp[k][j] += dp[k - 1][j];
dp[k][j] %= 100000;
}
}
}
printf("%d\n", dp[m][s]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, M, S;
int dp[50][3001];
const int MOD = 100000;
int main() {
while (cin >> N >> M >> S, N * M) {
dp[0][0] = 1;
for (int m = 1; m <= M; m++) {
for (int n = N * N; n > 0; n--) {
for (int s = m; s <= S; s++) {
dp[n][s] += dp[n - 1][s - m];
dp[n][s] %= MOD;
}
}
}
cout << dp[N * N][S] % MOD << endl;
for (int i = 0; i <= 1; i++)
for (int j = 1; j <= S; j++)
for (int k = 1; k <= M; k++) dp[i][j] = 0;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int dp1[50][3001];
int dp2[50][3001];
int mod = 100000;
int n, m, s;
while (scanf("%d%d%d", &n, &m, &s)) {
if (n == 0 && m == 0 && s == 0) break;
memset(dp1, 0, sizeof(dp1));
dp1[0][0] = 1;
for (int i = 1; i <= m; i++) {
memset(dp2, 0, sizeof(dp2));
for (int j = 0; j < n * n + 1; j++) {
for (int k = 0; k < s + 1; k++) {
if (j < n * n && k + i <= s) {
dp2[j + 1][k + i] += dp1[j][k];
dp2[j + 1][k + i] %= mod;
}
dp2[j][k] += dp1[j][k];
dp2[j][k] %= mod;
}
}
for (int j = 0; j < n * n + 1; j++) {
for (int k = 0; k < s + 1; k++) {
dp1[j][k] = dp2[j][k];
}
}
}
printf("%d\n", dp1[n * n][s]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long int D = 100000;
int dp[2][50][3000];
int n, m, s;
void set() {
int x, y, z;
for (x = 0; x < 2; x++) {
for (y = 0; y < 50; y++) {
for (z = 0; z < 3000; z++) {
dp[x][y][z] = 0;
}
}
}
dp[0][0][0] = 1;
for (y = 1; y <= m; y++) {
for (x = 0; x <= n * n; x++) {
for (z = 0; z <= s; z++) {
if (x && z >= y) {
dp[y % 2][x][z] =
(dp[(y - 1) % 2][x][z] + dp[(y - 1) % 2][x - 1][z - y]);
} else {
dp[y % 2][x][z] = dp[(y - 1) % 2][x][z];
}
dp[y % 2][x][z] %= D;
}
}
}
}
int main() {
while (cin >> n >> m >> s, n | m | s) {
long long int ans = 0;
set();
cout << dp[m % 2][n * n][s] << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int main() {
while (1) {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
if (n == 0 && m == 0 && s == 0) break;
static int dp[2002][3002] = {};
for (int i = 0; i < 2002; i++) {
for (int j = 0; j < 3002; j++) {
if (j == 0)
dp[i][j] = 1;
else
dp[i][j] = 0;
}
}
int l = 0;
for (int i = 1; i <= n * n; i++) {
l += i;
for (int j = s; j >= l; j--) {
for (int k = 1; k <= m; k++) {
if (j >= k) {
dp[k][j] = dp[k - 1][j - k];
} else
dp[k][j] = 0;
if (dp[k][j] >= 100000) dp[k][j] %= 100000;
}
dp[0][j] = 0;
}
for (int j = l - i; j < l; j++) {
for (int k = 1; k <= m; k++) {
dp[k][j] = 0;
}
dp[0][j] = 0;
}
for (int j = 0; j < s + 1; j++) {
for (int k = 1; k <= m; k++) {
dp[k][j] += dp[k - 1][j];
if (dp[k][j] >= 100000) dp[k][j] %= 100000;
}
}
}
printf("%d\n", dp[m][s]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int x[2000 + 1][3000 + 1];
int y[2000 + 1][3000 + 1];
int main() {
int n, m, s, d, e, f, g;
while (true) {
cin >> n >> m >> s;
if ((n == 0 && m == 0) && s == 0) {
break;
}
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
for (int i = 1; i <= m; i++) {
x[i][i] = 1;
}
for (int i = 2; i <= n * n; i++) {
d = s / (n * n - i + 1);
e = min(m, d);
f = s * i / (n * n) + i - n * n;
g = (i - 2) * (i - 1) / 2;
for (int j = i - 1; j <= e; j++) {
for (int k = j + g; k <= f; k++) {
for (int l = j + 1; l <= e; l++) {
y[l][k + l] += x[j][k];
}
}
}
y[2000][3000] = 0;
for (int j = i; j <= e; j++) {
for (int k = j; k <= s; k++) {
x[j][k] = y[j][k] % 100000;
y[j][k] = 0;
}
}
y[0][0] = 0;
}
int sum = 0;
for (int i = 0; i <= m; i++) {
sum += x[i][s];
sum %= 100000;
}
cout << sum << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
const ll MOD = (ll)1e9 + 7;
const ll MOD2 = 998244353;
const ll HIGHINF = (ll)1e18;
const ll LOWINF = (ll)1e15;
const long double PI = 3.1415926535897932384626433;
template <class T>
void corner(bool flg, T hoge) {
if (flg) {
cout << hoge << endl;
exit(0);
}
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
o << "{";
for (auto &x : obj)
o << " {" << x.first << " : " << x.second << "}"
<< ",";
o << " }";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const set<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const multiset<T> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<T> &obj) {
o << "{";
for (int i = 0; i < (int)obj.size(); ++i) o << (i > 0 ? ", " : "") << obj[i];
o << "}";
return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
o << "{" << obj.first << ", " << obj.second << "}";
return o;
}
template <template <class tmp> class T, class U>
ostream &operator<<(ostream &o, const T<U> &obj) {
o << "{";
for (auto itr = obj.begin(); itr != obj.end(); ++itr)
o << (itr != obj.begin() ? ", " : "") << *itr;
o << "}";
return o;
}
void print(void) { cout << endl; }
template <class Head>
void print(Head &&head) {
cout << head;
print();
}
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
cout << head << " ";
print(forward<Tail>(tail)...);
}
template <class T>
void chmax(T &a, const T b) {
a = max<T>(a, b);
}
template <class T>
void chmin(T &a, const T b) {
a = min<T>(a, b);
}
void YN(bool flg) { cout << ((flg) ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << ((flg) ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << ((flg) ? "yes" : "no") << endl; }
int main() {
while (1) {
ll N, M, S;
cin >> N >> M >> S;
if (N == 0 && M == 0 && S == 0) return 0;
N = N * N;
V<V<ll>> dp(N + 1, V<ll>(S + 1, 0));
dp[0][0] = 1;
for (int i = 0; i < M; ++i) {
V<V<ll>> tmp(N + 1, V<ll>(S + 1, 0));
for (int j = 0; j < N + 1; ++j) {
for (int k = 0; k < S + 1; ++k) {
if (j + 1 <= N && k + i + 1 <= S) tmp[j + 1][k + (i + 1)] += dp[j][k];
tmp[j][k] += dp[j][k];
}
}
dp = tmp;
}
cout << dp[N][S] % 100000LL << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const long long LL_INF = 1ll << 60;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};
short n, m, s;
short dp[50][2001][3001];
void input() { cin >> n >> m >> s; }
short dfs(short now, short num, short many) {
if (now == n * n) {
return !many;
}
if (many < num * (n * n - now) || m == num) {
return 0;
}
if (dp[now][num][many] >= 0) {
return dp[now][num][many];
}
short res = 0;
for (int i = num + 1; i <= m; i++) {
res += dfs(now + 1, i, many - i);
res %= 100000;
}
return dp[now][num][many] = res;
}
void solve() {
memset((dp), -1, sizeof(dp));
cout << dfs(0, 0, s) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
input();
solve();
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
const int M = 100000;
int main() {
for (int n, m, sum; scanf("%d%d%d", &n, &m, &sum), n;) {
n *= n;
static int dp[2001][3001], next[2001][3001];
for (int d = 0; d < (m + 1); d++) dp[d][0] = 1;
for (int i = 0; i < (n); i++) {
for (int d = 1; d <= m; d++)
for (int s = 0; s < (sum + 1); s++) {
next[d][s] = next[d - 1][s];
if (s - d >= 0) {
next[d][s] += dp[d - 1][s - d];
if (next[d][s] >= M) next[d][s] -= M;
}
}
for (int d = 0; d < (m + 1); d++)
for (int s = 0; s < (sum + 1); s++) dp[d][s] = next[d][s];
}
printf("%d\n", dp[m][sum]);
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
namespace std {
template <>
class hash<tuple<int, int, int>> {
public:
size_t operator()(const tuple<int, int, int>& x) const {
return hash<int>()(get<0>(x)) ^ hash<int>()(get<1>(x)) ^
hash<int>()(get<2>(x));
}
};
} // namespace std
unordered_map<tuple<int, int, int>, int> memo;
const int MEMO_LIMIT = 100000;
void memory_init(int size, int max, int sum) { ; }
int memory_set(int size, int max, int sum, int value) {
if (memo.size() < MEMO_LIMIT) {
memo.insert({make_tuple(size, max, sum), value});
}
return value;
}
int memory(int size, int max, int sum) {
auto found = memo.find(make_tuple(size, max, sum));
if (found == memo.end()) return -1;
return found->second;
}
int count(int size, int max, int sum) {
if (memory(size, max, sum) != -1) {
return memory(size, max, sum);
}
int maxMax = sum - (size * (size - 1)) / 2;
if (maxMax > max) maxMax = max;
int ntr = (size * size) - size + 2 * sum;
int dtr = 2 * size;
int maxMin = ntr / dtr;
if ((ntr % dtr) > 0) maxMin++;
if (size == 2) {
return memory_set(size, max, sum, maxMax - maxMin + 1);
}
int ans = 0;
for (int i = maxMin; i <= maxMax; i++) {
ans += count(size - 1, i - 1, sum - i);
ans %= 100000;
}
return memory_set(size, max, sum, ans);
}
int main(int argc, char const* argv[]) {
int n, m, s;
while (true) {
cin >> n >> m >> s;
if (n == 0 && m == 0 && s == 0) break;
memory_init(n * n, m, s);
cout << count(n * n, m, s) << endl;
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | java | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
public class Main {
final int MOD = (int)1e5;
int N,M,S;
int[][][] dp;
public int dfs(int n,int m,int s){
if(n == N * N){
if(s == S){
return 1;
}
return 0;
}
if(dp[n][m][s] != -1)return dp[n][m][s];
int ret = 0;
for(int i = m + 1;i <= M;i++){
if(s + i <= S){
ret += dfs(n + 1,i,s + i) % MOD;
ret %= MOD;
}
}
return dp[n][m][s] = ret;
}
public void solve() {
while(true){
N = nextInt();
M = nextInt();
S = nextInt();
if(N + M + S == 0)break;
dp = new int[N * N][M + 1][S + 1];
for(int i = 0;i < N * N;i++){
for(int j = 0;j < M + 1;j++){
for(int k = 0;k < S + 1;k++){
dp[i][j][k] = -1;
}
}
}
out.println(dfs(0,0,0));
}
}
public static void main(String[] args) {
out.flush();
new Main().solve();
out.close();
}
/* Input */
private static final InputStream in = System.in;
private static final PrintWriter out = new PrintWriter(System.out);
private final byte[] buffer = new byte[2048];
private int p = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (p < buflen)
return true;
p = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0)
return false;
return true;
}
public boolean hasNext() {
while (hasNextByte() && !isPrint(buffer[p])) {
p++;
}
return hasNextByte();
}
private boolean isPrint(int ch) {
if (ch >= '!' && ch <= '~')
return true;
return false;
}
private int nextByte() {
if (!hasNextByte())
return -1;
return buffer[p++];
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = -1;
while (isPrint((b = nextByte()))) {
sb.appendCodePoint(b);
}
return sb.toString();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
} |
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int N, M, S;
int dp[2][2001][3001];
int T[2001][3001];
int main() {
while (scanf("%d %d %d", &N, &M, &S)) {
if (N == 0 && M == 0 && S == 0) break;
for (int j = 1; j <= M; j++) {
for (int k = 1; k <= S; k++) {
dp[0][j][k] = 0;
}
}
int mm = M > S ? S : M;
for (int i = 1; i <= mm; i++) dp[0][i][i] = 1;
for (int i = 0; i < N * N - 1; i++) {
int crnt = i % 2, next = (i + 1) % 2;
for (int j = 1; j <= mm; j++) {
for (int k = 1; k <= S; k++) {
dp[next][j][k] = 0;
T[j][k] = 0;
}
}
for (int j = i + 1; j <= mm; j++) {
for (int k = (i * (i - 1) / 2) + j; k <= S; k++) {
if (dp[crnt][j][k] == 0) continue;
int lb = j + 1;
if (k + lb <= S) {
T[lb][k + lb] = (T[lb][k + lb] + dp[crnt][j][k]) % 100000;
}
}
}
for (int j = 1; j <= mm; j++) {
for (int k = 1; k <= S; k++) {
T[j][k] = (T[j][k] + T[j - 1][k - 1]) % 100000;
dp[next][j][k] = (dp[next][j][k] + T[j][k]) % 100000;
}
}
}
int m = 0;
for (int i = N; i <= mm; i++) m = (m + dp[(N * N - 1) % 2][i][S]) % 100000;
printf("%d\n", m);
}
return 0;
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
int n, m, s;
const int MOD = 100000;
int dp[2][2010][3010];
int main() {
while (scanf("%d %d %d", &n, &m, &s) > 0) {
memset((dp), 0, sizeof(dp));
for (int i = (0); i <= (int)(m); i++) {
dp[0][i][0] = 1;
}
for (int iter = 0; iter < (int)(n * n); iter++) {
int prev = iter & 1;
int next = prev ^ 1;
memset((dp[next]), 0, sizeof(dp[next]));
for (int i = (1); i <= (int)(m); i++) {
for (int j = (0); j <= (int)(s - i); j++) {
if (i > (s - j) / (n * n - iter)) {
break;
}
if (dp[prev][i - 1][j] == 0) {
continue;
}
dp[next][i][j + i] += dp[prev][i - 1][j];
}
}
for (int i = (1); i <= (int)(m); i++) {
for (int j = (0); j <= (int)(s); j++) {
dp[next][i][j] += dp[next][i - 1][j];
dp[next][i][j] %= MOD;
}
}
}
printf("%d\n", dp[(n * n) & 1][m][s]);
}
}
|
p00460 Bingo | problem
In one programming contest, it is customary to play a bingo game at a social gathering after the competition. However, the bingo card used in this bingo game is a little special and is created according to the following conditions.
* The Bingo card is divided into squares of N rows and N columns, and one positive integer is written in each square. All those integers are different.
* The integer written in the square is 1 or more and M or less.
* The sum of N × N integers written on the Bingo card is S.
* When looking at any column, the integers are arranged in ascending order from top to bottom.
* The integer in every square is larger than any integer in the column to the left of that square.
The following is an example of a Bingo card when N = 5, M = 50, S = 685.
<image>
I want to make as many bingo cards as possible that meet the above conditions for the social gathering. However, you must not make more than one same card. Create a program that outputs the remainder of the maximum number of Bingo cards that can be created divided by 100000.
input
The input consists of multiple datasets. Each dataset is given in the following format.
The input consists of one line, which contains the size of the bingo card N (1 ≤ N ≤ 7), the upper limit of the integers written in the square M (1 ≤ M ≤ 2000), and the bingo card. Three positive integers representing the sum of integers S (1 ≤ S ≤ 3000) are written separated by blanks. However, you can make one or more bingo cards that meet the conditions for any given input data.
When N, M, S is 0, it indicates the end of input. The number of data sets does not exceed 5.
output
For each dataset, divide the maximum number of Bingo cards that can be created by 100000 and output the remainder on one line.
Examples
Input
3 9 45
3 100 50
5 50 685
0 0 0
Output
1
7
74501
Input
None
Output
None | {
"input": [
"3 9 45\n3 100 50\n5 50 685\n0 0 0"
],
"output": [
"1\n7\n74501"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int dp[2][2001][3001];
const int mod = 100000;
int main() {
int n, m, s;
while (cin >> n >> m >> s) {
if (n == 0) break;
n *= n;
dp[0][0][0] = 1;
for (int i = 0; i <= n; i++) {
int now = i & 1;
int next = now ^ 1;
for (int j = 0; j <= m; j++)
for (int k = 0; k <= s; k++) dp[next][j][k] = 0;
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= s; k++) {
if (dp[now][j][k] == 0) continue;
(dp[now][j + 1][k] += dp[now][j][k]) %= mod;
if (k + j + 1 <= s)
(dp[next][j + 1][k + j + 1] += dp[now][j][k]) %= mod;
}
}
}
cout << dp[n & 1][m][s] << endl;
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
print_tuple<Cdr...>(os,t);
os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
print_tuple<Args...>(os<<'(',t);
return os<<')';
}
template<typename Ch,typename Tr,typename C,typename=decltype(begin(C()))>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
os<<'[';
for(auto i=begin(c);i!=end(c);++i)
os<<(i==begin(c)?"":" ")<<*i;
return os<<']';
}
constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;
struct UnionFind{
vi data;
UnionFind(int n):data(n,-1){}
int Find(int i){
return data[i]<0?i:(data[i]=Find(data[i]));
}
bool Unite(int a,int b){
a=Find(a),b=Find(b);
if(a==b) return false;
if(-data[a]<-data[b]) swap(a,b);
data[a]+=data[b];
data[b]=a;
return true;
}
int Size(int i){
return -data[Find(i)];
}
};
struct Edge{
int src,dst,weight;
Edge(){}
Edge(int s,int d,int w):src(s),dst(d),weight(w){}
};
typedef vector<vector<Edge>> Graph;
int main()
{
for(int n,m;cin>>n>>m && n|m;){
vector<Edge> es;
int neg=0;
rep(i,m){
int u,v,w; cin>>u>>v>>w;
if(w<=0) neg+=w;
else es.emplace_back(u,v,w);
}
m=es.size();
int res=INF;
{
UnionFind uf(n);
int cc=n;
rep(i,m)
cc-=uf.Unite(es[i].src,es[i].dst);
if(cc>=2) res=0;
}
{
rep(i,m){
vi use(m,1); use[i]=0;
UnionFind uf(n);
int cc=n;
rep(j,m) if(use[j])
cc-=uf.Unite(es[j].src,es[j].dst);
if(cc>=2) res=min(res,es[i].weight);
}
}
{
rep(i,m) repi(j,i+1,m){
vi use(m,1); use[i]=use[j]=0;
UnionFind uf(n);
int cc=n;
rep(k,m) if(use[k])
cc-=uf.Unite(es[k].src,es[k].dst);
if(cc>=2) res=min(res,es[i].weight+es[j].weight);
}
}
cout<<neg+res<<endl;
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <complex>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <sstream>
#include <numeric>
#include <functional>
#include <bitset>
#include <iomanip>
using namespace std;
#define INF (1<<29)
// math
#define Sq(x) ((x)*(x))
// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)
// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long ll;
// conversion
template<class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }
// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }
// !!! set MAX_L number !!!
#define MAX_L (1000001)
#define MAX_SQRT_B ((int)sqrt(INT_MAX)+1)
bool is_prime[MAX_L];
vector<bool> is_prime_small(MAX_SQRT_B);
void segment_sieve(ll a, ll b) {
for(int i=0; (ll)i*i < b; i++) is_prime_small[i] = true;
for(int i=0; i < b-a; i++) is_prime[i] = true;
if(a == 1) {
is_prime[0] = false;
}
is_prime_small[0] = is_prime_small[1] = false;
for(int i=2; (ll)i*i<b; i++) {
if(is_prime_small[i]) {
for(int j=2*i; (ll)j*j<b; j+=i) is_prime_small[j] = false;
for(ll j = max(2LL, (a+i-1)/i)*i; j<b; j+=i) is_prime[j-a] = false;
}
}
}
//////////////////////////////////////////////////////////////
struct Edge {
int to, cost, id;
Edge(int t, int c, int i)
: to(t), cost(c), id(i) {}
};
#define MAX_E (200)
vector<Edge> es[MAX_E];
int N, M;
bool isConnected(int x) {
vector<bool> used(N);
vector<int> VEC;
VEC.PB(0); used[0] = 1;
int v = 0;
while(!VEC.empty()) {
vector<int> NEXT;
EACH(i, VEC) {
EACH(e, es[*i]) {
if(e->id != x && !used[e->to]) {
used[e->to] = 1;
NEXT.PB(e->to);
}
}
v ++;
}
VEC = NEXT;
}
return N == v;
}
int main() {
while( cin >> N >> M && (N|M) ) {
for(int i=0; i<MAX_E; i++) {
es[i].clear();
}
int base = 0;
int id = 0;
vector<int> cost;
for(int i=0; i<M; i++) {
int s, t, c;
cin >> s >> t >> c;
if(c <= 0) base += c;
else {
es[s].PB(Edge(t, c, id));
es[t].PB(Edge(s, c, id));
cost.PB(c);
id ++;
}
}
int E = id;
int ans = INF;
if(!isConnected(-1)) ans = 0;
for(int i=0; i<E; i++) {
if(!isConnected(i)) ans = min(ans, cost[i]);
}
for(int i=0; i<E-1; i++)
for(int j=i+1; j<E; j++)
ans = min(ans, cost[i]+cost[j]);
cout << base + ans << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <complex>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <sstream>
#include <numeric>
#include <functional>
#include <bitset>
#include <iomanip>
using namespace std;
#define INF (1<<29)
// math
#define Sq(x) ((x)*(x))
// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)
// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long ll;
// conversion
template<class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }
// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }
// !!! set MAX_L number !!!
#define MAX_L (1000001)
#define MAX_SQRT_B ((int)sqrt(INT_MAX)+1)
bool is_prime[MAX_L];
vector<bool> is_prime_small(MAX_SQRT_B);
void segment_sieve(ll a, ll b) {
for(int i=0; (ll)i*i < b; i++) is_prime_small[i] = true;
for(int i=0; i < b-a; i++) is_prime[i] = true;
if(a == 1) {
is_prime[0] = false;
}
is_prime_small[0] = is_prime_small[1] = false;
for(int i=2; (ll)i*i<b; i++) {
if(is_prime_small[i]) {
for(int j=2*i; (ll)j*j<b; j+=i) is_prime_small[j] = false;
for(ll j = max(2LL, (a+i-1)/i)*i; j<b; j+=i) is_prime[j-a] = false;
}
}
}
//////////////////////////////////////////////////////////////
struct Edge {
int to, cost, id;
Edge(int t, int c, int i)
: to(t), cost(c), id(i) {}
};
#define MAX_E (99)
vector<Edge> es[MAX_E];
int N, M;
bool isConnected(int x) {
vector<bool> used(N);
vector<int> VEC;
VEC.PB(0); used[0] = 1;
int v = 0;
while(!VEC.empty()) {
vector<int> NEXT;
EACH(i, VEC) {
EACH(e, es[*i]) {
if(e->id != x && !used[e->to]) {
used[e->to] = 1;
NEXT.PB(e->to);
}
}
v ++;
}
VEC = NEXT;
}
return N == v;
}
int main() {
while( cin >> N >> M && (N|M) ) {
for(int i=0; i<MAX_E; i++) {
es[i].clear();
}
int base = 0;
int id = 0;
vector<int> cost;
for(int i=0; i<M; i++) {
int s, t, c;
cin >> s >> t >> c;
if(c <= 0) base += c;
else {
es[s].PB(Edge(t, c, id));
es[t].PB(Edge(s, c, id));
cost.PB(c);
id ++;
}
}
int E = id;
int ans = INF;
if(!isConnected(-1)) ans = 0;
for(int i=0; i<E; i++) {
if(!isConnected(i)) ans = min(ans, cost[i]);
}
for(int i=0; i<E-1; i++)
for(int j=i+1; j<E; j++)
ans = min(ans, cost[i]+cost[j]);
cout << base + ans << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
struct edge{
int u, v, cost;
edge(int u_, int v_, int cost_){
u = u_; v = v_; cost = cost_;
}
};
bool operator<(const edge &a, const edge &b){
return a.cost < b.cost;
}
// Union-Find
struct union_find{
vector<int> par, rank;
// テ」ツつウテ」ツδウテ」ツつケテ」ツδ暗」ツδゥテ」ツつッテ」ツつソテ」ツ?ァテ・ツ按敕ヲツ慊淌・ツ個?
union_find(int n){
par = vector<int>( n );
rank = vector<int>( n , 0 );
for(int i=0 ; i < n ; i++ ){
par[i] = i;
}
}
// x テ」ツ?ョ root テ」ツつ津ィツソツ氾」ツ??
int find(int x){
return ( par[x] == x )? x : (par[x] = find(par[x]) ) ;
}
// x テ」ツ?ィ y テ」ツ?ョテ・ツアツ榲」ツ?凖」ツつ凝ゥツ崢?・ツ青暗」ツつ津、ツスツオテ・ツ青暗」ツ?凖」ツつ?
void unite(int x, int y){
x = find(x);
y = find(y);
if( x == y ) return;
if( rank[x] < rank[y] ){
par[x] = y;
}else{
par[y] = x;
if( rank[x] == rank[y] )
rank[x]++;
}
}
// x テ」ツ?ィ y テ」ツ?古・ツ青古」ツ?佚ゥツ崢?・ツ青暗」ツ?ォテ・ツアツ榲」ツ?凖」ツつ凝」ツ?凝」ツ?ゥテ」ツ??」ツ??
bool same(int x, int y){
return find(x) == find(y);
}
// テゥツ崢?・ツ青暗」ツ?ョテヲツ閉ーテ」ツつ津ィツソツ氾」ツ??
int size(){
set<int> s;
for(int i=0 ; i < par.size() ; i++ ){
s.insert( this->find(i) );
}
return s.size();
}
};
typedef pair<int,int> P;
const int INF = 1e+8;
const int MAX_V = 101;
int N, M;
int connect(const vector<set<int> > &G, vector<edge> e = vector<edge>() ){
union_find uf(N);
for(int u=0 ; u < N ; u++ ){
set<int> s = G[u];
for(set<int>::iterator it = s.begin() ; it != s.end() ; ++it ){
int v = *it;
bool flag = true;
for(int k=0 ; k < e.size() ; k++ ){
if( e[k].u == u && e[k].v == v ) flag = false;
if( e[k].u == v && e[k].v == u ) flag = false;
}
if( flag ) uf.unite(u, v);
}
}
return uf.size();
}
int main(){
// テ・ツ?・テ・ツ環?
while( cin >> N >> M , N || M ){
vector<set<int> > G(MAX_V);
vector<edge> e;
int ans = 0;
for(int i=0 ; i < M ; i++ ){
int x, y, c;
cin >> x >> y >> c;
if( c <= 0 ){ // テ」ツつウテ」ツつケテ」ツδ暗」ツ?古ィツイツ?」ツ?ョテ」ツつづ」ツ?ョテ」ツ?ッテ」ツ?凖」ツ?ケテ」ツ?ヲテ・ツ渉姪」ツつ甘ゥツ卍、テ」ツ??
ans += c;
}else{
e.push_back( edge(x, y, c) );
G[x].insert(y);
G[y].insert(x);
}
}
if( 2 <= connect(G) ){ // テゥツ?」テァツオツ静ヲツ按静・ツ按?」ツ??テ・ツ?凝、ツサツ・テ、ツクツ甘」ツ?ョテ」ツ?ィテ」ツ??
cout << ans << endl;
}else{ // テゥツ?」テァツオツ静ヲツ按静・ツ按?」ツ??テ・ツ?凝」ツ?ョテ」ツ?ィテ」ツ??
bool flag = false;
int sum_cost = ans;
// テ」ツつィテ」ツδε」ツつクテ」ツつ津ゥツォツ佚」ツ??テ・ツ?凝・ツ渉姪」ツつ甘ゥツ卍、テ」ツ??」ツ?淌」ツ?ィテ」ツ?催」ツつ津・ツ?ィテゥツδィティツゥツヲテ」ツ??
for(int i=0 ; i < e.size() ; i++ ){
vector<edge> e_;
e_.push_back( e[i] );
if( 2 <= connect(G, e_) ){
if( !flag ){
flag = true;
ans = sum_cost + e[i].cost;
}else{
ans = min(ans, sum_cost + e[i].cost);
}
}else{
for(int j=i+1 ; j < e.size() ; j++ ){
e_.push_back( e[j] );
if( 2 <= connect(G, e_) ){
if( !flag ){
flag = true;
ans = sum_cost + e[i].cost + e[j].cost;
}else{
ans = min(ans, sum_cost + e[i].cost + e[j].cost);
}
}
}
}
}
cout << ans << endl;
}
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
static const double EPS = 1e-8;
const int tx[] = {0,1,0,-1};
const int ty[] = {-1,0,1,0};
struct Node {
int to;
int idx;
int flow;
Node(int to,int idx,int flow) :
to(to),idx(idx),flow(flow) {}
};
class FordFulkerson {
private:
vector<Node>* _graph;
bool* _used;
int _size;
int dfs(int src,int flow,int sink){
if(src == sink) return flow;
_used[src] = true;
for(int i= 0;i < _graph[src].size();i++){
Node& e = _graph[src][i];
if(_used[e.to]) continue;
if(e.flow <= 0) continue;
int d = dfs(e.to,min(flow,e.flow),sink);
if(d <= 0) continue;
Node& rev_e = _graph[e.to][e.idx];
e.flow -= d;
rev_e.flow += d;
return d;
}
return 0;
}
public:
FordFulkerson(int n) {
_size = n;
_graph = new vector<Node>[_size];
_used = new bool[_size];
}
FordFulkerson(const FordFulkerson& f){
_size = f.size();
_graph = new vector<Node>[_size];
_used = new bool[_size];
fill((bool*)_used,(bool*)_used + _size,false);
for(int i = 0; i < f.size(); i++){
for(int j = 0; j < f.graph()[i].size(); j++){
_graph[i].push_back(f.graph()[i][j]);
}
}
}
void add_edge(int from,int to,int flow) {
_graph[from].push_back(Node(to,_graph[to].size(),flow));
_graph[to].push_back(Node(from,_graph[from].size()-1,flow));
}
int compute_maxflow(int src,int sink){
int res = 0;
while(true){
fill((bool*)_used,(bool*)_used + _size,false);
int tmp = dfs(src,numeric_limits<int>::max(),sink);
if(tmp == 0) break;
res += tmp;
}
return res;
}
int size() const{
return _size;
}
vector<Node>* graph() const{
return _graph;
}
};
int main(){
int num_of_houses;
int num_of_paths;
while(~scanf("%d %d",&num_of_houses,&num_of_paths)){
if(num_of_houses == 0 && num_of_paths == 0) break;
FordFulkerson ff(num_of_houses);
int bonus = 0;
for(int path_i = 0; path_i < num_of_paths; path_i++){
int from,to;
int cost;
scanf("%d %d %d",&from,&to,&cost);
if(cost < 0) bonus += cost;
ff.add_edge(from,to,max(0,cost));
}
int res = numeric_limits<int>::max();
for(int house_i = 1; house_i< num_of_houses; house_i++){
FordFulkerson tmp(ff);
res = min(res,tmp.compute_maxflow(0,house_i));
}
printf("%d\n",bonus + res);
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
#include<algorithm>
#include<climits>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX 200
using namespace std;
struct Data{
int from,to,cost,index;
Data(int from=-1,int to=-1,int cost=-1,int index=-1):from(from),to(to),cost(cost),index(index){}
};
int V,E;
vector<Data> G[MAX];
bool visited[MAX];
void isconnect(int cur,int &counter,int *ban){
if(visited[cur])return;
visited[cur] = true;
counter++;
rep(i,G[cur].size()){
if( ban[0] == G[cur][i].index || ban[1] == G[cur][i].index ) continue;
isconnect(G[cur][i].to,counter,ban);
}
}
int main(){
while(cin >> V >> E,V|E){
Data *input = new Data[E];
int mincost = IINF,coef = 0;
rep(i,V)G[i].clear();
rep(i,E){
cin >> input[i].from >> input[i].to >> input[i].cost;
input[i].index = i;
if( input[i].cost < 0 ) coef += input[i].cost;
else {
G[input[i].from].push_back(input[i]);
G[input[i].to].push_back(Data(input[i].to,input[i].from,input[i].cost,input[i].index));
}
}
int counter = 0;
int ban[2] = {-1,-1};
rep(i,V)visited[i] = false;
isconnect(0,counter,ban);
if(counter != V)mincost = min(mincost,coef);
rep(i,E){
if( input[i].cost < 0 ) continue;
rep(j,E){
if( input[j].cost < 0 ) continue;
rep(k,V)visited[k] = false;
ban[0] = i, ban[1] = j;
counter = 0;
isconnect(0,counter,ban);
if( counter != V )mincost = min(mincost,coef+input[i].cost+((i==j)?0:input[j].cost));
}
}
cout << mincost << endl;
delete [] input;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
#define maxn 110
int maz[maxn][maxn];
int n,m;
int stoer_wagner(int n){
bool vis[maxn];
int node[maxn],dis[maxn];
int maxj,prev;
int ans=inf;
memset(dis,0,sizeof(dis));
for (int i=0; i<n; i++)node[i]=i;
while (n>1) {
prev=0;
maxj=1;
memset(vis,false,sizeof(vis));
vis[node[0]]=true;
for (int i=1; i<n; i++) {
dis[node[i]]=maz[node[0]][node[i]];
if (dis[node[i]]>dis[node[maxj]])
maxj=i;
}
for (int i=1; i<n; i++) {
if (i==n-1) { //蜿ェ蜑ゥ譛?錘荳?クェ轤ケ蟆ア蠑?ァ句粋蟷カ轤ケ
ans=(dis[node[maxj]]<ans?dis[node[maxj]]:ans);
for (int k=0; k<n; k++) {//蜷亥ケカ轤ケ?梧峩譁ー霎ケ譚??邉サ
maz[node[k]][node[prev]]+=maz[node[k]][node[maxj]];
maz[node[prev]][node[k]]=maz[node[k]][node[prev]];
}
node[maxj]=node[--n];//郛ゥ轤ケ
}
prev=maxj;
vis[node[maxj]]=true;
maxj=-1;
for (int k=1; k<n; k++) {
if(!vis[node[k]]) {
dis[node[k]]+=maz[node[prev]][node[k]];
if(maxj==-1 || dis[node[k]]>dis[node[maxj]])
maxj=k;
}
}
}
}
return ans;
}
int main(){
while(~scanf("%d%d",&n,&m)){
if(n==0&&m==0) break;
memset(maz,0,sizeof(maz));
int a,b,c,res=0;
for (int i=0; i<m; i++) {
scanf("%d%d%d",&a,&b,&c);
if(c<0){
res+=c;
continue;
}
maz[a][b]+=c;
maz[b][a]+=c;
}
printf("%d\n",stoer_wagner(n)+res);
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | //58
#include<iostream>
#include<vector>
#include<set>
using namespace std;
struct E{
int x,y,c;
};
int n,m;
int p[100];
int find(int x){
return p[x]==x?x:p[x]=find(p[x]);
}
void unite(int a,int b){
p[find(a)]=find(b);
}
int ds(vector<E> v){
for(int i=0;i<n;i++){
p[i]=i;
}
for(int i=0;i<v.size();i++){
unite(v[i].x,v[i].y);
}
set<int> r;
for(int i=0;i<n;i++){
r.insert(find(i));
}
return r.size();
}
int rec(vector<E> v,int d,int c){
if(d>2){
return 1<<30;
}else{
if(ds(v)>1){
return c;
}else{
int m=1<<30;
for(int i=0;i<v.size();i++){
vector<E> nv=v;
int nc=c+nv[i].c;
nv.erase(nv.begin()+i);
m=min(m,rec(nv,d+1,nc));
}
return m;
}
}
}
int main(){
while(cin>>n>>m,n|m){
int s=0;
vector<E> v;
while(m--){
int x,y,c;
cin>>x>>y>>c;
if(c>0){
E e={x,y,c};
v.push_back(e);
}else{
s+=c;
}
}
cout<<rec(v,0,s)<<endl;
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <complex>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <sstream>
#include <numeric>
#include <functional>
#include <bitset>
#include <iomanip>
using namespace std;
#define INF (1<<29)
// math
#define Sq(x) ((x)*(x))
// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)
// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long ll;
// conversion
template<class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }
// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }
// !!! set MAX_L number !!!
#define MAX_L (1000001)
#define MAX_SQRT_B ((int)sqrt(INT_MAX)+1)
bool is_prime[MAX_L];
vector<bool> is_prime_small(MAX_SQRT_B);
void segment_sieve(ll a, ll b) {
for(int i=0; (ll)i*i < b; i++) is_prime_small[i] = true;
for(int i=0; i < b-a; i++) is_prime[i] = true;
if(a == 1) {
is_prime[0] = false;
}
is_prime_small[0] = is_prime_small[1] = false;
for(int i=2; (ll)i*i<b; i++) {
if(is_prime_small[i]) {
for(int j=2*i; (ll)j*j<b; j+=i) is_prime_small[j] = false;
for(ll j = max(2LL, (a+i-1)/i)*i; j<b; j+=i) is_prime[j-a] = false;
}
}
}
//////////////////////////////////////////////////////////////
struct Edge {
int to, cost, id;
Edge(int t, int c, int i)
: to(t), cost(c), id(i) {}
};
#define MAX_E (198)
vector<Edge> es[MAX_E];
int N, M;
bool isConnected(int x) {
vector<bool> used(N);
vector<int> VEC;
VEC.PB(0); used[0] = 1;
int v = 0;
while(!VEC.empty()) {
vector<int> NEXT;
EACH(i, VEC) {
EACH(e, es[*i]) {
if(e->id != x && !used[e->to]) {
used[e->to] = 1;
NEXT.PB(e->to);
}
}
v ++;
}
VEC = NEXT;
}
return N == v;
}
int main() {
while( cin >> N >> M && (N|M) ) {
for(int i=0; i<MAX_E; i++) {
es[i].clear();
}
int base = 0;
int id = 0;
vector<int> cost;
for(int i=0; i<M; i++) {
int s, t, c;
cin >> s >> t >> c;
if(c <= 0) base += c;
else {
es[s].PB(Edge(t, c, id));
es[t].PB(Edge(s, c, id));
cost.PB(c);
id ++;
}
}
int E = id;
int ans = INF;
if(!isConnected(-1)) ans = 0;
for(int i=0; i<E; i++) {
if(!isConnected(i)) ans = min(ans, cost[i]);
}
for(int i=0; i<E-1; i++)
for(int j=i+1; j<E; j++)
ans = min(ans, cost[i]+cost[j]);
cout << base + ans << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
#define MAX 110
#define INF INT_MAX
typedef pair<int, int> pii;
struct Edge {
int from, to, cost;
Edge(int from, int to, int cost) :
from(from), to(to), cost(cost) {}
};
int N, M;
vector<Edge> G[MAX], cycle;
int color[MAX];
void dfs(int v, vector<Edge> u)
{
color[v] = 0;
for(int i = 0; i < (int)G[v].size(); i++) {
Edge e = G[v][i];
int to = e.to;
u.push_back(e);
if (color[to] == -1) {
dfs(to, u);
} else if(color[to] == 0) {
cycle = u;
throw 0;
}
u.pop_back();
}
color[v] = 1;
}
bool is_cycle_detect()
{
vector<Edge> vec;
memset(color, -1, sizeof(color));
for (int i = 0; i < N; i++) {
try {
if (color[i] == -1) {
dfs(i, vec);
}
} catch (...) {
break;
}
vec.clear();
}
return (cycle.size() > 0);
}
void init()
{
cycle.clear();
for (int i = 0; i < MAX; i++) {
G[i].clear();
}
}
class Union_Find {
public:
int par[MAX], rank[MAX], size[MAX], gnum;
Union_Find(int N) {
gnum = N;
for (int i = 0; i < N; i++) {
par[i] = i;
rank[i] = 0;
size[i] = 1;
}
}
int find(int x)
{
if (par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return;
if (rank[x] < rank[y]) {
par[x] = y;
size[y] += size[x];
} else {
par[y] = x;
size[x] += size[y];
if (rank[x] == rank[y]) {
rank[x]++;
}
}
gnum--;
}
bool same(int x, int y)
{
return (find(x) == find(y));
}
int getSize(int x)
{
return size[find(x)];
}
int groups()
{
return gnum;
}
};
int main()
{
int s, t, cost;
while (cin >> N >> M, N) {
int res = 0, min_cost = INF;
init();
Union_Find uf(N);
for (int i = 0; i < M; i++) {
cin >> s >> t >> cost;
if (cost > 0) {
G[s].push_back(Edge(s, t, cost));
uf.unite(s, t);
min_cost = min(min_cost, cost);
} else {
res += cost;
}
}
if (uf.gnum == 1) {
if (is_cycle_detect()) {
min_cost = INF;
set<pii> st;
vector<int> v;
for (int i = 0; i < (int)cycle.size(); i++) {
v.push_back(cycle[i].cost);
st.insert(pii(cycle[i].from, cycle[i].to));
}
sort(v.begin(), v.end());
min_cost = v[0] + v[1];
for (int i = 0; i < N; i++) {
for (int j = 0; j < (int)G[i].size(); j++) {
Edge &e = G[i][j];
if (st.count(pii(e.from, e.to)) == 0) {
min_cost = min(min_cost, e.cost);
}
}
}
}
} else {
min_cost = 0;
}
cout << res + min_cost << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Edge{
int src, dst, cap, rev;
Edge(int u, int v, int c, int r) :
src(u), dst(v), cap(c), rev(r) {}
};
typedef vector<Edge> Node;
typedef vector<Node> Graph;
void add_edge(Graph& G, int a, int b, int c){
G[a].push_back(Edge(a, b, c, G[b].size()));
G[b].push_back(Edge(b, a, c, G[a].size() - 1));
}
int dfs(Graph& G, int u, int t, int f, vector<bool>& used){
if(u == t) return f;
used[u] = true;
FORIT(e, G[u])if(e->cap > 0 && !used[e->dst]){
int g = dfs(G, e->dst, t, min(f, e->cap), used);
if(g > 0) {
e->cap -= g;
G[e->dst][e->rev].cap += g;
return g;
}
}
return 0;
}
int max_flow(Graph G, int s, int t){
int N = G.size();
int flow = 0;
while(true){
vector<bool> used(N, false);
int f = dfs(G, s, t, INF, used);
if(f == 0) return flow;
flow += f;
}
}
int main(){
int N, M;
while(cin >> N >> M && N){
Graph G(N);
int minus = 0;
REP(i, M){
int a, b, c;
cin >> a >> b >> c;
if(c <= 0){
minus += c;
continue;
}
add_edge(G, a, b, c);
}
int ans = INF;
FOR(t, 1, N){
ans = min(ans, max_flow(G, 0, t));
}
cout << ans + minus << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
typedef long long LL;
#define NN 120
#define INF 1LL << 60
LL vis[NN];
LL wet[NN];
LL combine[NN];
LL map[NN][NN];
LL S, T, minCut, N;
void Search(){
LL i, j, Max, tmp;
memset(vis, 0, sizeof(vis));
memset(wet, 0, sizeof(wet));
S = T = -1;
for (i = 0; i < N; i++){
Max = -INF;
for (j = 0; j < N; j++){
if (!combine[j] && !vis[j] && wet[j] > Max){
tmp = j;
Max = wet[j];
}
}
if (T == tmp) return;
S = T; T = tmp;
minCut = Max;
vis[tmp] = 1;
for (j = 0; j < N; j++){
if (!combine[j] && !vis[j]){
wet[j] += map[tmp][j];
}
}
}
}
LL Stoer_Wagner(){
LL i, j;
memset(combine, 0, sizeof(combine));
LL ans = INF;
for (i = 0; i < N - 1; i++){
Search();
if (minCut < ans) ans = minCut;
if (ans == 0) return 0;
combine[T] = 1;
for (j = 0; j < N; j++){
if (!combine[j]){
map[S][j] += map[T][j];
map[j][S] += map[j][T];
}
}
}
return ans;
}
int main(){
LL u, v, m, c;
while( scanf("%lld%lld", &N, &m) && (N+m) ){
memset( map, 0, sizeof( map ) );
LL ans = 0;
for(LL i=0 ; i < m ; ++i){
scanf("%lld%lld%lld", &u, &v, &c);
if( c < 0 ) ans += c;
else if( u != v ){
map[u][v] += c;
map[v][u] += c;
}
}
ans += Stoer_Wagner();
printf("%lld\n", ans);
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <complex>
#include <cmath>
#include <cstdio>
using namespace std;
#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])
#define INF (1ll<<60)
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long Weight;
struct Edge {
int src, dst;
Weight weight;
Edge(int src, int dst, Weight weight) :
src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
Weight minimumCut(const Graph &g) {
int n = g.size();
vector< vector<Weight> > h(n, vector<Weight>(n)); // make adj. matrix
REP(u,n) FOR(e,g[u]) h[e->src][e->dst] += e->weight;
vector<int> V(n); REP(u, n) V[u] = u;
Weight cut = INF;
for(int m = n; m > 1; m--) {
vector<Weight> ws(m, 0);
int u, v;
Weight w;
REP(k, m) {
u = v; v = max_element(ws.begin(), ws.end())-ws.begin();
w = ws[v]; ws[v] = -1;
REP(i, m) if (ws[i] >= 0) ws[i] += h[V[v]][V[i]];
}
REP(i, m) {
h[V[i]][V[u]] += h[V[i]][V[v]];
h[V[u]][V[i]] += h[V[v]][V[i]];
}
V.erase(V.begin()+v);
cut = min(cut, w);
}
return cut;
}
int main(){
int n,m;
while(cin >> n >> m && n){
Graph g(n);
long long ans = 0;
for(int i = 0 ; i < m ; i++){
int a,b,c;
cin >> a >> b >> c;
if( c <= 0 ) ans += c;
else g[a].push_back(Edge(a,b,c)),g[b].push_back(Edge(b,a,c));
}
cout << ans+minimumCut(g) << endl;
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
static const double EPS = 1e-8;
const int tx[] = {0,1,0,-1};
const int ty[] = {-1,0,1,0};
struct Node {
int to;
int idx;
int flow;
Node(int to,int idx,int flow) :
to(to),idx(idx),flow(flow) {}
};
class FordFulkerson {
private:
vector<Node>* _graph;
bool* _used;
int _size;
int dfs(int src,int flow,int sink){
if(src == sink) return flow;
_used[src] = true;
for(int i= 0;i < _graph[src].size();i++){
Node& e = _graph[src][i];
if(_used[e.to]) continue;
if(e.flow <= 0) continue;
int d = dfs(e.to,min(flow,e.flow),sink);
if(d <= 0) continue;
Node& rev_e = _graph[e.to][e.idx];
e.flow -= d;
rev_e.flow += d;
return d;
}
return 0;
}
public:
FordFulkerson(int n) {
_size = n;
_graph = new vector<Node>[_size];
_used = new bool[_size];
}
~FordFulkerson(){
delete[] _graph;
delete[] _used;
}
FordFulkerson(const FordFulkerson& f){
_size = f.size();
_graph = new vector<Node>[_size];
_used = new bool[_size];
fill((bool*)_used,(bool*)_used + _size,false);
for(int i = 0; i < f.size(); i++){
for(int j = 0; j < f.graph()[i].size(); j++){
_graph[i].push_back(f.graph()[i][j]);
}
}
}
void add_edge(int from,int to,int flow) {
_graph[from].push_back(Node(to,_graph[to].size(),flow));
_graph[to].push_back(Node(from,_graph[from].size()-1,flow));
}
int compute_maxflow(int src,int sink){
int res = 0;
while(true){
fill((bool*)_used,(bool*)_used + _size,false);
int tmp = dfs(src,numeric_limits<int>::max(),sink);
if(tmp == 0) break;
res += tmp;
}
return res;
}
int size() const{
return _size;
}
vector<Node>* graph() const{
return _graph;
}
};
int main(){
int num_of_houses;
int num_of_paths;
while(~scanf("%d %d",&num_of_houses,&num_of_paths)){
if(num_of_houses == 0 && num_of_paths == 0) break;
FordFulkerson ff(num_of_houses);
int bonus = 0;
for(int path_i = 0; path_i < num_of_paths; path_i++){
int from,to;
int cost;
scanf("%d %d %d",&from,&to,&cost);
if(cost < 0) bonus += cost;
ff.add_edge(from,to,max(0,cost));
}
int res = numeric_limits<int>::max();
for(int house_i = 1; house_i< num_of_houses; house_i++){
FordFulkerson tmp(ff);
res = min(res,tmp.compute_maxflow(0,house_i));
}
printf("%d\n",bonus + res);
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <vector>
#include <numeric>
#include <algorithm>
class unionfind {
private:
int size_;
std::vector<int> parent;
std::vector<int> rank;
public:
unionfind() : size_(0), parent(std::vector<int>()), rank(std::vector<int>()) {};
unionfind(int size__) : size_(size__), parent(std::vector<int>(size_)), rank(std::vector<int>(size_, 0)) {
std::iota(parent.begin(), parent.end(), 0);
}
int size() { return size_; }
int root(int x) {
if (parent[x] == x) return x;
return parent[x] = root(parent[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
void unite(int x, int y) {
x = root(x); y = root(y);
if (x == y) return;
if (rank[x] < rank[y]) parent[x] = y;
else if (rank[x] > rank[y]) parent[y] = x;
else parent[y] = x, rank[x]++;
}
};
#include <iostream>
using namespace std;
int n, m, x[109], y[109]; long long c[109]; bool used[109];
bool solve() {
unionfind uf(n);
for (int i = 0; i < m; i++) {
if (used[i]) uf.unite(x[i], y[i]);
}
int s = uf.root(0);
for (int i = 1; i < n; i++) {
if (s != uf.root(i)) return true;
}
return false;
}
int main() {
while (cin >> n >> m, n) {
int sum = 0;
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> c[i];
if (c[i] > 0) used[i] = true;
else used[i] = false, sum += c[i];
}
long long ret = 1LL << 60;
if (solve()) ret = sum;
for (int i = 0; i < m; i++) {
if (!used[i]) continue;
used[i] = false;
if (solve()) ret = min(ret, sum + c[i]);
used[i] = true;
}
for (int i = 0; i < m; i++) {
if (!used[i]) continue;
used[i] = false;
for (int j = i + 1; j < m; j++) {
if (!used[j]) continue;
used[j] = false;
if (solve()) ret = min(ret, sum + c[i] + c[j]);
used[j] = true;
}
used[i] = true;
}
cout << ret << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for ( int i = 0 ; i < (n); i++ )
#define All(v) v.begin(), v.end()
typedef pair<int, int> Pii; typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;
vector<Pii> G[101];
int min_bridge = INF;
int ord[101], low[101];
bool vis[101];
void dfs(int v, int p, int &k) {
vis[v] = true;
ord[v] = k++;
low[v] = ord[v];
Rep(i, G[v].size()) {
if ( !vis[G[v][i].fr] ) {
dfs(G[v][i].fr, v, k);
low[v] = min(low[v], low[G[v][i].fr]);
if ( ord[v] < low[G[v][i].fr] ) min_bridge = min(min_bridge, G[v][i].sc);
} else if ( G[v][i].fr != p ) {
low[v] = min(low[v], ord[G[v][i].fr]);
}
}
}
void dfs2(int v) {
if ( vis[v] ) return;
vis[v] = true;
Rep(i, G[v].size()) {
dfs2(G[v][i].fr);
}
}
signed main() {
int n, m;
while ( cin >> n >> m, n | m ) {
int minus_cost = 0;
min_bridge = INF;
int min_1 = INF, min_2 = INF;
Rep(i, 101) {
G[i].clear();
vis[i] = false;
}
Rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
if ( c <= 0 ) minus_cost += c;
else {
if ( min_1 > c ) min_2 = min_1, min_1 = c;
else if ( min_2 > c ) min_2 = c;
G[a].pb(Pii(b, c));
G[b].pb(Pii(a, c));
}
}
dfs2(0);
bool is_connected = true;
Rep(i, n) {
if ( !vis[i] ) {
is_connected = false;
break;
}
}
if ( !is_connected ) {
cout << minus_cost << endl;
continue;
}
Rep(i, n) vis[i] = false;
int k = 0;
Rep(i, n) {
if ( !vis[i] ) dfs(i, -1, k);
}
if ( min_bridge == INF ) cout << minus_cost << endl;
else cout << min(min_bridge, min_1+min_2) + minus_cost << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <queue>
using namespace std;
#define SZ(v) ((int)(v).size())
const int maxint = -1u>>1;
const int maxn = 100 + 30;
typedef long long ll;
int n, m;
ll adj[maxn][maxn];
int f[maxn], bestf[maxn];
int find(int v) {
if (v != bestf[v]) {
return bestf[v] = find(bestf[v]);
}
return v;
}
int bestff;
int v[maxn];
ll w[maxn];
bool a[maxn];
vector<pair<int, int> > res;
ll mincut() {
ll ans = (-1ull) >> 1;
//for (int i = 0; i < n; ++i) {
//for (int j = 0; j < n; ++j) {
//adj[i][j] = -adj[i][j];
//}
//}
for (int i = 0; i < n; ++i) {
f[i] = v[i] = i;
}
for (int t = n; t > 1; --t) {
a[0] = true;
for (int i = 1; i < t; ++i) {
a[i] =false;
w[i] = adj[v[0]][v[i]];
}
int prev = v[0];
for (int i = 1; i < t; ++i) {
int zj = -1;
for (int j = 1; j < t; ++j) {
if (a[j] == false && (zj == -1 || w[j] > w[zj])) {
zj = j;
}
}
a[zj] = true;
if (i == t - 1) {
if (w[zj] < ans) {
ans = w[zj];
copy(f, f + n, bestf);
bestff = v[zj];
}
for (int k = 0; k < t; ++k) {
adj[v[k]][prev] = adj[prev][v[k]] += adj[v[zj]][v[k]];
}
f[v[zj]] = prev;
v[zj] = v[t - 1];
break;
}
prev = v[zj];
for (int j = 1; j < t; ++j) {
if (a[j] == false) {
w[j] += adj[v[zj]][v[j]];
}
}
}
}
//bool flag[maxn] = {};
//for (int i = 0; i < n; ++i) {
//if (find(i) == bestff) {
//flag[i] = true;
//}
//}
//res.clear();
//for (int i = 0; i < n; ++i) {
//for (int j = i + 1; j < n; ++j) {
//if (flag[i] == flag[j] && save[i][j]) {
//res.push_back(make_pair(i, j));
//}
//}
//}
return ans;
}
void gao() {
ll ans = 0;
memset(adj, 0, sizeof(adj));
for (int i = 0, a, b; i < m; ++i) {
ll c;
scanf ("%d%d%lld", &a, &b, &c);
if (c < 0) {
ans += c;
} else {
adj[a][b] = adj[b][a] = adj[a][b] + c;
}
}
printf("%lld\n", ans + mincut());
}
int main() {
while (scanf("%d%d", &n, &m) == 2 && (n || m)) {
gao();
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<vector>
#include<cassert>
#include<queue>
using namespace std;
#define rep(i, n) for (int i = 0 ; i < int(n); ++i)
int main() {
while (true) {
long long n, m;
cin >> n >> m;
if (n == 0 && m == 0) break;
assert(n >= m);
long long sum = 0, mn = 1234567890123456789LL, mn1 = 1234567890321456789LL;
vector<pair<long long, pair<int, long long> > > edge[n];
rep (i, m) {
long long x, y, c;
cin >> x >> y >> c;
if (c <= 0) sum += c;
else {
if (c <= mn) {
mn1 = mn;
mn = c;
} else if (c < mn1) {
mn1 = c;
}
edge[x].push_back(make_pair(y, make_pair(i, c)));
edge[y].push_back(make_pair(x, make_pair(i, c)));
}
}
long long res = mn + mn1;
{
bool can[111][111] = {};
rep (i, n) {
queue<long long> pos;
pos.push(i);
while (!pos.empty()) {
long long now = pos.front();
pos.pop();
if (can[i][now]) continue;
can[i][now] = true;
rep (j, edge[now].size()) {
pos.push(edge[now][j].first);
}
}
}
rep (i, n) rep (j, n) if (!can[i][j] && !can[j][i]) {
cout << sum << endl;
goto next;
}
}
rep (iii, n) rep (jjj, edge[iii].size()) {
bool can[111][111] = {};
int num = edge[iii][jjj].second.first;
rep (i, n) {
queue<long long> pos;
pos.push(i);
while (!pos.empty()) {
long long now = pos.front();
pos.pop();
if (can[i][now]) continue;
can[i][now] = true;
rep (j, edge[now].size()) {
if (edge[now][j].second.first == num) continue;
pos.push(edge[now][j].first);
}
}
}
rep (i, n) rep (j, n) if (!can[i][j] && !can[j][i]) {
res = min(res, edge[iii][jjj].second.second);
}
}
cout << sum + res << endl;
next:;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>
//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else
//#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x
#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
//max flow (dinic)
template<class V> class MaxFlow_dinic{
public:
struct edge{int to,reve;V cap;};
static const int MV = 1100;
vector<edge> E[MV];
int itr[MV],lev[MV];
void add_edge(int x,int y,V cap,bool undir=false){
E[x].push_back((edge){y,(int)E[y].size(),cap});
E[y].push_back((edge){x,(int)E[x].size()-1,undir?cap:0});
}
void bfs(int cur){
memset(lev,0xff,sizeof(lev));
queue<int> q;
lev[cur]=0;
q.push(cur);
while(q.size()){
int v=q.front();
q.pop();
for(__typeof(E[v].begin()) e=E[v].begin();e!=E[v].end();e++) if(e->cap>0&&lev[e->to]<0) lev[e->to]=lev[v]+1,q.push(e->to);
}
}
V dfs(int from,int to,V cf){
if(from==to){
return cf;
}
for(;itr[from]<E[from].size();itr[from]++){
edge* e=&E[from][itr[from]];
if(e->cap>0&&lev[from]<lev[e->to]){
V f=dfs(e->to,to,min(cf,e->cap));
if(f>0){
e->cap-=f;
E[e->to][e->reve].cap += f;
return f;
}
}
}
return 0;
}
V maxflow(int from,int to){
V fl=0,tf;
while(1){
bfs(from);
if(lev[to]<0) return fl;
memset(itr,0,sizeof(itr));
while((tf=dfs(from,to,numeric_limits<V>::max()))>0) fl+=tf;
}
}
};
void mainmain(){
int n,m;
while(cin>>n>>m,n||m){
ll ans=INT_MAX;
vint a(m),b(m),c(m);
rep(i,m) cin>>a[i]>>b[i]>>c[i];
reep(i,1,n){
MaxFlow_dinic<ll> mf;
int tmp=0;
rep(j,m){
if(c[j]<0) tmp+=c[j];
else mf.add_edge(a[j],b[j],(ll)c[j],true);
}
ans=min(ans,tmp+mf.maxflow(0,i));
}
cout<<ans<<endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cout<<fixed<<setprecision(0);
mainmain();
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) {return (n==0)?1:10*TEN(n-1);}
int bsr(int x) { return 31 - __builtin_clz(x); }
template <class E>
using Graph = vector<vector<E>>;
// NOT VERIFY
template<class D, D INF>
struct BidirectedCut {
D sum = INF;
template<class E>
BidirectedCut(Graph<E> g) {
int n = (int)g.size();
int m_a = -1, m_b = -1;
vector<D> dist_base(n, 0);
for (int m = n; m > 1; m--) {
int a, b;
auto dist = dist_base;
for (int i = 0; i < m; i++) {
a = b; b = max_element(begin(dist), end(dist)) - begin(dist);
if (i == m-1) sum = min(sum, dist[b]);
dist[b] = -INF;
for (E &e: g[b]) {
if (e.to == m_b) e.to = m_a;
if (dist[e.to] == -INF) continue;
dist[e.to] += e.dist;
}
}
//merge a, b
m_a = a; m_b = b;
g[a].insert(end(g[a]), begin(g[b]), end(g[b]));
g[b].clear();
dist_base[b] = -INF;
}
}
};
/**
* Dinic?????????????????§???
*
* ?????¨???????????¬????????????
* template?????°???int V???????????°
* Edge??????rev?????????
* ---
* void add_edge(Graph<Edge> &g, int from, int to, int cap) {
* g[from].push_back(Edge{to, cap, (int)g[to].size()});
* g[to].push_back(Edge{from, 0, (int)g[from].size()-1});
* }
* ---
*/
template<class C, C INF> // Cap
struct MaxFlow {
int V;
vector<int> level, iter;
//g?????´?£????????????§??¨???
template<class E>
C exec(Graph<E> &g, int s, int t) {
V = (int)g.size();
level.resize(V);
iter.resize(V);
C flow = 0;
while (true) {
bfs(g, s);
if (level[t] < 0) return flow;
fill_n(iter.begin(), V, 0);
while (true) {
C f = dfs(g, s, t, INF);
if (!f) break;
flow += f;
}
}
}
template<class E>
void bfs(const Graph<E> &g, int s) {
fill_n(level.begin(), V, -1);
queue<int> que;
level[s] = 0;
que.push(s);
while (!que.empty()) {
int v = que.front(); que.pop();
for (E e: g[v]) {
if (e.cap <= 0) continue;
if (level[e.to] < 0) {
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
template<class E>
C dfs(Graph<E> &g, int v, int t, C f) {
if (v == t) return f;
C res = 0;
for (int &i = iter[v]; i < (int)g[v].size(); i++) {
E &e = g[v][i];
if (e.cap <= 0) continue;
if (level[v] < level[e.to]) {
C d = dfs(g, e.to, t, min(f, e.cap));
if (d <= 0) continue;
e.cap -= d;
g[e.to][e.rev].cap += d;
return d;
}
}
return res;
}
};
int rand_int(int l, int r) { //[l, r)
using D = uniform_int_distribution<int>;
static random_device rd;
static mt19937 gen(rd());
return D(l, r-1)(gen);
}
bool solve() {
int n, m;
cin >> n >> m;
if (!n) return false;
struct Edge {
int to;
ll dist;
};
Graph<Edge> g(n);
auto addEdge = [&](int a, int b, ll c) {
g[a].push_back(Edge{b, c});
g[b].push_back(Edge{a, c});
};
ll s = 0;
for (int i = 0; i < m; i++) {
int a, b; ll c;
cin >> a >> b >> c;
if (c < 0) {
s += c;
continue;
}
addEdge(a, b, c);
}
cout << s+BidirectedCut<ll, TEN(15)>(g).sum << endl;
return true;
}
int main() {
while (solve()) {}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cassert>
#include<climits>
#include<map>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define IINF (INT_MAX)
#define MAX 200
using namespace std;
struct Data{
int from,to,cost,index;
Data(int from=-1,int to=-1,int cost=-1,int index=-1):from(from),to(to),cost(cost),index(index){}
};
int V,E;
vector<Data> G[MAX];
bool visited[MAX];
void isconnect(int cur,int &counter,int *ban){
if(visited[cur])return;
visited[cur] = true;
counter++;
rep(i,G[cur].size()){
if( ban[0] == G[cur][i].index || ban[1] == G[cur][i].index ) continue;
isconnect(G[cur][i].to,counter,ban);
}
}
int main(){
while(cin >> V >> E,V|E){
Data *input = new Data[E];
int mincost = IINF,coef = 0;
rep(i,V)G[i].clear();
rep(i,E){
cin >> input[i].from >> input[i].to >> input[i].cost;
input[i].index = i;
if( input[i].cost < 0 ) coef += input[i].cost;
else {
G[input[i].from].push_back(input[i]);
G[input[i].to].push_back(Data(input[i].to,input[i].from,input[i].cost,input[i].index));
}
}
int counter = 0;
int ban[2] = {-1,-1};
rep(i,V)visited[i] = false;
isconnect(0,counter,ban);
if(counter != V)mincost = min(mincost,coef);
rep(i,E){
if( input[i].cost < 0 ) continue;
rep(j,E){
if( input[j].cost < 0 ) continue;
rep(k,V)visited[k] = false;
ban[0] = i, ban[1] = j;
counter = 0;
isconnect(0,counter,ban);
if( counter != V )mincost = min(mincost,coef+input[i].cost+((i==j)?0:input[j].cost));
}
}
cout << mincost << endl;
delete [] input;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;
#define typec long long
#define V 111
typec g[V][V], w[V];
typec a[V], v[V], na[V];
typec Stoer_Wagner(int n)
{
typec i, j, pv, zj,I=1;
typec best = I<<32;
for (i = 0; i < n; i++) v[i] = i; // vertex: 0 ~ n-1
while (n > 1)
{
for (a[v[0]] = 1, i = 1; i < n; i++)
{
a[v[i]] = 0;
na[i - 1] = i;
w[i] = g[v[0]][v[i]];
}
for (pv = v[0], i = 1; i < n; i++ )
{
for (zj = -1, j = 1; j < n; j++ )
if (!a[v[j]] && (zj < 0 || w[j] > w[zj]))
zj = j;
a[v[zj]] = 1;
if (i == n - 1)
{
if (best > w[zj]) best = w[zj];
for (i = 0; i < n; i++)
g[v[i]][pv] = g[pv][v[i]] += g[v[zj]][v[i]];
v[zj] = v[--n];
break;
}
pv = v[zj];
for (j = 1; j < n; j++)
if(!a[v[j]])
w[j] += g[v[zj]][v[j]];
}
}
return best;
}
bool vis[111];
void dfs(int u,int n)
{
for(int v=0;v<n;++v)
if(g[u][v]!=0&&vis[v]==0)
{
vis[v]=1;
dfs(v,n);
}
}
bool ok(int n)
{
for(int i=0;i<n;++i)
if(vis[i]==0)return 1;
return 0;
}
int main()
{
//freopen("G:\\in.txt","r",stdin);
int n, m, u, v, w;
while(scanf("%d%d", &n, &m),n+m)
{
memset(g, 0, sizeof(g));
typec ans=0;
while(m --)
{
scanf("%d%d%d", &u, &v, &w);
if(w<=0)ans+=w;
else g[v][u]= g[u][v] = w;
}
// memset(vis,0,sizeof(vis));
//dfs(0,n);
//if(ok(n))puts("0");
printf("%lld\n", ans+Stoer_Wagner(n));
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
using Int = long long;
struct SCC{
int n;
vector<vector<int> > G,rG,T,C;
vector<int> vs,used,belong;
SCC(){};
SCC(int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){}
void add_edge(int from,int to){
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v){
used[v] = 1;
for(int i=0;i<(int)G[v].size();i++){
if(!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k){
used[v] = 1;
belong[v] = k;
C[k].push_back(v);
for(int i=0;i<(int)rG[v].size();i++) {
if(!used[rG[v][i]]) rdfs(rG[v][i],k);
}
}
int build(){
fill(used.begin(),used.end(),0);
vs.clear();
for(int v=0;v<n;v++){
if(!used[v]) dfs(v);
}
fill(used.begin(),used.end(),0);
int k=0;
for(int i=vs.size()-1;i>=0;i--){
if(!used[vs[i]]){
T.push_back(vector<int>());
C.push_back(vector<int>());
rdfs(vs[i],k++);
}
}
return k;
}
};
struct UnionFind{
int n;
vector<int> r,p;
UnionFind(int n):n(n),r(n,1),p(n){iota(p.begin(),p.end(),0);}
int find(int x){
if(x==p[x]) return x;
return p[x]=find(p[x]);
}
void unite(int x,int y){
x=find(x);y=find(y);
if(x==y) return;
if(r[x]<r[y]) swap(x,y);
r[x]+=r[y];
p[y]=x;
}
};
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,m;
while(cin>>n>>m,n){
vector<int> x(m),y(m),c(m);
int ans=0;
UnionFind uf(n);
SCC scc(n);
for(int i=0;i<m;i++){
cin>>x[i]>>y[i]>>c[i];
if(c[i]<=0) ans+=c[i];
else{
scc.add_edge(x[i],y[i]);
uf.unite(x[i],y[i]);
}
}
if(uf.r[uf.find(0)]!=n){
cout<<ans<<endl;
continue;
}
scc.build();
//cout<<ans<<endl;
auto b=scc.belong;
vector<int> v;
int t=-1;
for(int i=0;i<m;i++){
if(c[i]<=0) continue;
if(b[x[i]]==b[y[i]]){
v.emplace_back(c[i]);
continue;
}
if(t<0||c[i]<t) t=c[i];
}
sort(v.begin(),v.end());
if(!v.empty()&&(t<0||v[0]+v[1]<t)) t=v[0]+v[1];
cout<<ans+t<<endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
using namespace std;
#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int popcount(T n) { return n ? 1 + popcount(n & (n - 1)) : 0; }
#endif
#ifdef __GNUC__
template <class T> int popcount(T n);
template <> int popcount(unsigned int n) { return n ? __builtin_popcount(n) : 0; }
template <> int popcount(int n) { return n ? __builtin_popcount(n) : 0; }
template <> int popcount(unsigned long long n) { return n ? __builtin_popcountll(n) : 0; }
template <> int popcount(long long n) { return n ? __builtin_popcountll(n) : 0; }
#endif
#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CL(arr, val) memset(arr, val, sizeof(arr))
#define rep(i, n) for (int i = 0; i < n; ++i)
template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }
typedef long long ll;
typedef pair<int, int> pint;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }
template <class T> void print(T a, int n, int br = 1, const string& deli = ", ") { cout << "{ "; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << " }"; while (br--) cout << endl; }
template <class T> void print(const vector<T>& v, int br = 1, const string& deli = ", ") { print(v, v.size(), br, deli); }
template <class T>
class UnionFind
{
private:
T* parent;
T* rank;
public:
int groups;
UnionFind(int n)
: groups(n)
{
parent = new T[n];
rank = new T[n];
for (int i = 0; i < n; ++i)
{
parent[i] = i;
rank[i] = 0;
}
}
void unite(T a, T b)
{
T root_a = find(a);
T root_b = find(b);
if (root_a == root_b)
return;
--groups;
if (rank[root_a] < rank[root_b])
parent[root_a] = root_b;
else if (rank[root_a] > rank[root_b])
parent[root_b] = root_a;
else
{
parent[root_a] = root_b;
++rank[root_a];
}
}
bool same(T a, T b)
{
return find(a) == find(b);
}
T find(T e)
{
if (parent[e] == e)
return e;
else
return parent[e] = find(parent[e]);
}
};
class SCC
{
public:
int V;
vector<vector<int> > g;
vector<vector<int> > rg;
vector<int> vs; // 帰りの順番, 後ろのほう要素は始点側
vector<bool> used;
vector<int> component;
SCC(int V)
: V(V), g(V), rg(V), vs(V), used(V), component(V) { }
void add_edge(int from, int to)
{
g[from].push_back(to);
rg[to].push_back(from);
}
void dfs(int v)
{
used[v] = true;
for (int i = 0; i < g[v].size(); ++i)
if (!used[g[v][i]])
dfs(g[v][i]);
vs.push_back(v);
}
void rdfs(int v, int k)
{
used[v] = true;
component[v] = k;
for (int i = 0; i < rg[v].size(); ++i)
if (!used[rg[v][i]])
rdfs(rg[v][i], k);
}
// 強連結成分の数を返す
// O(V + E)
// グラフの始点側の値(component[v])は小さい
// 末端は大きい値, 最末端(component[v] == scc() - 1)
int scc()
{
vs.clear();
fill(used.begin(), used.end(), false);
for (int i = 0; i < V; ++i)
if (!used[i])
dfs(i);
fill(used.begin(), used.end(), false);
int k = 0;
for (int i = vs.size() - 1; i >= 0; --i)
if (!used[vs[i]])
rdfs(vs[i], k++);
return k;
}
};
int main()
{
ios::sync_with_stdio(false);
int n, m;
while (cin >> n >> m, n)
{
vector<pint> e[128]; // (to, cost)
UnionFind<int> uf(n);
int res = 0;
while (m--)
{
int x, y, c;
cin >> x >> y >> c;
if (c <= 0)
res += c;
else
{
e[x].push_back(pint(y, c));
uf.unite(x, y);
}
}
if (uf.groups == 1)
{
SCC scc(n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < e[i].size(); ++j)
scc.add_edge(i, e[i][j].first);
scc.scc();
int min_cost = INT_MAX;
map<int, vector<int> > cost_in_scc;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < e[i].size(); ++j)
{
int x = i, y = e[i][j].first, c = e[i][j].second;
if (scc.component[x] != scc.component[y])
min_swap(min_cost, c);
else
cost_in_scc[scc.component[x]].push_back(c);
}
}
foreach (it, cost_in_scc)
{
vector<int>& v = it->second;
sort(all(v));
min_swap(min_cost, v[0] + v[1]);
}
res += min_cost;
}
cout << res << endl;
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int D_MAX_V=210;
const int D_v_size=210;
struct D_wolf{
int t,r;
long long c;
D_wolf(){t=c=r=0;}
D_wolf(int t1,long long c1,int r1){
t=t1;c=c1;r=r1;
}
};
vector<D_wolf>D_G[D_MAX_V];
int D_level[D_MAX_V];
int D_iter[D_MAX_V];
void add_edge(int from,int to,long long cap){
D_G[from].push_back(D_wolf(to,cap,D_G[to].size()));
D_G[to].push_back(D_wolf(from,0,D_G[from].size()-1));
}
void D_bfs(int s){
for(int i=0;i<D_v_size;i++)D_level[i]=-1;
queue<int> Q;
D_level[s]=0;
Q.push(s);
while(Q.size()){
int v=Q.front();
Q.pop();
for(int i=0;i<D_G[v].size();i++){
if(D_G[v][i].c>0&&D_level[D_G[v][i].t]<0){
D_level[D_G[v][i].t]=D_level[v]+1;
Q.push(D_G[v][i].t);
}
}
}
}
long long D_dfs(int v,int t,long long f){
if(v==t)return f;
for(;D_iter[v]<D_G[v].size();D_iter[v]++){
int i=D_iter[v];
if(D_G[v][i].c>0&&D_level[v]<D_level[D_G[v][i].t]){
long long d=D_dfs(D_G[v][i].t,t,min(f,D_G[v][i].c));
if(d>0){
D_G[v][i].c-=d;
D_G[D_G[v][i].t][D_G[v][i].r].c+=d;
return d;
}
}
}
return 0;
}
long long max_flow(int s,int t){
long long flow=0;
for(;;){
D_bfs(s);
if(D_level[t]<0)return flow;
for(int i=0;i<D_v_size;i++)D_iter[i]=0;
long long f;
while((f=D_dfs(s,t,9999999999999LL))>0){flow+=f;}
}
return 0;
}
long long INF=9999999999999LL;
int p[30000];
int q[30000];
int r[30000];
int main(){
int a,b;
while(scanf("%d%d",&a,&b),a){
for(int i=0;i<b;i++){
scanf("%d%d%d",p+i,q+i,r+i);
}
long long ret=999999999999999LL;
for(int i=1;i<a;i++){
for(int j=0;j<D_MAX_V;j++){
D_G[j].clear();
D_iter[j]=D_level[j]=0;
}
long long tmp=0;
/* for(int j=0;j<a;j++){
add_edge(j,j+a,INF);
if(j==0)add_edge(s,j,INF);
else add_edge(s,j,0);
if(j==i)add_edge(j+a,t,INF);
else add_edge(j+a,t,0);
}*/
for(int j=0;j<b;j++){
if(r[j]<=0){
tmp+=r[j];continue;
}
add_edge(p[j],q[j],r[j]);
add_edge(q[j],p[j],r[j]);
}
ret=min(ret,tmp+max_flow(0,i));
}
printf("%lld\n",ret);
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f ;
const int maxw = 1000 ;
int g[105][105] , w[105] ;
int a[105] , v[105] , na[105] ;
int n , m ;
int mincut( int n ) {
int i , j, pv , zj ;
int best = inf ;
for( i = 0 ; i < n; i ++ ) {
v[i] = i ;
}
while( n > 1 ) {
for( a[v[0]] = 1 , i = 1 ; i < n; i ++ ) {
a[v[i]] = 0 ; na[i-1] = i ;
w[i] = g[v[0]][v[i]] ;
}
for( pv = v[0] ,i = 1 ; i < n ; i ++ ) {
for( zj = -1 , j = 1 ; j < n ; j ++ )
if( !a[v[j]] && (zj<0||w[j]>w[zj]))
zj = j ;
a[v[zj]] = 1 ;
if( i == n - 1 ) {
if( best > w[zj] ) best = w[zj] ;
for( i = 0 ; i < n; i++ ) {
g[v[i]][pv] = g[pv][v[i]] +=g[v[zj]][v[i]] ;
}
v[zj] = v[--n] ;
break;
}
pv = v[zj] ;
for( j = 1 ; j < n; j ++ ) if( !a[v[j]])
w[j] += g[v[zj]][v[j]] ;
}
}
return best ;
}
int main(){
while( scanf( "%d%d" , &n , &m ) != EOF ) {
if( n == 0 && m == 0 ) break;
memset( g , 0 , sizeof(g) ) ;
int ans = 0 ;
int i ;
for( i = 1 ; i <= m ;i ++ ) {
int from , to ,val ;
scanf( "%d%d%d" , &from , &to , &val ) ;
if( val < 0 ) {
ans += val ;
continue ;
}
if( g[from][to] == inf ) {
g[from][to] = val ;
}else{
g[from][to] += val ;
}
g[to][from] = g[from][to] ;
}
printf( "%d\n" , ans + mincut(n) ) ;
}
return 0 ;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
static int N, M, E;
static int[] from;
static long[] cost;
static boolean disjoint() {
UnionFind uf = new UnionFind(N);
for (int i = 0; i < N; ++i) {
if (from[i] != -1) uf.union(i, from[i]);
}
return uf.size(0) != N;
}
static long solve() {
if (E <= N - 2) return 0;
if (disjoint()) return 0;
long ret = Long.MAX_VALUE;
for (int i = 0; i < N; ++i) {
if (from[i] == -1) continue;
for (int j = i + 1; j < N; ++j) {
if (from[j] == -1) continue;
ret = Math.min(ret, cost[i] + cost[j]);
}
}
for (int i = 0; i < N; ++i) {
if (from[i] == -1) continue;
int tmp = from[i];
from[i] = -1;
if (disjoint()) ret = Math.min(ret, cost[i]);
from[i] = tmp;
}
return ret;
}
public static void main(String[] args) throws Exception {
while (true) {
N = sc.nextInt();
M = sc.nextInt();
if (N == 0) break;
from = new int[N];
cost = new long[N];
Arrays.fill(from, -1);
long minus = 0;
E = 0;
for (int i = 0; i < M; ++i) {
int X = sc.nextInt();
int Y = sc.nextInt();
long C = sc.nextLong();
if (C <= 0) {
minus += C;
} else {
from[Y] = X;
cost[Y] = C;
++E;
}
}
System.out.println(solve() + minus);
}
}
static class UnionFind {
int[] set;
UnionFind(int n) {
set = new int[n];
Arrays.fill(set, -1);
}
void union(int a, int b) {
int rtA = root(a);
int rtb = root(b);
if (rtA == rtb) {
return;
}
set[rtA] += set[rtb];
set[rtb] = rtA;
}
boolean find(int a, int b) {
return root(a) == root(b);
}
int root(int a) {
if (set[a] < 0) {
return a;
} else {
set[a] = root(set[a]);
return set[a];
}
}
int size(int a) {
return -set[root(a)];
}
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | python3 | # AOJ 1065 The House of Huge Family
# Python3 2018.7.10 bal4u
# UNION-FIND library
class UnionSet:
def __init__(self, nmax):
self.size = [1]*nmax
self.id = [i for i in range(nmax+1)]
def root(self, i):
while i != self.id[i]:
self.id[i] = self.id[self.id[i]]
i = self.id[i]
return i
def connected(self, p, q): return self.root(p) == self.root(q)
def unite(self, p, q):
i, j = self.root(p), self.root(q)
if i == j: return
if self.size[i] < self.size[j]:
self.id[i] = j
self.size[j] += self.size[i]
else:
self.id[j] = i
self.size[i] += self.size[j]
# UNION-FIND library
while True:
n, m = map(int, input().split())
if n == 0: break
u = UnionSet(n)
ans, tbl = 0, []
for i in range(m):
x, y, c = map(int, input().split())
if c < 0: ans += c
else: tbl.append((c, x, y))
tbl.sort(reverse=True)
for c, x, y in tbl:
if not u.connected(x, y):
if n > 2:
n -= 1
u.unite(x, y)
else: ans += c
print(ans)
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<stdio.h>
#include<algorithm>
#include<vector>
struct S{int f,t,c;}s;
int C(S l,S r){return l.c>r.c;}
int u[100],r[100];
int F(int x)
{
return (u[x]-x?u[x]=F(u[x]):x);
}
void U(int x,int y)
{
x=F(x),y=F(y);
if(r[x]>r[y])u[y]=x;
else if(r[x]<r[y])u[x]=y;
else u[x]=y,++r[y];
}
int main()
{
int n,m,i,a;
while(scanf("%d%d",&n,&m),n)
{
std::vector<S>v;
for(a=i=0;i<n;++i)r[u[i]=i]=1;
while(m--)
{
scanf("%d%d%d",&s.f,&s.t,&s.c);
if(s.c>0)v.push_back(s);
else a+=s.c;
}
std::sort(v.begin(),v.end(),C);
for(i=0;i<v.size();++i)
{
s=v[i];
if(F(s.f)!=F(s.t))
{
if(n>2)U(s.f,s.t),--n;
else a+=s.c;
}
}
printf("%d\n",a);
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<queue>
#include<cstdio>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int INF=1<<29;
template<class T>
struct AdjMatrix:public vector< vector<T> >{
AdjMatrix(int n,const vector<T> &v=vector<T>()):vector< vector<T> >(n,v){}
};
template<class T>
T augment(const AdjMatrix<T> &capa,int src,int snk,AdjMatrix<T> &flow){
int n=capa.size();
vector<int> parent(n,-1); parent[src]=-2;
T water=0;
queue< pair<T,int> > qu; qu.push(make_pair(INF,src));
while(!qu.empty()){
pair<T,int> a=qu.front(); qu.pop();
int u=a.second;
T w=a.first;
if(u==snk){ water=w; break; }
rep(v,n) if(parent[v]==-1 && capa[u][v]-flow[u][v]>0) {
qu.push(make_pair(min(w,capa[u][v]-flow[u][v]),v));
parent[v]=u;
}
}
if(water==0) return 0;
for(int v=snk,u=parent[snk];v!=src;v=u,u=parent[u]){
flow[u][v]+=water;
flow[v][u]-=water;
}
return water;
}
template<class T>
T FordFulkerson(const AdjMatrix<T> &capa,int src,int snk){
int n=capa.size();
AdjMatrix<T> flow(n,vector<T>(n));
T ans=0;
while(1){
T water=augment(capa,src,snk,flow);
if(water==0) break;
ans+=water;
}
return ans;
}
int main(){
for(int n,m;scanf("%d%d",&n,&m),n;){
int ans=0;
AdjMatrix<int> adj(n,vector<int>(n));
rep(i,m){
int u,v,w; scanf("%d%d%d",&u,&v,&w);
if(w<0) ans+=w;
adj[u][v]=adj[v][u]=max(w,0);
}
int ans2=INF;
rep(v,n) if(v!=0) ans2=min(ans2,FordFulkerson(adj,0,v));
printf("%d\n",ans+ans2);
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//The House of Huge Family
public class Main{
class E{
int s, t, c, id;
public E(int s, int t, int c, int id) {
this.s = s;
this.t = t;
this.c = c;
this.id = id;
}
}
int n, m;
List<E>[] adj;
boolean isCon(int f1, int f2){
boolean[] u = new boolean[n];
List<Integer> l = new ArrayList<Integer>();
l.add(0); u[0] = true;
int N = 0;
while(!l.isEmpty()){
List<Integer> next = new ArrayList<Integer>();
for(int i:l){
N++;
for(E e:adj[i])if(!u[e.t]&&e.id!=f1&&e.id!=f2){
u[e.t] = true; next.add(e.t);
}
}
l = next;
}
return N==n;
}
@SuppressWarnings("unchecked")
void run(){
Scanner sc = new Scanner(System.in);
for(;;){
n = sc.nextInt(); m = sc.nextInt();
if((n|m)==0)break;
adj = new List[n];
for(int i=0;i<n;i++)adj[i]=new ArrayList<E>();
int N = 0, M = 0;
List<Integer> cost = new ArrayList<Integer>();
while(m--!=0){
int s = sc.nextInt(), t = sc.nextInt(), c = sc.nextInt();
if(c<=0)M+=c;
else{
cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++;
}
}
int min = Integer.MAX_VALUE;
if(!isCon(-1, -1))min = 0;
for(int i=0;i<N;i++)if(!isCon(i, -1))min = Math.min(min, cost.get(i));
for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)min = Math.min(min, cost.get(i)+cost.get(j));
System.out.println(M+min);
}
}
public static void main(String[] args) {
new Main().run();
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//The House of Huge Family
public class Main{
class E{
int s, t, c, id;
public E(int s, int t, int c, int id) {
this.s = s;
this.t = t;
this.c = c;
this.id = id;
}
}
int n, m;
List<E>[] adj;
boolean isCon(int f1, int f2){
boolean[] u = new boolean[n];
List<Integer> l = new ArrayList<Integer>();
l.add(0); u[0] = true;
int N = 0;
while(!l.isEmpty()){
List<Integer> next = new ArrayList<Integer>();
for(int i:l){
N++;
for(E e:adj[i])if(!u[e.t]&&e.id!=f1&&e.id!=f2){
u[e.t] = true; next.add(e.t);
}
}
l = next;
}
return N==n;
}
@SuppressWarnings("unchecked")
void run(){
Scanner sc = new Scanner(System.in);
for(;;){
n = sc.nextInt(); m = sc.nextInt();
if((n|m)==0)break;
adj = new List[n];
for(int i=0;i<n;i++)adj[i]=new ArrayList<E>();
int N = 0, M = 0;
List<Integer> cost = new ArrayList<Integer>();
while(m--!=0){
int s = sc.nextInt(), t = sc.nextInt(), c = sc.nextInt();
if(c<=0)M+=c;
else{
cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++;
}
}
int min = Integer.MAX_VALUE;
if(!isCon(-1, -1))min = 0;
for(int i=0;i<N;i++)if(!isCon(i, -1))min = Math.min(min, cost.get(i));
for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)if(!isCon(i, j))min = Math.min(min, cost.get(i)+cost.get(j));
System.out.println(M+min);
}
}
public static void main(String[] args) {
new Main().run();
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
static const double EPS = 1e-8;
const int tx[] = {0,1,0,-1};
const int ty[] = {-1,0,1,0};
struct Node {
int to;
int idx;
int flow;
Node(int to,int idx,int flow) :
to(to),idx(idx),flow(flow) {}
};
class FordFulkerson {
private:
vector<Node>* _graph;
bool* _used;
int _size;
int dfs(int src,int flow,int sink){
if(src == sink) return flow;
_used[src] = true;
for(int i= 0;i < _graph[src].size();i++){
Node& e = _graph[src][i];
if(_used[e.to]) continue;
if(e.flow <= 0) continue;
int next_flow = min(flow,e.flow);
int d = dfs(e.to,next_flow,sink);
if(d <= 0) continue;
Node& rev_e = _graph[e.to][e.idx];
e.flow -= d;
rev_e.flow += d;
return d;
}
return 0;
}
public:
FordFulkerson(int n) {
_size = n;
_graph = new vector<Node>[_size];
_used = new bool[_size];
}
FordFulkerson(const FordFulkerson& f){
_size = f.size();
_graph = new vector<Node>[_size];
_used = new bool[_size];
fill((bool*)_used,(bool*)_used + _size,false);
for(int i = 0; i < f.size(); i++){
for(int j = 0; j < f.graph()[i].size(); j++){
_graph[i].push_back(f.graph()[i][j]);
}
}
}
void add_edge(int from,int to,int flow) {
_graph[from].push_back(Node(to,_graph[to].size(),flow));
_graph[to].push_back(Node(from,_graph[from].size()-1,flow));
}
int compute_maxflow(int src,int sink){
int res = 0;
while(true){
fill((bool*)_used,(bool*)_used + _size,false);
int tmp = dfs(src,numeric_limits<int>::max(),sink);
if(tmp == 0) break;
res += tmp;
}
return res;
}
int size() const{
return _size;
}
vector<Node>* graph() const{
return _graph;
}
};
class UnionFindTree {
private:
int* par;
int* rank;
public:
UnionFindTree(int n){
par = new int[n]();
rank = new int[n]();
for(int i=0;i<n;i++){
par[i] = i;
rank[i] = 0;
}
}
~UnionFindTree(){
delete[] rank;
delete[] par;
}
int find(int x){
if(par[x] == x){
return x;
} else {
return par[x] = find(par[x]);
}
}
void unite(int x,int y){
x = find(x);
y = find(y);
if (x == y) return;
par[x] = y;
}
bool same(int x,int y){
return find(x) == find(y);
}
};
int main(){
int num_of_houses;
int num_of_paths;
while(~scanf("%d %d",&num_of_houses,&num_of_paths)){
if(num_of_houses == 0 && num_of_paths == 0) break;
FordFulkerson ff(num_of_houses);
UnionFindTree uft(num_of_houses);
int bonus = 0;
for(int path_i = 0; path_i < num_of_paths; path_i++){
int from,to;
int cost;
scanf("%d %d %d",&from,&to,&cost);
if(cost < 0) bonus += cost;
ff.add_edge(from,to,max(0,cost));
uft.unite(from,to);
}
int tree_count = 0;
bool visited[101];
memset(visited,false,sizeof(visited));
for(int house_i = 0; house_i< num_of_houses; house_i++){
if(!visited[uft.find(house_i)]) tree_count++;
visited[uft.find(house_i)] = true;
}
int res = numeric_limits<int>::max();
if(tree_count >= 2){
res = 0;
}
else{
for(int house_i = 0; house_i< num_of_houses; house_i++){
int house_j = uft.find(house_i);
if(house_i == house_j) continue;
FordFulkerson tmp(ff);
res = min(res,tmp.compute_maxflow(house_i,house_j));
}
}
printf("%d\n",bonus + res);
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | // I'm the Topcoder
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;
//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif
//**************************CONSTANT***********************
#define INF 0x7F7F7F7F
#define eps 1e-8
#define PI acos(-1.)
#define PI2 asin (1.);
typedef long long LL;
//typedef __int64 LL; //codeforces
typedef unsigned int ui;
typedef unsigned long long ui64;
#define MP make_pair
typedef vector<int> VI;
typedef pair<int, int> PII;
#define pb push_back
#define mp make_pair
//***************************SENTENCE************************
#define CL(a,b) memset (a, b, sizeof (a))
#define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b))
//****************************FUNCTION************************
template <typename T> double DIS(T va, T vb) { return sqr(va.x - vb.x, va.y - vb.y); }
template <class T> inline T INTEGER_LEN(T v) { int len = 1; while (v /= 10) ++len; return len; }
template <typename T> inline T square(T va, T vb) { return va * va + vb * vb; }
// aply for the memory of the stack
#pragma comment (linker, "/STACK:1024000000,1024000000")
//end
#define CY 105
typedef int typec;
bool inset[CY], vis[CY];
typec wage[CY];
int g[CY][CY];
int N, M;
typec Search(int n, int &s, int &t) {
memset(vis, false, n * sizeof(vis[0]));
memset(wage, 0, n * sizeof(wage[0]));
s = t = -1;
typec cf, mx;
int u = 0;
for (int i = 0; i < n; ++i) {
mx = -INF;
for (int j = 0; j < n; ++j) {
if (!inset[j] && !vis[j] && mx < wage[j]) {
mx = wage[u = j];
}
}
if (u == t) return cf;
s = t; t = u;
cf = mx;
vis[u] = true;
for (int v = 0; v < n; ++v) {
if (!inset[v] && !vis[v]) {
wage[v] += g[u][v];
}
}
}
return cf;
}
typec Stoer_Wagner(int n) {
int s, t;
typec ans = INF, cf;
memset(inset, false, n * sizeof(inset[0]));
for (int i = 1; i < n; ++i) {
cf = Search(n, s, t);
ans = min(ans, cf);
if (ans == 0) return ans;
inset[t] = true;
for (int j = 0; j < n; ++j) {
if (!inset[j] && j != s) {
g[s][j] += g[t][j];
g[j][s] += g[j][t];
}
}
}
return ans;
}
int main(void) {
while (2 == scanf("%d%d", &N, &M)) {
if (N == 0 && M == 0) break;
memset(g, 0, sizeof(g));
int ans = 0, u, v, c;
for (int i = 0; i < M; ++i) {
scanf("%d%d%d", &u, &v, &c);
if (c > 0) g[u][v] += c, g[v][u] += c;
else ans += c;
}
printf("%d\n", ans + Stoer_Wagner(N));
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <stdio.h>
#include <string.h>
#define MaxN 110
const int oo = 0x7fffffff;
int g[MaxN][MaxN], d[MaxN];
bool used[MaxN], vst[MaxN];
int main()
{
int min, n, m, max, tmp, pre, cur, a, b, w, cnt;
while (scanf("%d%d", &n, &m), n) {
min = oo, cnt = 0;
memset(g, 0, sizeof(g));
memset(used, 0, sizeof(used));
int Res = 0;
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &a, &b, &w);
if (w < 0){
Res += w;
continue;
}
g[a][b] += w;
g[b][a] = g[a][b];
}
if (n < 2) {
printf("%d\n",Res);
continue;
}
for (int i = 1; i < n; ++i) {
memset(vst, 0, sizeof(vst));
vst[0] = 1;
cur = pre = d[0] = 0;
for (int k = 1; k < n; ++k) d[k] = g[0][k];
for (int j = i; j < n; ++j) {
max = 0;
pre = cur;
for (int k = 0; k < n; ++k) {
if (vst[k] || used[k]) continue;
if (d[k] > max) {
max = d[k];
cur = k;
}
}
vst[cur] = 1;
for (int k = 0; k < n; ++k) {
if (vst[k] || used[k]) continue;
d[k] += g[cur][k];
}
}
used[cur] = 1;
for (int j = 0; j < n; ++j) {
if (j == pre || used[j]) continue;
g[pre][j] += g[cur][j];
g[j][pre] = g[pre][j];
}
tmp = 0;
for (int j = 0; j < n; ++j) {
if (used[j]) continue;
tmp += g[cur][j];
}
if (tmp < min) min = tmp;
else continue;
}
printf("%d\n", Res+min);
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <complex>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <sstream>
#include <numeric>
#include <functional>
#include <bitset>
#include <iomanip>
using namespace std;
#define INF (1<<29)
// math
#define Sq(x) ((x)*(x))
// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)
// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long ll;
// conversion
template<class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }
// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }
// !!! set MAX_L number !!!
#define MAX_L (1000001)
#define MAX_SQRT_B ((int)sqrt(INT_MAX)+1)
bool is_prime[MAX_L];
vector<bool> is_prime_small(MAX_SQRT_B);
void segment_sieve(ll a, ll b) {
for(int i=0; (ll)i*i < b; i++) is_prime_small[i] = true;
for(int i=0; i < b-a; i++) is_prime[i] = true;
if(a == 1) {
is_prime[0] = false;
}
is_prime_small[0] = is_prime_small[1] = false;
for(int i=2; (ll)i*i<b; i++) {
if(is_prime_small[i]) {
for(int j=2*i; (ll)j*j<b; j+=i) is_prime_small[j] = false;
for(ll j = max(2LL, (a+i-1)/i)*i; j<b; j+=i) is_prime[j-a] = false;
}
}
}
//////////////////////////////////////////////////////////////
struct Edge {
int to, cost, id;
Edge(int t, int c, int i)
: to(t), cost(c), id(i) {}
};
#define MAX_E (100)
vector<Edge> es[MAX_E];
int N, M;
bool isConnected(int x) {
vector<bool> used(N);
vector<int> VEC;
VEC.PB(0); used[0] = 1;
int v = 0;
while(!VEC.empty()) {
vector<int> NEXT;
EACH(i, VEC) {
EACH(e, es[*i]) {
if(e->id != x && !used[e->to]) {
used[e->to] = 1;
NEXT.PB(e->to);
}
}
v ++;
}
VEC = NEXT;
}
return N == v;
}
int main() {
while( cin >> N >> M && (N|M) ) {
for(int i=0; i<MAX_E; i++) {
es[i].clear();
}
int base = 0;
int id = 0;
vector<int> cost;
for(int i=0; i<M; i++) {
int s, t, c;
cin >> s >> t >> c;
if(c <= 0) base += c;
else {
es[s].PB(Edge(t, c, id));
es[t].PB(Edge(s, c, id));
cost.PB(c);
id ++;
}
}
int E = id;
int ans = INF;
if(!isConnected(-1)) ans = 0;
for(int i=0; i<E; i++) {
if(!isConnected(i)) ans = min(ans, cost[i]);
}
for(int i=0; i<E-1; i++)
for(int j=i+1; j<E; j++)
ans = min(ans, cost[i]+cost[j]);
cout << base + ans << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | java | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//The House of Huge Family
public class Main{
class E{
int s, t, id;
long c;
public E(int s, int t, long c, int id) {
this.s = s;
this.t = t;
this.c = c;
this.id = id;
}
}
int n, m;
List<E>[] adj;
boolean isCon(int f1, int f2){
boolean[] u = new boolean[n];
List<Integer> l = new ArrayList<Integer>();
l.add(0); u[0] = true;
int N = 0;
while(!l.isEmpty()){
List<Integer> next = new ArrayList<Integer>();
for(int i:l){
N++;
for(E e:adj[i])if(!u[e.t]&&e.id!=f1&&e.id!=f2){
u[e.t] = true; next.add(e.t);
}
}
l = next;
}
return N==n;
}
@SuppressWarnings("unchecked")
void run(){
Scanner sc = new Scanner(System.in);
for(;;){
n = sc.nextInt(); m = sc.nextInt();
if((n|m)==0)break;
adj = new List[n];
for(int i=0;i<n;i++)adj[i]=new ArrayList<E>();
int N = 0;
long M = 0;
List<Long> cost = new ArrayList<Long>();
while(m--!=0){
int s = sc.nextInt(), t = sc.nextInt();
long c = sc.nextInt();
if(c<=0)M+=c;
else{
cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++;
}
}
long min = 1L << 40;
if(!isCon(-1, -1))min = 0;
for(int i=0;i<N;i++)if(!isCon(i, -1))min = Math.min(min, cost.get(i));
for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)if(!isCon(i, j))min = Math.min(min, cost.get(i)+cost.get(j));
System.out.println(M+min);
}
}
public static void main(String[] args) {
new Main().run();
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
static const double EPS = 1e-8;
const int tx[] = {0,1,0,-1};
const int ty[] = {-1,0,1,0};
struct Node {
int to;
int idx;
int flow;
Node(int to,int idx,int flow) :
to(to),idx(idx),flow(flow) {}
};
class FordFulkerson {
private:
vector<Node>* _graph;
bool* _used;
int _size;
int dfs(int src,int flow,int sink){
if(src == sink) return flow;
_used[src] = true;
for(int i= 0;i < _graph[src].size();i++){
Node& e = _graph[src][i];
if(_used[e.to]) continue;
if(e.flow <= 0) continue;
int d = dfs(e.to,min(flow,e.flow),sink);
if(d <= 0) continue;
Node& rev_e = _graph[e.to][e.idx];
e.flow -= d;
rev_e.flow += d;
return d;
}
return 0;
}
public:
FordFulkerson(int n) {
_size = n;
_graph = new vector<Node>[_size];
_used = new bool[_size];
}
FordFulkerson(const FordFulkerson& f){
_size = f.size();
_graph = new vector<Node>[_size];
_used = new bool[_size];
fill((bool*)_used,(bool*)_used + _size,false);
for(int i = 0; i < f.size(); i++){
for(int j = 0; j < f.graph()[i].size(); j++){
_graph[i].push_back(f.graph()[i][j]);
}
}
}
void add_edge(int from,int to,int flow) {
_graph[from].push_back(Node(to,_graph[to].size(),flow));
_graph[to].push_back(Node(from,_graph[from].size()-1,flow));
}
int compute_maxflow(int src,int sink){
int res = 0;
while(true){
fill((bool*)_used,(bool*)_used + _size,false);
int tmp = dfs(src,numeric_limits<int>::max(),sink);
if(tmp == 0) break;
res += tmp;
}
return res;
}
int size() const{
return _size;
}
vector<Node>* graph() const{
return _graph;
}
};
class UnionFindTree {
private:
int* par;
int* rank;
public:
UnionFindTree(int n){
par = new int[n]();
rank = new int[n]();
for(int i=0;i<n;i++){
par[i] = i;
rank[i] = 0;
}
}
~UnionFindTree(){
delete[] rank;
delete[] par;
}
int find(int x){
if(par[x] == x){
return x;
} else {
return par[x] = find(par[x]);
}
}
void unite(int x,int y){
x = find(x);
y = find(y);
if (x == y) return;
par[x] = y;
}
bool same(int x,int y){
return find(x) == find(y);
}
};
int main(){
int num_of_houses;
int num_of_paths;
while(~scanf("%d %d",&num_of_houses,&num_of_paths)){
if(num_of_houses == 0 && num_of_paths == 0) break;
FordFulkerson ff(num_of_houses);
UnionFindTree uft(num_of_houses);
int bonus = 0;
for(int path_i = 0; path_i < num_of_paths; path_i++){
int from,to;
int cost;
scanf("%d %d %d",&from,&to,&cost);
if(cost < 0) bonus += cost;
ff.add_edge(from,to,max(0,cost));
uft.unite(from,to);
}
int tree_count = 0;
bool visited[101];
memset(visited,false,sizeof(visited));
for(int house_i = 0; house_i< num_of_houses; house_i++){
if(!visited[uft.find(house_i)]) tree_count++;
visited[uft.find(house_i)] = true;
}
int res = numeric_limits<int>::max();
if(tree_count >= 2){
res = 0;
}
else{
for(int house_i = 0; house_i< num_of_houses; house_i++){
int house_j = uft.find(house_i);
if(house_i == house_j) continue;
FordFulkerson tmp(ff);
res = min(res,tmp.compute_maxflow(house_i,house_j));
}
}
printf("%d\n",bonus + res);
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define NUM 100
struct Info{
Info(int arg_from,int arg_to,int arg_cost){
from = arg_from;
to = arg_to;
cost = arg_cost;
}
int from,to,cost;
};
int N,M;
int boss[100],height[100];
int get_boss(int id){
if(boss[id] == id)return id;
else{
return boss[id] = get_boss(boss[id]);
}
}
void unite(int x,int y){
int boss_x = get_boss(x);
int boss_y = get_boss(y);
if(boss_x == boss_y)return;
if(height[x] > height[y]){
boss[boss_y] = boss_x;
}else if(height[x] < height[y]){
boss[boss_x] = boss_y;
}else{ //height[x] == height[y]
boss[boss_y] = boss_x;
height[x]++;
}
}
void init(){
for(int i = 0; i < N; i++){
boss[i] = i;
height[i] = 0;
}
}
vector<Info> V;
void func(){
init();
V.clear();
int from,to,cost;
int ans = 0;
for(int i = 0; i < M; i++){
scanf("%d %d %d",&from,&to,&cost);
if(cost < 0){
ans += cost;
}else{
unite(from,to);
V.push_back(Info(from,to,cost));
}
}
int group_num = 0;
for(int i = 0; i < N; i++){
if(get_boss(i) == i)group_num++;
}
if(group_num >= 2){
printf("%d\n",ans);
return;
}
int ans_1 = BIG_NUM;
for(int i = 0; i < V.size(); i++){
init();
for(int k = 0; k < V.size(); k++){
if(i == k)continue;
unite(V[k].from,V[k].to);
}
group_num = 0;
for(int k = 0; k < N; k++){
if(get_boss(k) == k)group_num++;
}
if(group_num >= 2){
ans_1 = min(ans_1,ans+V[i].cost);
}
}
int ans_2 = BIG_NUM;
for(int i = 0; i < V.size()-1; i++){
for(int k = i+1; k < V.size(); k++){
init();
for(int p = 0; p < V.size(); p++){
if(p == i || p == k)continue;
unite(V[p].from,V[p].to);
}
group_num = 0;
for(int p = 0; p < N; p++){
if(get_boss(p) == p)group_num++;
}
if(group_num >= 2){
ans_2 = min(ans_2,ans+V[i].cost+V[k].cost);
}
}
}
printf("%d\n",min(ans_1,ans_2));
}
int main(){
while(true){
scanf("%d %d",&N,&M);
if(N == 0 && M == 0)break;
func();
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
static const double EPS = 1e-8;
const int tx[] = {0,1,0,-1};
const int ty[] = {-1,0,1,0};
struct Node {
int to;
int idx;
int flow;
Node(int to,int idx,int flow) :
to(to),idx(idx),flow(flow) {}
};
class FordFulkerson {
private:
vector<Node>* _graph;
bool* _used;
int _size;
int dfs(int src,int flow,int sink){
if(src == sink) return flow;
int res = 0;
_used[src] = true;
for(int i= 0;i < _graph[src].size();i++){
Node& e = _graph[src][i];
if(_used[e.to]) continue;
if(e.flow <= 0) continue;
int d = dfs(e.to,min(flow,e.flow),sink);
if(d > 0){
Node& rev_e = _graph[e.to][e.idx];
e.flow -= d;
rev_e.flow += d;
return d;
}
}
return 0;
}
public:
FordFulkerson(int n) {
_size = n;
_graph = new vector<Node>[_size];
_used = new bool[_size];
}
FordFulkerson(const FordFulkerson& f){
_size = f.size();
_graph = new vector<Node>[_size];
_used = new bool[_size];
fill((bool*)_used,(bool*)_used + _size,false);
for(int i = 0; i < f.size(); i++){
for(int j = 0; j < f.graph()[i].size(); j++){
_graph[i].push_back(f.graph()[i][j]);
}
}
}
void add_edge(int from,int to,int flow) {
_graph[from].push_back(Node(to,_graph[to].size(),flow));
_graph[to].push_back(Node(from,_graph[from].size()-1,flow));
}
int compute_maxflow(int src,int sink){
int res = 0;
while(true){
fill((bool*)_used,(bool*)_used + _size,false);
int tmp = dfs(src,numeric_limits<int>::max(),sink);
if(tmp == 0) break;
res += tmp;
}
return res;
}
int size() const{
return _size;
}
vector<Node>* graph() const{
return _graph;
}
};
class UnionFindTree {
private:
int* par;
int* rank;
public:
UnionFindTree(int n){
par = new int[n]();
rank = new int[n]();
for(int i=0;i<n;i++){
par[i] = i;
rank[i] = 0;
}
}
~UnionFindTree(){
delete[] rank;
delete[] par;
}
int find(int x){
if(par[x] == x){
return x;
} else {
return par[x] = find(par[x]);
}
}
void unite(int x,int y){
x = find(x);
y = find(y);
if (x == y) return;
par[x] = y;
}
bool same(int x,int y){
return find(x) == find(y);
}
};
int main(){
int num_of_houses;
int num_of_paths;
while(~scanf("%d %d",&num_of_houses,&num_of_paths)){
if(num_of_houses == 0 && num_of_paths == 0) break;
FordFulkerson ff(num_of_houses);
UnionFindTree uft(num_of_houses);
int bonus = 0;
for(int path_i = 0; path_i < num_of_paths; path_i++){
int from,to;
int cost;
scanf("%d %d %d",&from,&to,&cost);
if(cost < 0) bonus += cost;
ff.add_edge(from,to,max(0,cost));
uft.unite(from,to);
}
int tree_count = 0;
bool visited[101];
memset(visited,false,sizeof(visited));
for(int house_i = 0; house_i< num_of_houses; house_i++){
if(!visited[uft.find(house_i)]) tree_count++;
visited[uft.find(house_i)] = true;
}
int res = numeric_limits<int>::max();
if(tree_count >= 2){
res = 0;
}
else{
for(int house_i = 0; house_i< num_of_houses; house_i++){
int house_j = uft.find(house_i);
if(house_i == house_j) continue;
FordFulkerson tmp(ff);
res = min(res,tmp.compute_maxflow(house_i,house_j));
}
}
printf("%d\n",bonus + res);
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<stdio.h>
#include<string.h>
long m,n,a,b,c;
long val[105];
long v[105][105];
int getans(){
int ans,i,j,k,r,s1,s2;
long a[105];
long mark[105];
long w[105];
memset(w,0,sizeof(w));
if (n==2){
return val[0];
}
a[0]=0;
for (i=0;i<n;i++)
mark[i]=1;
ans=val[a[0]];
for (i=1;i<=n-1;i++){
r=0;
mark[a[0]]=2;
for (j=0;j<n;j++){
if (mark[j]==1)
w[j]=v[a[0]][j];
}
while (true){
k=-1;
for (j=0;j<n;j++){
if (mark[j]==1 &&(k==-1 || w[j]>w[k]))
k=j;
}
if (k==-1)
break;
r++;
a[r]=k;
ans=ans<val[k]?ans:val[k];
mark[k]=2;
for (j=0;j<n;j++){
if (mark[j]==1)
w[j]+=v[j][k];
}
}
s1=a[r-1];s2=a[r];
val[s1]+=val[s2];
val[s1]-=v[s1][s2]*2;
mark[s2]=3;
for (j=0;j<n;j++){
if (mark[j]!=3){
v[s1][j]=v[j][s1]=v[s1][j]+v[j][s2];
mark[j]=1;
}
}
v[s1][s2]=v[s2][s1]=0;
}
return ans;
}
int main(){
while (scanf("%d %d",&n,&m)==2){
int nowans=0;
if (n==0)
return 0;
memset(val,0,sizeof(val));
memset(v,0,sizeof(v));
for (int j=0;j<m;j++){
scanf("%d %d %d",&a,&b,&c);
if (c<=0)
nowans+=c;
else{
v[a][b]=v[b][a]=v[a][b]+c;
val[a]+=c;
val[b]+=c;
}
}
printf("%d\n",nowans+getans());
}
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <complex>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <sstream>
#include <numeric>
#include <functional>
#include <bitset>
#include <iomanip>
using namespace std;
#define INF (1<<29)
// math
#define Sq(x) ((x)*(x))
// container utility
#define ALL(x) (x).begin(), (x).end()
#define MP make_pair
#define PB push_back
#define EACH(it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();it++)
// rep
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long ll;
// conversion
template<class T> inline string toStr(T a) { ostringstream oss; oss << a; return oss.str(); }
inline int toInt(string s) { return atoi(s.c_str()); }
// prime
bool isPrime(int a) { for(int i=2; i*i <=a; i++) if(a%i == 0) return false; return true; }
// !!! set MAX_L number !!!
#define MAX_L (1000001)
#define MAX_SQRT_B ((int)sqrt(INT_MAX)+1)
bool is_prime[MAX_L];
vector<bool> is_prime_small(MAX_SQRT_B);
void segment_sieve(ll a, ll b) {
for(int i=0; (ll)i*i < b; i++) is_prime_small[i] = true;
for(int i=0; i < b-a; i++) is_prime[i] = true;
if(a == 1) {
is_prime[0] = false;
}
is_prime_small[0] = is_prime_small[1] = false;
for(int i=2; (ll)i*i<b; i++) {
if(is_prime_small[i]) {
for(int j=2*i; (ll)j*j<b; j+=i) is_prime_small[j] = false;
for(ll j = max(2LL, (a+i-1)/i)*i; j<b; j+=i) is_prime[j-a] = false;
}
}
}
//////////////////////////////////////////////////////////////
struct Edge {
int to, cost, id;
Edge(int t, int c, int i)
: to(t), cost(c), id(i) {}
};
#define MAX_E (300)
vector<Edge> es[MAX_E];
int N, M;
bool isConnected(int x) {
vector<bool> used(N);
vector<int> VEC;
VEC.PB(0); used[0] = 1;
int v = 0;
while(!VEC.empty()) {
vector<int> NEXT;
EACH(i, VEC) {
EACH(e, es[*i]) {
if(e->id != x && !used[e->to]) {
used[e->to] = 1;
NEXT.PB(e->to);
}
}
v ++;
}
VEC = NEXT;
}
return N == v;
}
int main() {
while( cin >> N >> M && (N|M) ) {
for(int i=0; i<MAX_E; i++) {
es[i].clear();
}
int base = 0;
int id = 0;
vector<int> cost;
for(int i=0; i<M; i++) {
int s, t, c;
cin >> s >> t >> c;
if(c <= 0) base += c;
else {
es[s].PB(Edge(t, c, id));
es[t].PB(Edge(s, c, id));
cost.PB(c);
id ++;
}
}
int E = id;
int ans = INF;
if(!isConnected(-1)) ans = 0;
for(int i=0; i<E; i++) {
if(!isConnected(i)) ans = min(ans, cost[i]);
}
for(int i=0; i<E-1; i++)
for(int j=i+1; j<E; j++)
ans = min(ans, cost[i]+cost[j]);
cout << base + ans << endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
#define N 105
using namespace std;
struct edge{
int to, num;
};
int n, m;
vector<edge> G[N];
int A, B, minuscost;
int used[N], cost[N];
int dfs(int x){
if(used[x]) return 0;
used[x]=1;
int res=0;
for(int i=0;i<G[x].size();i++){
if(A==G[x][i].num) continue;
if(B==G[x][i].num) continue;
res+=dfs(G[x][i].to);
}
return res+1;
}
void solve(){
memset(used,0,sizeof(used));
A=-1, B=-1;
int nodesum=dfs(0);
if(nodesum!=n){
cout<<minuscost<<endl;
return ;
}
int ans=(1e9);
for(int i=0;i<m;i++){
A=i;
for(int j=i+1;j<m;j++){
B=j;
memset(used,0,sizeof(used));
int r=dfs(0);
if(r!=n) ans=min(ans, cost[A]+cost[B]+minuscost);
}
B=-1;
memset(used,0,sizeof(used));
int r=dfs(0);
if(r!=n) ans=min(ans, cost[A]+minuscost);
}
cout<<ans<<endl;
}
int main(){
while(1){
cin>>n>>m;
if(!n&&!m) break;
minuscost=0;
int cnt=0;
for(int i=0;i<m;i++){
int x, y, c;
cin>>x>>y>>c;
if(c<=0){
minuscost+=c;
continue;
}
cost[cnt]=c;
G[x].push_back((edge){y,cnt});
G[y].push_back((edge){x,cnt++});
}
m=cnt;
solve();
for(int i=0;i<n;i++) G[i].clear();
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<bits/stdc++.h>
using namespace std;
using Int = long long;
struct Fordfulerson{
const int INF = 1<<28;
struct edge{
int to,cap,rev;
edge(){};
edge(int to,int cap,int rev):to(to),cap(cap),rev(rev){};
};
int n;
vector<vector<edge> > G;
vector<int> used;
Fordfulerson(){};
Fordfulerson(int sz):n(sz),G(n),used(n){}
void add_edge(int from,int to,int cap){
G[from].push_back(edge(to,cap,G[to].size()));
G[to].push_back(edge(from,0,G[from].size()-1));
}
int dfs(int v,int t,int f){
if(v == t) return f;
used[v] = true;
for(int i=0;i<(int)G[v].size();i++){
edge &e = G[v][i];
if(!used[e.to] && e.cap > 0){
int d = dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t){
int flow = 0;
for(;;){
fill(used.begin(),used.end(),0);
int f = dfs(s,t,INF);
if(f == 0) return flow;
flow += f;
}
}
};
struct SCC{
int n;
vector<vector<int> > G,rG,T,C;
vector<int> vs,used,belong;
SCC(){};
SCC(int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){}
void add_edge(int from,int to){
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v){
used[v] = 1;
for(int i=0;i<(int)G[v].size();i++){
if(!used[G[v][i]]) dfs(G[v][i]);
}
vs.push_back(v);
}
void rdfs(int v,int k){
used[v] = 1;
belong[v] = k;
C[k].push_back(v);
for(int i=0;i<(int)rG[v].size();i++) {
if(!used[rG[v][i]]) rdfs(rG[v][i],k);
}
}
int build(){
fill(used.begin(),used.end(),0);
vs.clear();
for(int v=0;v<n;v++){
if(!used[v]) dfs(v);
}
fill(used.begin(),used.end(),0);
int k=0;
for(int i=vs.size()-1;i>=0;i--){
if(!used[vs[i]]){
T.push_back(vector<int>());
C.push_back(vector<int>());
rdfs(vs[i],k++);
}
}
return k;
}
};
struct UnionFind{
int n;
vector<int> r,p;
UnionFind(int n):n(n),r(n,1),p(n){iota(p.begin(),p.end(),0);}
int find(int x){
if(x==p[x]) return x;
return p[x]=find(p[x]);
}
void unite(int x,int y){
x=find(x);y=find(y);
if(x==y) return;
if(r[x]<r[y]) swap(x,y);
r[x]+=r[y];
p[y]=x;
}
};
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,m;
while(cin>>n>>m,n){
vector<int> x(m),y(m),c(m);
int ans=0;
UnionFind uf(n);
SCC scc(n);
for(int i=0;i<m;i++){
cin>>x[i]>>y[i]>>c[i];
if(c[i]<=0) ans+=c[i];
else{
scc.add_edge(x[i],y[i]);
uf.unite(x[i],y[i]);
}
}
if(uf.r[uf.find(0)]!=n){
cout<<ans<<endl;
continue;
}
scc.build();
//cout<<ans<<endl;
auto b=scc.belong;
vector<int> v;
int t=-1;
for(int i=0;i<m;i++){
if(c[i]<=0) continue;
if(b[x[i]]==b[y[i]]){
v.emplace_back(c[i]);
continue;
}
if(t<0||c[i]<t) t=c[i];
}
sort(v.begin(),v.end());
if(!v.empty()&&(t<0||v[0]+v[1]<t)) t=v[0]+v[1];
cout<<ans+t<<endl;
}
return 0;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | CORRECT | cpp | #include<iostream>
#include<cstdio>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<cassert>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define ALL(C) (C).begin(),(C).end()
#define fi first
#define se second
const int N = 10000;
struct Edge{
int to,cost;
};
//solve connected graph
vector<int> edgeOnCycle;//only cost is enough
vector<int> edgeOnTree;//only cost is enough
int minEdgeOnCycle[2];//[0] < [1]
int minEdgeOnTree;
vector<Edge> edge[N];//
int dig[N];//for assertion
int vis[N];
bool dfs(int now,int color){
if (vis[now])return vis[now] == color;
vis[now]=color;
bool oncycle=false;
rep(i,(int)edge[now].size()){
bool tmp=dfs(edge[now][i].to,color);
if (tmp){
oncycle=true;
if (edge[now][i].cost < minEdgeOnCycle[0]){
minEdgeOnCycle[1]=minEdgeOnCycle[0];
minEdgeOnCycle[0]=edge[now][i].cost;
}else if (edge[now][i].cost < minEdgeOnCycle[1]){
minEdgeOnCycle[1]=edge[now][i].cost;
}
}else minEdgeOnTree=min(minEdgeOnTree,edge[now][i].cost);
}
return oncycle;
}
//find connect ?
vector<int> udedge[N];//undirect
void dfs2(int now){
if (vis[now])return;
vis[now]=true;
rep(i,(int)udedge[now].size()){
dfs2(udedge[now][i]);
}
}
bool isconnected(int n){
rep(i,n)vis[i]=0;
dfs2(0);
rep(i,n){
if (vis[i]==0)return false;
}
return true;
}
int solve(int n){
if (!isconnected(n))return 0;
int color=1;
rep(i,n)vis[i]=0;
rep(i,n){
if (vis[i] == 0)dfs(i,color),color++;
}
if (minEdgeOnCycle[0] == INT_MAX)return minEdgeOnTree;
return min(minEdgeOnCycle[0]+minEdgeOnCycle[1],minEdgeOnTree);
}
int main(){
int n,m;
while(cin>>n>>m && n){
rep(i,n){
udedge[i].clear();
edge[i].clear();
dig[i]=0;
}
edgeOnCycle.clear();
edgeOnTree.clear();
minEdgeOnCycle[0]=INT_MAX;
minEdgeOnCycle[1]=INT_MAX;
minEdgeOnTree=INT_MAX;
int ans=0;
rep(i,m){
int f,t,c;
cin>>f>>t>>c;
dig[t]++;
if (c <= 0){//elminate negative edge (also negative seledge)
ans+=c;//c<0
}else if (f == t){//ignore self edge of positive cost
}else {
edge[f].push_back((Edge){t,c});
udedge[f].push_back(t);
udedge[t].push_back(f);
}
}
rep(i,n)if (dig[i] >1)assert(false);
int sum=solve(n);
cout << ans+sum << endl;
}
return false;
} |
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int v, e, f[100];
vector<int> G[100];
void dfs(int n, int a) {
f[n] = a;
for (int i = 0; i < (int)G[n].size(); i++)
if (!f[G[n][i]]) dfs(G[n][i], a);
}
int main() {
for (; cin >> v >> e && v;) {
int r = 1e9, ch = 0;
fill(f, f + 100, 0);
for (int i = 0; i < v; i++) G[i].clear();
for (int i = 0, a, b, c; i < e && cin >> a >> b >> c; i++)
G[a].push_back(b), G[b].push_back(a), r = min(r, c);
for (int i = 0; i < v; i++)
if (!f[i]) dfs(i, i + 1);
for (int i = 0; i < v; i++) ch += f[i];
if (ch == v)
cout << r << endl;
else
cout << 0 << endl;
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
long long g[111][111], w[111];
long long a[111], v[111], na[111];
long long Stoer_Wagner(int n) {
long long i, j, pv, zj, I = 1;
long long best = I << 32;
for (i = 0; i < n; i++) v[i] = i;
while (n > 1) {
for (a[v[0]] = 1, i = 1; i < n; i++) {
a[v[i]] = 0;
na[i - 1] = i;
w[i] = g[v[0]][v[i]];
}
for (pv = v[0], i = 1; i < n; i++) {
for (zj = -1, j = 1; j < n; j++)
if (!a[v[j]] && (zj < 0 || w[j] > w[zj])) zj = j;
a[v[zj]] = 1;
if (i == n - 1) {
if (best > w[zj]) best = w[zj];
for (i = 0; i < n; i++) g[v[i]][pv] = g[pv][v[i]] += g[v[zj]][v[i]];
v[zj] = v[--n];
break;
}
pv = v[zj];
for (j = 1; j < n; j++)
if (!a[v[j]]) w[j] += g[v[zj]][v[j]];
}
}
return best;
}
bool vis[111];
void dfs(int u, int n) {
for (int v = 0; v < n; ++v)
if (g[u][v] != 0 && vis[v] == 0) {
vis[v] = 1;
dfs(v, n);
}
}
bool ok(int n) {
for (int i = 0; i < n; ++i)
if (vis[i] == 0) return 1;
return 0;
}
int main() {
printf(
"%d"
"%d",
1, 2);
int n, m, u, v, w;
while (scanf("%d%d", &n, &m), n + m) {
memset(g, 0, sizeof(g));
long long ans = 0;
while (m--) {
scanf("%d%d%d", &u, &v, &w);
if (w <= 0)
ans += w;
else
g[v][u] = g[u][v] = w;
}
printf("%lld\n", ans + Stoer_Wagner(n));
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
while (true) {
long long n, m;
cin >> n >> m;
if (n == 0 && m == 0) break;
assert(n >= m);
long long sum = 0;
vector<pair<long long, long long> > edge[n];
for (int i = 0; i < int(m); ++i) {
long long x, y, c;
cin >> x >> y >> c;
if (c < 0)
sum += c;
else
edge[x].push_back(make_pair(y, c));
}
long long res = 1234567890123456789;
{
bool can[111][111] = {};
for (int i = 0; i < int(n); ++i) {
queue<long long> pos;
pos.push(i);
while (!pos.empty()) {
long long now = pos.front();
pos.pop();
can[i][now] = true;
for (int j = 0; j < int(edge[now].size()); ++j) {
if (!can[i][edge[now][j].first]) {
pos.push(edge[now][j].first);
}
}
}
}
for (int i = 0; i < int(n); ++i)
for (int j = 0; j < int(n); ++j)
if (!can[i][j] && !can[j][i]) {
cout << sum << endl;
goto next;
}
}
for (int iii = 0; iii < int(n); ++iii)
for (int jjj = 0; jjj < int(edge[iii].size()); ++jjj)
for (int kkk = 0; kkk < int(n); ++kkk)
for (int lll = 0; lll < int(edge[kkk].size()); ++lll) {
bool can[111][111] = {};
for (int i = 0; i < int(n); ++i) {
queue<long long> pos;
pos.push(i);
while (!pos.empty()) {
long long now = pos.front();
pos.pop();
can[i][now] = true;
for (int j = 0; j < int(edge[now].size()); ++j) {
if (now == iii && j == jjj) continue;
if (now == kkk && j == lll) continue;
if (!can[i][edge[now][j].first]) {
pos.push(edge[now][j].first);
}
}
}
}
for (int i = 0; i < int(n); ++i)
for (int j = 0; j < int(n); ++j)
if (!can[i][j] && !can[j][i]) {
if (iii == kkk && jjj == lll) {
res = min(res, edge[iii][jjj].second);
} else {
res =
min(res, edge[iii][jjj].second + edge[iii][jjj].second);
}
}
}
cout << sum + res << endl;
next:;
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int x[100], y[100], c[100];
int r[100], tmp1, tmp2;
int cost, total;
int i, j;
while (1) {
cin >> n >> m;
if (!n && !m) break;
total = 0;
for (i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> c[i];
total += c[i];
}
for (i = 0; i < n; i++) r[i] = i;
for (i = 0; i < m; i++) {
for (j = i + 1; j < m; j++) {
if (c[i] < c[j]) {
swap(x[i], x[j]);
swap(y[i], y[j]);
swap(c[i], c[j]);
}
}
}
cost = 0;
for (i = 0; i < m; i++) {
tmp1 = r[0];
tmp2 = -1;
for (j = 1; j < n; j++) {
if (tmp1 != r[j]) {
if (tmp2 < 0)
tmp2 = r[j];
else if (tmp2 != r[j])
break;
}
}
if (j == n) break;
cost += c[i];
if (c[i] < 0 && r[x[i]] == r[y[i]]) cost -= c[i];
tmp1 = r[y[i]];
for (j = 0; j < n; j++) {
if (r[j] == tmp1) r[j] = r[x[i]];
}
}
while (c[i] >= 0 && i < m) {
if (r[x[i]] == r[y[i]]) cost += c[i];
i++;
}
cout << total - cost << endl;
}
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) {
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t) {
stringstream ss;
ss << f;
ss >> t;
}
static const int INF = 1 << 24;
template <class V>
class MaxFlow_dinic {
public:
struct edge {
int to, reve;
V cap;
};
static const int MV = 1100;
vector<edge> E[MV];
int itr[MV], lev[MV];
void add_edge(int x, int y, V cap, bool undir = false) {
E[x].push_back((edge){y, (int)E[y].size(), cap});
E[y].push_back((edge){x, (int)E[x].size() - 1, undir ? cap : 0});
}
void bfs(int cur) {
memset(lev, 0xff, sizeof(lev));
queue<int> q;
lev[cur] = 0;
q.push(cur);
while (q.size()) {
int v = q.front();
q.pop();
for (__typeof(E[v].begin()) e = E[v].begin(); e != E[v].end(); e++)
if (e->cap > 0 && lev[e->to] < 0)
lev[e->to] = lev[v] + 1, q.push(e->to);
}
}
V dfs(int from, int to, V cf) {
if (from == to) {
return cf;
}
for (; itr[from] < E[from].size(); itr[from]++) {
edge *e = &E[from][itr[from]];
if (e->cap > 0 && lev[from] < lev[e->to]) {
V f = dfs(e->to, to, min(cf, e->cap));
if (f > 0) {
e->cap -= f;
E[e->to][e->reve].cap += f;
return f;
}
}
}
return 0;
}
V maxflow(int from, int to) {
V fl = 0, tf;
while (1) {
bfs(from);
if (lev[to] < 0) return fl;
memset(itr, 0, sizeof(itr));
while ((tf = dfs(from, to, numeric_limits<V>::max())) > 0) fl += tf;
}
}
};
void mainmain() {
int n, m;
while (cin >> n >> m, n || m) {
long long ans = INT_MAX;
vector<int> a(m), b(m), c(m);
for (int(i) = (0); (i) < ((m)); ++(i)) cin >> a[i] >> b[i] >> c[i];
for (int i = (1); i < (n); ++i) {
MaxFlow_dinic<long long> mf;
int tmp = 0;
for (int(j) = (0); (j) < ((m)); ++(j)) {
if (c[j] < 0)
tmp += c[j];
else
mf.add_edge(a[j], b[j], (long long)c[j]);
}
ans = min(ans, tmp + mf.maxflow(0, i));
}
cout << ans << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(0);
mainmain();
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
int inf;
using namespace std;
struct edge {
int u, v, next, f, pre;
} e[100000];
int num, rnum;
int n, m, N;
int source, sink;
int head[132], rhead[132], start[132];
int d[132], numb[132];
int p[132];
void Init() {
memset(head, -1, sizeof(head));
memset(rhead, -1, sizeof(rhead));
memset(p, -1, sizeof(p));
num = 0;
}
void BFS() {
int i, j;
for (i = 1; i <= N; i++) {
d[i] = N;
numb[i] = 0;
}
int Q[132], head(0), tail(0);
d[sink] = 0;
numb[0] = 1;
Q[++tail] = sink;
while (head < tail) {
i = Q[++head];
for (j = rhead[i]; j != -1; j = e[j].pre) {
if (e[j].f == 0 || d[e[j].u] < N) continue;
d[e[j].u] = d[i] + 1;
numb[d[e[j].u]]++;
Q[++tail] = e[j].u;
}
}
}
int Augment() {
int i;
int tmp = inf;
for (i = p[sink]; i != -1; i = p[e[i].u]) {
if (tmp > e[i].f) tmp = e[i].f;
}
for (i = p[sink]; i != -1; i = p[e[i].u]) {
e[i].f -= tmp;
e[i ^ 1].f += tmp;
}
return tmp;
}
int Retreat(int &i) {
int tmp, j, mind(N - 1);
for (j = head[i]; j != -1; j = e[j].next) {
if (e[j].f > 0 && d[e[j].v] < mind) mind = d[e[j].v];
}
tmp = d[i];
d[i] = mind + 1;
numb[tmp]--;
numb[d[i]]++;
if (i != source) i = e[p[i]].u;
return numb[tmp];
}
int maxflow() {
int flow(0);
int i, j;
BFS();
for (i = 1; i <= N; i++) start[i] = head[i];
i = source;
while (d[source] < N) {
for (j = start[i]; j != -1; j = e[j].next)
if (e[j].f > 0 && d[i] == d[e[j].v] + 1) break;
if (j != -1) {
start[i] = j;
p[e[j].v] = j;
i = e[j].v;
if (i == sink) {
flow += Augment();
i = source;
}
} else {
start[i] = head[i];
if (Retreat(i) == 0) break;
}
}
return flow;
}
void addedge(int a, int b, int c) {
e[num].f = c;
e[num].u = a;
e[num].v = b;
e[num].next = head[a];
head[a] = num;
e[num].pre = rhead[b];
rhead[b] = num;
num++;
e[num].u = b;
e[num].v = a;
e[num].f = 0;
e[num].next = head[b];
head[b] = num;
e[num].pre = rhead[a];
rhead[a] = num;
num++;
}
int g[132][132];
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
int i;
int a, b, c;
while (scanf("%d%d", &n, &m) != EOF) {
if (n == 0 && m == 0) break;
N = n;
memset(g, 0, sizeof(g));
int ans = 0;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &a, &b, &c);
a++;
b++;
g[a][b] += c;
ans += c;
}
inf = ans;
for (int i = 2; i <= n; i++) {
source = 1;
sink = i;
Init();
int tmp = 0;
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) {
if (j == k) continue;
if (g[j][k] < 0) tmp += g[j][k];
if (g[j][k] > 0) addedge(j, k, g[j][k]);
}
ans = min(ans, maxflow() + tmp);
}
printf("%d\n", ans);
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline bool minimize(T1& a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2>
inline bool maximize(T1& a, T2 b) {
return a < b && (a = b, 1);
}
int const inf = 1 << 29;
struct unicycle_graph {
int N;
using graph_edge_t = pair<int, long long>;
vector<vector<graph_edge_t>> g;
vector<int> cycle_vs_;
vector<bool> in_cycle_v_;
using edge = pair<int, int>;
set<edge> in_cycle_edge_;
unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {}
void add_edge(int f, int t, int c) {
g[f].emplace_back(t, c);
g[t].emplace_back(f, c);
}
bool remove_edge(int f, int t) {
for (int i = 0; i < (int)g[f].size(); i++) {
if (g[f][i].first == t) {
g[f].erase(g[f].begin() + i);
for (int k = 0; k < (int)g[t].size(); k++) {
if (g[t][k].first == f) {
g[t].erase(g[t].begin() + k);
if (in_cycle_edge(f, t)) {
cycle_vs_.clear();
in_cycle_v_.clear();
in_cycle_v_.resize(N);
in_cycle_edge_.clear();
}
return true;
}
}
}
}
return false;
}
void build() {
vector<bool> vis(N);
vector<int> prev(N);
std::function<bool(int, int)> dfs = [&](int x, int p) {
for (auto const& e : g[x]) {
int to, cost;
tie(to, cost) = e;
if (to == p) {
continue;
}
prev[to] = x;
if (vis[to]) {
for (int curr = x; curr != to; curr = prev[curr]) {
cycle_vs_.push_back(curr);
in_cycle_v_[curr] = 1;
in_cycle_edge_.emplace(prev[curr], curr);
in_cycle_edge_.emplace(curr, prev[curr]);
}
cycle_vs_.push_back(to);
in_cycle_v_[to] = 1;
in_cycle_edge_.emplace(to, prev[to]);
in_cycle_edge_.emplace(prev[to], to);
reverse((cycle_vs_).begin(), (cycle_vs_).end());
return true;
} else {
vis[to] = 1;
if (dfs(to, x)) {
return true;
}
}
}
return false;
};
for (int i = 0; i < (int)N; i++) {
if (!vis[i]) {
vis[i] = 1;
if (dfs(i, -1)) {
break;
}
}
}
}
bool in_cycle(int x) { return in_cycle_v_[x]; }
bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); }
vector<int> const& cycle_vertices() { return cycle_vs_; }
};
int count_components(unicycle_graph::vector<vector<graph_edge_t>>& g) {
int N = g.size();
vector<bool> vis(N);
std::function<void(int)> dfs = [&](int x) {
for (auto&& e : g[x]) {
if (vis[e.first]) {
continue;
}
vis[e.first] = 1;
dfs(e.first);
}
};
int ret = 0;
for (int i = 0; i < (int)N; i++) {
if (!vis[i]) {
vis[i] = 1;
dfs(i);
ret++;
}
}
return ret;
}
struct union_find {
vector<int> rank_, size_, rid_;
union_find(int n) {
rank_.resize(n);
rid_.assign(n, -1);
size_.resize(n, 1);
}
void operator()(int u, int v) {
u = operator[](u), v = operator[](v);
if (u == v) {
return;
}
size_[u] = size_[v] = size_[u] + size_[v];
if (rank_[u] < rank_[v]) {
rid_[u] = v;
} else {
rid_[v] = u;
if (rank_[u] == rank_[v]) {
rank_[u]++;
}
}
}
int operator[](int x) {
if (rid_[x] < 0)
return x;
else
return rid_[x] = operator[](rid_[x]);
}
int size_of(int x) { return size_[x]; }
};
int main() {
for (int N, M; cin >> N >> M && (N | M);) {
unicycle_graph ug(N);
union_find uf(N);
vector<pair<long long, unicycle_graph::edge>> all_edges;
for (int i = 0; i < (int)M; i++) {
int x, y;
long long c;
cin >> x >> y >> c;
all_edges.emplace_back(c, make_pair(x, y));
ug.add_edge(x, y, c);
uf(x, y);
}
ug.build();
assert(uf.size_of(0) == N);
vector<long long> ccosts;
long long mcost = 1e10;
for (int i = 0; i < (int)M; i++) {
auto c = all_edges[i].first;
auto e = all_edges[i].second;
if (ug.in_cycle_edge(e.first, e.second)) {
ccosts.push_back(c);
} else {
minimize(mcost, c);
}
}
sort((ccosts).begin(), (ccosts).end());
if (ccosts.size() > 1)
mcost = min({mcost, ccosts[0] + ccosts[1], mcost + ccosts[0]});
if (mcost == 1e10)
cout << 0 << endl;
else
cout << mcost << endl;
}
return 0;
}
|
p00650 The House of Huge Family | Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future.
Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town.
They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups.
They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways.
Now, you have a figure of room layout. Your task is written below.
You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room.
To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost.
Please answer the lowest cost.
By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way.
Constraints
* 2 ≤ n ≤ 100
* -10,000 ≤ Ci ≤ 10,000
* Y1 ... Ym can't be duplicated integer by each other.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
n m
X1 Y1 C1
...
Xm Ym Cm
All numbers in each datasets are integers. The integers in each line are separated by a space.
The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1.
Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space).
Output
For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers.
Example
Input
3 2
0 1 2
1 2 1
2 1
0 1 100
2 1
0 1 0
2 1
0 1 -1
0 0
Output
1
100
0
-1 | {
"input": [
"3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0"
],
"output": [
"1\n100\n0\n-1"
]
} | {
"input": [],
"output": []
} | IN-CORRECT | cpp | #include<vector>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<cctype>
#include<cassert>
#include<cmath>
#include<climits>
#define all(c) (c).begin(),(c).end()
using namespace std;
typedef unsigned int uint;
typedef long long ll;
struct edge{ll to,cap,rev;};
#define MAX_V 200
vector<edge>G[MAX_V];
void add_edge(ll from,ll to,ll cap){
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}
bool used[MAX_V];
ll dfs(ll v,ll t,ll f){
if(v==t)return f;
used[v]=true;
for(int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(!used[e.to] && e.cap>0){
ll d=dfs(e.to,t,min(e.cap,f));
if(d>0){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
#define INF (1<<29)
ll max_flow(ll s,ll t){
ll flow=0;
for(;;){
fill(used,used+MAX_V,false);
ll f=dfs(s,t,INF);
if(f==0)return flow;
flow+=f;
}
}
vector<edge>g[MAX_V];
void add_edge2(ll from,ll to,ll cap){
g[from].push_back((edge){to,cap,g[to].size()});
g[to].push_back((edge){from,0,g[from].size()-1});
}
int main(void){
int n,m;
while(cin >> n >> m,n|m){
for(int i=0;i<MAX_V;i++)G[i].clear(),g[i].clear();
ll a,b,c,sum=0;
for(int i=0;i<m;i++){
cin >> a >> b >> c;
add_edge2(a,b,c);
if(c<0)sum+=c;
}
ll s=n,t=s+1;
ll ans=INT_MAX;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j){
for(int k=0;k<MAX_V;k++)G[i]=g[i];
add_edge(s,i,INT_MAX);
add_edge(j,t,INT_MAX);
ans=min(ans,max_flow(s,t));
}
}
}
cout << ans+sum << endl;
}
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.