Topic links
UVA10498
Exercises
Template questions
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <cmath> #include <map> #define LL Long Long Int#define REP (i,n) for (int i = 1; I <= (n), i++) #define Redge (U) for (int k = h[u],to; k; k = ed[k].nxt) #define CLS (s , V) memset (s,v,sizeof (s)) #define MP (A, b) make_pair<int,int> (A, b) #define CP pair<int,int>using namespace std;const int MAXN = 25,MAXM = 100005;const double eps = 1e-12,inf = 1e15;int n,m;double a[maxn][maxn];void Pivot (int l,in T 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 (l! = i && fabs (a[i][e]) > 0) {t = a[i][e]; A[i][e] = 0; for (int j = 0; J <= N; j + +) A[i][j]-= a[l][j] * t; }}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 () {while (~scanf ("%d%d", &n,&m)) {REP (j,n) scanf ("%lf", &a[0][j]); Rep (I,m) {rep (j,n) scanf ("%lf", &a[i][j]); scanf ("%lf", &a[i][0]); } A[0][0] = 0; Simplex (); printf ("Nasa can spend%lld taka.\n", (LL) floor ((-a[0][0]) * m + 1-eps)); } return 0;}
UVA10498 Happiness "simplex"