-
Notifications
You must be signed in to change notification settings - Fork 0
/
B.cpp
159 lines (135 loc) · 3.63 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
B - 选数
*/
#include <bits/stdc++.h>
using namespace std;
namespace simplex {
using Array = vector<double>;
using Matrix = vector<Array>;
const double eps = 1e-8, inf = (double) ( 1ll << 60 );
// 系数矩阵,第一行为c,第一列为b
Matrix A;
class Solver {
private:
int m;
int n;
public:
Solver( int _m, int _n )
: m( _m )
, n( _n ) {}
void input_matrix( const Matrix &a, const Array &b, const Array &c ) {
for ( int i = 1; i <= n; ++i ) {
A[ 0 ][ i ] = c[ i - 1 ];
}
for ( int i = 1; i <= m; ++i ) {
for ( int j = 1; j <= n; ++j ) {
A[ i ][ j ] = a[ i - 1 ][ j - 1 ];
}
A[ i ][ 0 ] = b[ i - 1 ]; // input b
}
}
void display() const {
cout << "m=" << m << ", n=" << n << endl;
for ( int i = 0; i <= m; ++i ) {
for ( int j = 0; j <= n; ++j ) {
cout << "\t" << A[ i ][ j ];
}
cout << endl;
}
}
void pivot( int l, int e ) {
double tmp = A[ l ][ e ];
A[ l ][ e ] = 1.0;
vector<int> tmp_arr;
for ( int j = 0; j <= n; ++j ) {
if ( std::fabs( A[ l ][ j ] ) > 0 ) {
A[ l ][ j ] /= tmp;
tmp_arr.push_back( j );
}
}
int cnt = tmp_arr.size();
for ( int i = 0; i <= m; i++ ) {
if ( i != l && std::fabs( A[ i ][ e ] ) > eps ) {
tmp = A[ i ][ e ], A[ i ][ e ] = 0;
for ( int j = 0; j < cnt; ++j ) {
A[ i ][ tmp_arr[ j ] ] -= A[ l ][ tmp_arr[ j ] ] * tmp;
}
}
}
}
bool simplex( bool verbose = false ) {
while ( true ) {
int l = 0, e = 0;
double minv = inf;
for ( int j = 1; j <= n; ++j ) {
if ( A[ 0 ][ j ] > eps ) {
e = j;
break;
}
}
if ( !e ) break;
for ( int i = 1; i <= m; ++i ) {
if ( A[ i ][ e ] > eps && A[ i ][ 0 ] / A[ i ][ e ] < minv ) {
minv = A[ i ][ 0 ] / A[ i ][ e ];
l = i;
}
}
if ( !l ) {
// printf( "Unbounded\n" );
return false;
}
pivot( l, e );
if ( verbose ) display();
}
return true;
}
double get_result() const { return -A[ 0 ][ 0 ]; }
void standardize() {
for ( int i = 0; i < n; ++i )
A[ 0 ].push_back( 0 );
for ( int i = 1; i <= m; ++i ) {
for ( int j = 1; j <= m; ++j ) {
if ( i == j ) {
A[ i ].push_back( 1 );
} else {
A[ i ].push_back( 0 );
}
}
}
n += m;
}
};
} // namespace simplex
int main() {
using namespace simplex;
int N, K;
cin >> N >> K;
int row = 2 * N + 1;
int bn = 5 * N + 1, cn = 3 * N;
A = Matrix( bn + 1, Array( cn + 1 ) );
for ( int i = 1; i <= cn; ++i )
cin >> A[ 0 ][ i ];
for ( int i = 1; i <= row; ++i ) {
A[ i ][ 0 ] = K;
for ( int j = i; j < i + N; ++j ) {
A[ i ][ j ] = 1;
}
}
for ( int i = row + 1; i <= row + cn; ++i ) {
A[ i ][ 0 ] = 1;
A[ i ][ i - row ] = 1;
}
Solver s( bn, cn );
// s.display();
s.simplex();
// s.display();
cout << int( s.get_result() + 0.5 ) << endl;
return 0;
}
/*
Sample Input
5 3
14 21 9 30 11 8 1 20 29 23 17 27 7 8 35
Sample Output
195
*/