#include <stdio.h> #include <algorithm> #include <list> struct Position { int x; int y; Position() { } Position(int _x, int _y) : x(_x), y(_y) { } }; struct Cell { int cost; bool done; }; // GLOBALS int n; Cell** costs; Position* positions; int allCosts; int markedCups; long long int total; std::list<Position> queue; // FUNCS bool mySort(const Position& p1, const Position& p2) { if (costs[p1.y][p1.x].cost == costs[p2.y][p2.x].cost) return p1.x - p1.y < p2.x - p2.y; else return costs[p1.y][p1.x].cost < costs[p2.y][p2.x].cost; } void markDone(int p1x, int p1y, int p2x, int p2y, int p3x, int p3y) { Cell& c1 = costs[p1y][p1x]; Cell& c2 = costs[p2y][p2x]; Cell& c3 = costs[p3y][p3x]; if (c1.done && c2.done && !c3.done) { c3.done = true; //printf("MARK: %d %d\n", p3x, p3y); queue.push_back(Position(p3x, p3y)); if (p3x == p3y) markedCups++; } else if (c1.done && !c2.done && c3.done) { c2.done = true; //printf("MARK: %d %d\n", p2x, p2y); queue.push_back(Position(p2x, p2y)); if (p2x == p2y) markedCups++; } else if (!c1.done && c2.done && c3.done) { c1.done = true; //printf("MARK: %d %d\n", p1x, p1y); queue.push_back(Position(p1x, p1y)); if (p1x == p1y) markedCups++; } } void process(int px, int py) { //printf("PROCESS: %d %d\n", px, py); costs[py][px].done = true; for (int x = py; x < n; ++x) { if (x < px) { //int sy = markDone(x, py, px, py, px, py + x - py + 1); } else if (x > px) { markDone(px, py, x, py, x, py + px - py + 1); } } for (int y = 0; y < px; ++y) { if (y < py) { markDone(px, py, px, y, py - 1, y); } } /*for (int i = 0; i < n; ++i) { for (int j = 0; j < n; j++) printf("[%d-%d] ", costs[i][j].cost, costs[i][j].done); printf("\n"); }*/ } int main() { scanf("%d", &n); allCosts = (n + 1) * n / 2; costs = new Cell*[n]; positions = new Position[allCosts]; int positionIndex = 0; for (int i = 0; i < n; ++i) { costs[i] = new Cell[n]; for (int j = 0; j < n; j++) { if (j < i) costs[i][j].cost = 0; else { scanf("%d", &(costs[i][j].cost)); positions[positionIndex].x = j; positions[positionIndex].y = i; positionIndex++; } costs[i][j].done = false; } } std::sort(positions, positions + allCosts, mySort); markedCups = 0; total = 0; for (int i = 0 ; i < allCosts; i++) { int px = positions[i].x; int py = positions[i].y; if (costs[py][px].done) continue; //printf("-----------------------------------------------------------\n"); total += (long long int)costs[py][px].cost; process(px, py); while (!queue.empty()) { Position& cell = queue.front(); process(cell.x, cell.y); queue.pop_front(); } if (markedCups == n) break; } printf("%lld", total); delete[] positions; for (int i = 0; i < n; ++i) delete[] costs[i]; delete[] costs; return 0; }
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 | #include <stdio.h> #include <algorithm> #include <list> struct Position { int x; int y; Position() { } Position(int _x, int _y) : x(_x), y(_y) { } }; struct Cell { int cost; bool done; }; // GLOBALS int n; Cell** costs; Position* positions; int allCosts; int markedCups; long long int total; std::list<Position> queue; // FUNCS bool mySort(const Position& p1, const Position& p2) { if (costs[p1.y][p1.x].cost == costs[p2.y][p2.x].cost) return p1.x - p1.y < p2.x - p2.y; else return costs[p1.y][p1.x].cost < costs[p2.y][p2.x].cost; } void markDone(int p1x, int p1y, int p2x, int p2y, int p3x, int p3y) { Cell& c1 = costs[p1y][p1x]; Cell& c2 = costs[p2y][p2x]; Cell& c3 = costs[p3y][p3x]; if (c1.done && c2.done && !c3.done) { c3.done = true; //printf("MARK: %d %d\n", p3x, p3y); queue.push_back(Position(p3x, p3y)); if (p3x == p3y) markedCups++; } else if (c1.done && !c2.done && c3.done) { c2.done = true; //printf("MARK: %d %d\n", p2x, p2y); queue.push_back(Position(p2x, p2y)); if (p2x == p2y) markedCups++; } else if (!c1.done && c2.done && c3.done) { c1.done = true; //printf("MARK: %d %d\n", p1x, p1y); queue.push_back(Position(p1x, p1y)); if (p1x == p1y) markedCups++; } } void process(int px, int py) { //printf("PROCESS: %d %d\n", px, py); costs[py][px].done = true; for (int x = py; x < n; ++x) { if (x < px) { //int sy = markDone(x, py, px, py, px, py + x - py + 1); } else if (x > px) { markDone(px, py, x, py, x, py + px - py + 1); } } for (int y = 0; y < px; ++y) { if (y < py) { markDone(px, py, px, y, py - 1, y); } } /*for (int i = 0; i < n; ++i) { for (int j = 0; j < n; j++) printf("[%d-%d] ", costs[i][j].cost, costs[i][j].done); printf("\n"); }*/ } int main() { scanf("%d", &n); allCosts = (n + 1) * n / 2; costs = new Cell*[n]; positions = new Position[allCosts]; int positionIndex = 0; for (int i = 0; i < n; ++i) { costs[i] = new Cell[n]; for (int j = 0; j < n; j++) { if (j < i) costs[i][j].cost = 0; else { scanf("%d", &(costs[i][j].cost)); positions[positionIndex].x = j; positions[positionIndex].y = i; positionIndex++; } costs[i][j].done = false; } } std::sort(positions, positions + allCosts, mySort); markedCups = 0; total = 0; for (int i = 0 ; i < allCosts; i++) { int px = positions[i].x; int py = positions[i].y; if (costs[py][px].done) continue; //printf("-----------------------------------------------------------\n"); total += (long long int)costs[py][px].cost; process(px, py); while (!queue.empty()) { Position& cell = queue.front(); process(cell.x, cell.y); queue.pop_front(); } if (markedCups == n) break; } printf("%lld", total); delete[] positions; for (int i = 0; i < n; ++i) delete[] costs[i]; delete[] costs; return 0; } |