/* * Użyłem implementacji wyznaczania maksymalnego przepływu ze strony: * https://sites.google.com/site/indy256/algo/preflow */ #define MP make_pair #define ST first #define ND second #include <cstdio> #include <algorithm> using namespace std; const int MAX_INT = 1e9; const int MAX_N = 407; int capacity[MAX_N][MAX_N]; pair <int, int> task[MAX_N], interval[MAX_N]; int input[MAX_N], workTime[MAX_N]; int n, m, sum; bool workOnTaskPossible(pair <int, int> t, pair <int, int> i) { return t.ST <= i.ST && i.ND <= t.ND; } int maxFlow(int s, int t) { int N = 3 * n + 1; int h[MAX_N], maxh[MAX_N], e[MAX_N]; int f[MAX_N][MAX_N]; h[s] = N - 1; for (int i = 0; i < N; ++i) { f[s][i] = capacity[s][i]; f[i][s] = -f[s][i]; e[i] = capacity[s][i]; } int sz = 0; while (true) { if (sz == 0) { for (int i = 0; i < N; ++i) { if (i != s && i != t && e[i] > 0) { if (sz != 0 && h[i] > h[maxh[0]]) { sz = 0; } maxh[sz++] = i; } } } if (sz == 0) { break; } while (sz != 0) { int i = maxh[sz - 1]; bool pushed = false; for (int j = 0; j < N && e[i] != 0; ++j) { if (h[i] == h[j] + 1 && capacity[i][j] - f[i][j] > 0) { int df = min(capacity[i][j] - f[i][j], e[i]); f[i][j] += df; f[j][i] -= df; e[i] -= df; e[j] += df; if (e[i] == 0) { --sz; } pushed = true; } } if (!pushed) { h[i] = MAX_INT; for (int j = 0; j < N; ++j) { if (h[i] > h[j] + 1 && capacity[i][j] - f[i][j] > 0) { h[i] = h[j] + 1; } } if (h[i] > h[maxh[0]]) { sz = 0; break; } } } } int flow = 0; for (int i = 0; i < N; i++) { flow += f[s][i]; } return flow; } void readInput() { scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d %d %d", &task[i].ST, &task[i].ND, &workTime[i]); input[2 * i] = task[i].ST; input[2 * i + 1] = task[i].ND; sum += workTime[i]; } } void createIntervals() { sort(input, input + 2 * n); for (int i = 0; i < 2 * n - 1; ++i) { interval[i + 1] = MP(input[i], input[i + 1]); } } void createGraph() { for (int i = 1; i <= 2 * n - 1; ++i) { capacity[0][i] = (interval[i].ND - interval[i].ST) * m; } for (int i = 1; i <= 2 * n - 1; ++i) { for (int j = 2 * n; j < 3 * n; ++j) { if (workOnTaskPossible(task[j - 2 * n], interval[i])) { capacity[i][j] = interval[i].ND - interval[i].ST; } } } for (int i = 2 * n; i < 3 * n; ++i) { capacity[i][3 * n] = workTime[i - 2 * n]; } } void printResult() { printf(maxFlow(0, 3 * n) >= sum ? "TAK\n" : "NIE\n"); } int main() { readInput(); createIntervals(); createGraph(); printResult(); 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 | /* * Użyłem implementacji wyznaczania maksymalnego przepływu ze strony: * https://sites.google.com/site/indy256/algo/preflow */ #define MP make_pair #define ST first #define ND second #include <cstdio> #include <algorithm> using namespace std; const int MAX_INT = 1e9; const int MAX_N = 407; int capacity[MAX_N][MAX_N]; pair <int, int> task[MAX_N], interval[MAX_N]; int input[MAX_N], workTime[MAX_N]; int n, m, sum; bool workOnTaskPossible(pair <int, int> t, pair <int, int> i) { return t.ST <= i.ST && i.ND <= t.ND; } int maxFlow(int s, int t) { int N = 3 * n + 1; int h[MAX_N], maxh[MAX_N], e[MAX_N]; int f[MAX_N][MAX_N]; h[s] = N - 1; for (int i = 0; i < N; ++i) { f[s][i] = capacity[s][i]; f[i][s] = -f[s][i]; e[i] = capacity[s][i]; } int sz = 0; while (true) { if (sz == 0) { for (int i = 0; i < N; ++i) { if (i != s && i != t && e[i] > 0) { if (sz != 0 && h[i] > h[maxh[0]]) { sz = 0; } maxh[sz++] = i; } } } if (sz == 0) { break; } while (sz != 0) { int i = maxh[sz - 1]; bool pushed = false; for (int j = 0; j < N && e[i] != 0; ++j) { if (h[i] == h[j] + 1 && capacity[i][j] - f[i][j] > 0) { int df = min(capacity[i][j] - f[i][j], e[i]); f[i][j] += df; f[j][i] -= df; e[i] -= df; e[j] += df; if (e[i] == 0) { --sz; } pushed = true; } } if (!pushed) { h[i] = MAX_INT; for (int j = 0; j < N; ++j) { if (h[i] > h[j] + 1 && capacity[i][j] - f[i][j] > 0) { h[i] = h[j] + 1; } } if (h[i] > h[maxh[0]]) { sz = 0; break; } } } } int flow = 0; for (int i = 0; i < N; i++) { flow += f[s][i]; } return flow; } void readInput() { scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d %d %d", &task[i].ST, &task[i].ND, &workTime[i]); input[2 * i] = task[i].ST; input[2 * i + 1] = task[i].ND; sum += workTime[i]; } } void createIntervals() { sort(input, input + 2 * n); for (int i = 0; i < 2 * n - 1; ++i) { interval[i + 1] = MP(input[i], input[i + 1]); } } void createGraph() { for (int i = 1; i <= 2 * n - 1; ++i) { capacity[0][i] = (interval[i].ND - interval[i].ST) * m; } for (int i = 1; i <= 2 * n - 1; ++i) { for (int j = 2 * n; j < 3 * n; ++j) { if (workOnTaskPossible(task[j - 2 * n], interval[i])) { capacity[i][j] = interval[i].ND - interval[i].ST; } } } for (int i = 2 * n; i < 3 * n; ++i) { capacity[i][3 * n] = workTime[i - 2 * n]; } } void printResult() { printf(maxFlow(0, 3 * n) >= sum ? "TAK\n" : "NIE\n"); } int main() { readInput(); createIntervals(); createGraph(); printResult(); return 0; } |