BZOJ3550 [ONTAK2010]Vacation 【单纯形】
题目链接
题解
单纯形裸题
题意不清,每个位置最多选一次
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<ctime>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1005,maxm = 100005;
const double eps = 1e-8,INF = 1e15;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,N,K;
double a[maxn][maxn];
void Pivot(int l,int e){
double t = a[l][e]; a[l][e] = 1;
for (int j = 0; j <= n; j++) a[l][j] /= t;
for (int i = 0; i <= m; i++) if (i != l && fabs(a[i][e]) > eps){
t = a[i][e]; a[i][e] = 0;
for (int j = 0; j <= n; j++) a[i][j] -= a[l][j] * t;
}
}
void init(){
while (true){
int e = 0,l = 0;
for (int i = 1; i <= m; i++) if (a[i][0] < -eps && (!l || (rand() & 1))) l = i;
if (!l) break;
for (int j = 1; j <= n; j++) if (a[l][j] < -eps && (!e || (rand() & 1))) e = j;
Pivot(l,e);
}
}
void simplex(){
while (true){
int l = 0,e = 0; double mn = 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] < mn)
mn = a[i][0] / a[i][e],l = i;
Pivot(l,e);
}
}
int main(){
srand(time(NULL));
N = read(); n = 3 * N; K = read();
int tmp = (N << 1 | 1); m = tmp + n;
for (int j = 1; j <= n; j++){
a[0][j] = read();
a[tmp + j][j] = 1;
a[tmp + j][0] = 1;
}
for (int j = 1; j <= n; j++){
int E = min(j,tmp);
for (int i = max(1,j - N + 1); i <= E; i++)
a[i][j] = 1;
}
for (int i = 1; i <= tmp; i++) a[i][0] = K;
init(); simplex();
printf("%lld\n",(LL)(-a[0][0] + 0.5));
return 0;
}