#include <iostream> #include <stdio.h> #include <iostream> #include <map> #include <utility> using namespace std; bool debug = true; void displayBase(bool B[], int n) { if (debug) { int i = 0; int j = 0; while (!((j == n) && (i == 0 ))) { cout << B[j * n + i] << ' '; i++; if (i == n) { i = 0; j++; cout << endl; }; }; cout << endl; }; }; void addCol(bool B[], int n, int addThis, int toThis) { int i; for (i = 0; i < n; i++) { B[toThis * n + i] = B[toThis * n + i] ^ B[addThis * n + i]; }; }; void swapCols(bool B[], int n, int x, int y) { for (int i = 0; i < n; i++) { B[x * n + i] = B[x * n + i] ^ B[y * n + i]; B[y * n + i] = B[x * n + i] ^ B[y * n + i]; B[x * n + i] = B[x * n + i] ^ B[y * n + i]; }; }; void cycleInto(bool B[], int n, int what, int where) { for (int i = what - 1; i >= where; i-- ) { swapCols(B, n, i, i+1); }; }; bool gaussianReduce(bool B[], int n, int upTo, int onePos) { // cout << " clearing base up to " << upTo << endl; // displayBase(B, n); while ( onePos < n && !B[upTo * n + onePos]) onePos++; // cout << " one pos is " << onePos << endl; if (onePos == n) return false; int j = 0; int i = 0; while (j < onePos) { if (B[ i * n + j]) i++; j++; }; // cout << " i: " << i << " j: " << j << endl << endl; if ((i == upTo) and (j == onePos )) { return true; } else if ((B[ i * n + j])) { addCol(B, n, i, upTo ); return gaussianReduce(B, n, upTo, onePos); } else { cycleInto(B, n, upTo, i); // cout << "cycled into " << endl; // displayBase(B, n); return true; }; return false; }; // bool gaussianReduce(bool B[], int n, int upTo) { // int i; // int nowReducingRow = 0; // int currentCol = 0; // bool foundOne; // while (nowReducingRow <= upTo and currentCol < n) { // i = nowReducingRow; // foundOne = false; // while ( !foundOne && i <= upTo ) { // if ( B[i * n + currentCol] ) // foundOne = true; // else // i++; // }; // if (foundOne) { // if (i != nowReducingRow) { // swapCols(B, n, i, nowReducingRow); // // if (debug) cout << "current Col is " << currentCol << // // " swapping cols " << i << " and " << nowReducingRow << endl; // // displayBase(B, n); // }; // for (i = nowReducingRow + 1; i <= upTo; i++) { // if ( B[i * n + currentCol] ) { // addCol(B, n, nowReducingRow, i); // // if (debug) cout << "current Col is " << currentCol << // // " adding col " << nowReducingRow << " to " << i << endl; // // displayBase(B, n); // }; // }; // nowReducingRow++; // }; // currentCol++; // }; // bool resu = false; // for (i = 0; i < n; i++) { // resu = resu || B[upTo*n + i]; // }; // // if (resu) { // // if (debug) cout << "Dobra macierz" << endl; // // } else // // if (debug) cout << "ZUAAA macierz" << endl; // return resu; // }; void makeBase(bool B[], int n) { int i = 0; int j = 0; while (!((j == n) && (i == 0))) { B[j * n + i] = 0; i++; if (i == n) { i = 0; j++; }; }; }; void addToBase(bool B[], int n, int whichCol, int from, int to) { for (int i = 0; i < n; i++) B[whichCol * n + i ] = (i >= from) && (i <= to); }; void debugPrint(long C[], int n) { if (debug) { int i = 0; int j = 0; while (!((j == n) && (i == 0 ))) { cout << C[j * n + i] << ' '; i++; if (i == n) { i = 0; j++; cout << endl; }; }; cout << endl; }; }; void findSmallest(long C[], int n, int &x, int &y, int &val) { x = 0; y = 0; val = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i <= j) { if ((val == -1) and not(C[i * n + j] == 0)) { val = C[i * n + j]; x = i; y = j; }; if (C[i * n + j] < val and not(C[i * n + j] == 0)) { val = C[i * n + j]; x = i; y = j; }; }; }; }; }; int main() { bool kurrent; int n; cin >> n; bool B[n * n]; makeBase(B, n); long c; multimap<long, pair<int, int> > m; // read C for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i <= j) { scanf(" %ld", &c); m.insert(pair<long, pair<int, int> >(c, pair<int, int>(i,j))); }; }; }; // cout << "Number of elements with key a: " << m.count("a") << endl; // // int i = 1234; int j = 56789; int cVal = 123121; int noInBase = 0; long long baseCost = 0; for (multimap<long, pair<int, int> >::iterator it = m.begin(); ((noInBase < n) && (it != m.end()) ); ++it) { // if (debug) cout << " [ val: " << (*it).first // << ", from : " << (*it).second.first // << ", to : " << (*it).second.second << "]" << endl; addToBase(B, n, noInBase, (*it).second.first, (*it).second.second); if (gaussianReduce(B, n, noInBase, 0)) { // displayBase(B, n); noInBase++; baseCost += (*it).first; }; } cout << baseCost; };
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | #include <iostream> #include <stdio.h> #include <iostream> #include <map> #include <utility> using namespace std; bool debug = true; void displayBase(bool B[], int n) { if (debug) { int i = 0; int j = 0; while (!((j == n) && (i == 0 ))) { cout << B[j * n + i] << ' '; i++; if (i == n) { i = 0; j++; cout << endl; }; }; cout << endl; }; }; void addCol(bool B[], int n, int addThis, int toThis) { int i; for (i = 0; i < n; i++) { B[toThis * n + i] = B[toThis * n + i] ^ B[addThis * n + i]; }; }; void swapCols(bool B[], int n, int x, int y) { for (int i = 0; i < n; i++) { B[x * n + i] = B[x * n + i] ^ B[y * n + i]; B[y * n + i] = B[x * n + i] ^ B[y * n + i]; B[x * n + i] = B[x * n + i] ^ B[y * n + i]; }; }; void cycleInto(bool B[], int n, int what, int where) { for (int i = what - 1; i >= where; i-- ) { swapCols(B, n, i, i+1); }; }; bool gaussianReduce(bool B[], int n, int upTo, int onePos) { // cout << " clearing base up to " << upTo << endl; // displayBase(B, n); while ( onePos < n && !B[upTo * n + onePos]) onePos++; // cout << " one pos is " << onePos << endl; if (onePos == n) return false; int j = 0; int i = 0; while (j < onePos) { if (B[ i * n + j]) i++; j++; }; // cout << " i: " << i << " j: " << j << endl << endl; if ((i == upTo) and (j == onePos )) { return true; } else if ((B[ i * n + j])) { addCol(B, n, i, upTo ); return gaussianReduce(B, n, upTo, onePos); } else { cycleInto(B, n, upTo, i); // cout << "cycled into " << endl; // displayBase(B, n); return true; }; return false; }; // bool gaussianReduce(bool B[], int n, int upTo) { // int i; // int nowReducingRow = 0; // int currentCol = 0; // bool foundOne; // while (nowReducingRow <= upTo and currentCol < n) { // i = nowReducingRow; // foundOne = false; // while ( !foundOne && i <= upTo ) { // if ( B[i * n + currentCol] ) // foundOne = true; // else // i++; // }; // if (foundOne) { // if (i != nowReducingRow) { // swapCols(B, n, i, nowReducingRow); // // if (debug) cout << "current Col is " << currentCol << // // " swapping cols " << i << " and " << nowReducingRow << endl; // // displayBase(B, n); // }; // for (i = nowReducingRow + 1; i <= upTo; i++) { // if ( B[i * n + currentCol] ) { // addCol(B, n, nowReducingRow, i); // // if (debug) cout << "current Col is " << currentCol << // // " adding col " << nowReducingRow << " to " << i << endl; // // displayBase(B, n); // }; // }; // nowReducingRow++; // }; // currentCol++; // }; // bool resu = false; // for (i = 0; i < n; i++) { // resu = resu || B[upTo*n + i]; // }; // // if (resu) { // // if (debug) cout << "Dobra macierz" << endl; // // } else // // if (debug) cout << "ZUAAA macierz" << endl; // return resu; // }; void makeBase(bool B[], int n) { int i = 0; int j = 0; while (!((j == n) && (i == 0))) { B[j * n + i] = 0; i++; if (i == n) { i = 0; j++; }; }; }; void addToBase(bool B[], int n, int whichCol, int from, int to) { for (int i = 0; i < n; i++) B[whichCol * n + i ] = (i >= from) && (i <= to); }; void debugPrint(long C[], int n) { if (debug) { int i = 0; int j = 0; while (!((j == n) && (i == 0 ))) { cout << C[j * n + i] << ' '; i++; if (i == n) { i = 0; j++; cout << endl; }; }; cout << endl; }; }; void findSmallest(long C[], int n, int &x, int &y, int &val) { x = 0; y = 0; val = -1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i <= j) { if ((val == -1) and not(C[i * n + j] == 0)) { val = C[i * n + j]; x = i; y = j; }; if (C[i * n + j] < val and not(C[i * n + j] == 0)) { val = C[i * n + j]; x = i; y = j; }; }; }; }; }; int main() { bool kurrent; int n; cin >> n; bool B[n * n]; makeBase(B, n); long c; multimap<long, pair<int, int> > m; // read C for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i <= j) { scanf(" %ld", &c); m.insert(pair<long, pair<int, int> >(c, pair<int, int>(i,j))); }; }; }; // cout << "Number of elements with key a: " << m.count("a") << endl; // // int i = 1234; int j = 56789; int cVal = 123121; int noInBase = 0; long long baseCost = 0; for (multimap<long, pair<int, int> >::iterator it = m.begin(); ((noInBase < n) && (it != m.end()) ); ++it) { // if (debug) cout << " [ val: " << (*it).first // << ", from : " << (*it).second.first // << ", to : " << (*it).second.second << "]" << endl; addToBase(B, n, noInBase, (*it).second.first, (*it).second.second); if (gaussianReduce(B, n, noInBase, 0)) { // displayBase(B, n); noInBase++; baseCost += (*it).first; }; } cout << baseCost; }; |