P3265 [JLOI2015]装备购买
线性基可以用高斯消元做,为了简化这一过程,所以有了这样的“基”,如果不存在这样的基,添加即可,否则与当前的基消元。
查看代码
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
| #include <cstdio> #include <vector> #include <algorithm> using namespace std; #define double long double template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const double eps = 1e-8; const int N = 510; struct Node { int c; vector <double> v; bool operator < (const Node &_) const { return c < _.c; } } w[N]; int n, m, p[N]; int sign (double x) { if (x > eps) return 1; if (x < -eps) return -1; return 0; } int main () { read(n), read(m); for (int i = 0; i < n; i++) for (int j = 0, a; j < m; j++) { read(a); w[i].v.push_back(a); } for (int i = 0; i < n; i++) read(w[i].c); sort(w, w + n); for (int i = 0; i < m; i++) p[i] = -1; int cnt = 0, cst = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (!sign(w[i].v[j])) continue; if (p[j] == -1) { p[j] = i; cnt++, cst += w[i].c; break; } else { double t = w[i].v[j] / w[p[j]].v[j]; for (int k = j; k < m; k++) w[i].v[k] -= t * w[p[j]].v[k]; } } printf("%d %d", cnt, cst); return 0; }
|