GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add files via upload · SuckSolace/code-library@83130de

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Math
    • Simplex Algorithm.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Math/Simplex Algorithm.cpp‎

Copy file name to clipboard
+215Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
const int N=510;
5+
const int mod=1e9+7;
6+
using ll = long long;
7+
using ld = long double;
8+
const ld eps=1e-9;
9+
const ld PI=acos(-1.0);
10+
11+
ll n,k,a[N],dis[N][N];
12+
vector <ll> g[N],cost[N];
13+
bool ok[N][N];
14+
15+
void dfs(ll u,ll p,ll d,ll cur) {
16+
dis[cur][u] = d;
17+
ll len = g[u].size(), i;
18+
for(int i=0;i<len;i++) {
19+
ll v = g[u][i];
20+
if(v == p )continue;
21+
dfs(v,u,d+cost[u][i],cur);
22+
}
23+
}
24+
25+
#define MAXC 1010
26+
#define MAXV 1010
27+
#define EPS 1e-13
28+
29+
#define MINIMIZE -1
30+
#define MAXIMIZE +1
31+
#define LESSEQ -1
32+
#define EQUAL 0
33+
#define GREATEQ 1
34+
#define INFEASIBLE -1
35+
#define UNBOUNDED 666
36+
37+
38+
/***
39+
1. Simplex Algorithm for Linear Programming
40+
2. Maximize or minimize f0*x0 + f1*x1 + f2*x2 + ... + fn-1*xn-1 subject to some constraints
41+
3. Constraints are of the form, c0x0 + c1x1 + c2x2 + ... + cn-1xn-1 (<= or >= or =) lim
42+
4. m is the number of constraints indexed from 1 to m, and n is the number of variables indexed from 0 to n-1
43+
5. ar[0] contains the objective function f, and ar[1] to ar[m] contains the constraints, ar[i][n] = lim_i
44+
6. It is assumed that all variables satisfies non-negativity constraint, i.e, xi >= 0
45+
7. If non-negativity constraint is not desired for a variable x, replace each occurrence
46+
by difference of two new variables r1 and r2 (where r1 >= 0 and r2 >= 0, handled automatically by simplex).
47+
That is, replace every x by r1 - r2 (Number of variables increases by one, -x, +r1, +r2)
48+
8. solution_flag = INFEASIBLE if no solution is possible and UNBOUNDED if no finite solution is possible
49+
9. Returns the maximum/minimum value of the linear equation satisfying all constraints otherwise
50+
10. After successful completion, val[] contains the values of x0, x1 .... xn for the optimal value returned
51+
52+
*** If ABS(X) <= M in constraints, Replace with X <= M and -X <= M
53+
54+
*** Fractional LP:
55+
max/min
56+
3x1 + 2x2 + 4x3 + 6
57+
-------------------
58+
3x1 + 3x2 + 2x3 + 5
59+
60+
constraint:
61+
2x1 + 3x2 + 5x3 ≥ 23
62+
3x2 + 5x2 + 4x3 <= 30
63+
x1, x2, x3 ≥ 0
64+
65+
Replace with:
66+
max/min
67+
3y1 + 2y2 + 4y3 + 6t
68+
69+
constraint:
70+
3y1 + 3y2 + 2y3 + 5t = 1
71+
2y1 + 3y2 + 53 - 23t ≥ 0
72+
3y1 + 5y2 + 4y3- 30t <= 0
73+
y1, y2, y3, t ≥ 0
74+
***/
75+
76+
//Complexity: O(n^3) or much faster
77+
//MAXV=1e5,MAXC=2 works within 80ms
78+
//MAXV=1e5,MAXC=200 works within 1.5s
79+
80+
namespace lp {
81+
long double val[MAXV], ar[MAXC][MAXV];
82+
int m, n, solution_flag, minmax_flag, basis[MAXC], index[MAXV];
83+
84+
/// nvars = number of variables, f = objective function, flag = MINIMIZE or MAXIMIZE
85+
inline void init(int nvars, long double f[], int flag){
86+
solution_flag = 0;
87+
ar[0][nvars] = 0.0;
88+
m = 0, n = nvars, minmax_flag = flag;
89+
for (int i = 0; i < n; i++){
90+
ar[0][i] = f[i] * minmax_flag; /// Negating sign of objective function when minimizing
91+
}
92+
}
93+
94+
/// C[] = co-efficients of the constraints (LHS), lim = limit in RHS
95+
/// cmp = EQUAL for C[] = lim, LESSEQ for C[] <= lim, GREATEQ for C[] >= lim
96+
inline void add_constraint(long double C[], long double lim, int cmp){
97+
m++, cmp *= -1;
98+
if (cmp == 0){
99+
for (int i = 0; i < n; i++) ar[m][i] = C[i];
100+
ar[m++][n] = lim;
101+
for (int i = 0; i < n; i++) ar[m][i] = -C[i];
102+
ar[m][n] = -lim;
103+
}
104+
else{
105+
for (int i = 0; i < n; i++) ar[m][i] = C[i] * cmp;
106+
ar[m][n] = lim * cmp;
107+
}
108+
}
109+
110+
inline void init(){ /// Initialization
111+
for (int i = 0; i <= m; i++) basis[i] = -i;
112+
for (int j = 0; j <= n; j++){
113+
ar[0][j] = -ar[0][j], index[j] = j, val[j] = 0;
114+
}
115+
}
116+
117+
inline void pivot(int m, int n, int a, int b){ /// Pivoting and exchanging a non-basic variable with a basic variable
118+
for (int i = 0; i <= m; i++){
119+
if (i != a){
120+
for (int j = 0; j <= n; j++){
121+
if (j != b){
122+
ar[i][j] -= (ar[i][b] * ar[a][j]) / ar[a][b];
123+
}
124+
}
125+
}
126+
}
127+
for (int j = 0; j <= n; j++){
128+
if (j != b) ar[a][j] /= ar[a][b];
129+
}
130+
for (int i = 0; i <= m; i++){
131+
if (i != a) ar[i][b] = -ar[i][b] / ar[a][b];
132+
}
133+
ar[a][b] = 1.0 / ar[a][b];
134+
swap(basis[a], index[b]);
135+
}
136+
inline long double solve(){ /// simplex core
137+
init();
138+
int i, j, k, l;
139+
for (; ;){
140+
for (i = 1, k = 1; i <= m; i++){
141+
if ((ar[i][n] < ar[k][n]) || (ar[i][n] == ar[k][n] && basis[i] < basis[k] && (rand() & 1))) k = i;
142+
}
143+
if (ar[k][n] >= -EPS) break;
144+
145+
for (j = 0, l = 0; j < n; j++){
146+
if ((ar[k][j] < (ar[k][l] - EPS)) || (ar[k][j] < (ar[k][l] - EPS) && index[i] < index[j] && (rand() & 1))){
147+
l = j;
148+
}
149+
}
150+
if (ar[k][l] >= -EPS){
151+
solution_flag = INFEASIBLE; /// No solution is possible
152+
return -1.0;
153+
}
154+
pivot(m, n, k, l);
155+
}
156+
for (; ;){
157+
for (j = 0, l = 0; j < n; j++){
158+
if ((ar[0][j] < ar[0][l]) || (ar[0][j] == ar[0][l] && index[j] < index[l] && (rand() & 1))) l = j;
159+
}
160+
if (ar[0][l] > -EPS) break;
161+
for (i = 1, k = 0; i <= m; i++){
162+
if (ar[i][l] > EPS && (!k || ar[i][n] / ar[i][l] < ar[k][n] / ar[k][l] - EPS || (ar[i][n] / ar[i][l] < ar[k][n] / ar[k][l] + EPS && basis[i] < basis[k]))){
163+
k = i;
164+
}
165+
}
166+
if (ar[k][l] <= EPS){
167+
solution_flag = UNBOUNDED; /// Solution is infinity, no finite solution exists
168+
return -666.0;
169+
}
170+
pivot(m, n, k, l);
171+
}
172+
for (i = 1; i <= m; i++){
173+
if (basis[i] >= 0) val[basis[i]] = ar[i][n];
174+
}
175+
solution_flag = 1; /// Successful completion
176+
return (ar[0][n] * minmax_flag); /// Negate the output for MINIMIZE since the objective function was negated
177+
}
178+
}
179+
long double obj[N],cons[N];
180+
181+
int32_t main() {
182+
ll black = 0;
183+
cin >> n >> k;
184+
for(int i = 0; i < n; i++) cin >> a[i], black += a[i];
185+
for(int i = 1; i < n; i++) {
186+
ll x, y, w; cin >> x >> y >> w;
187+
x--; y--;
188+
g[x].push_back(y);
189+
g[y].push_back(x);
190+
cost[x].push_back(w);
191+
cost[y].push_back(w);
192+
}
193+
for(int i = 0; i < n; i++) dfs(i,-1,0,i);
194+
for(int i = 0; i < n; i++) {
195+
for(int j = 0; j < n; j++) {
196+
if(dis[i][j] <= k )ok[i][j] = 1;
197+
}
198+
}
199+
for(int i = 0; i < n; i++) obj[i] = (long double)1 - a[i];
200+
lp::init(n,obj,MINIMIZE);
201+
for(int i = 0; i < n; i++)cons[i] = 1.0;
202+
lp::add_constraint(cons,black,EQUAL);
203+
for(int i = 0; i < n; i++) {
204+
for(int j = 0; j < n; j++) {
205+
cons[j] = (long double)ok[i][j];
206+
}
207+
lp::add_constraint(cons,1.0,GREATEQ);
208+
}
209+
long double ret = lp::solve();
210+
ll ans = round(ret);
211+
if(ans < 0)ans = -1;
212+
cout<<ans<< '\n';
213+
return 0;
214+
}
215+
// https://codeforces.com/problemset/problem/375/E

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.