#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) const int inf = 1e9 + 5; const int nax = 1035; int odl[nax], q[nax]; int m[nax][nax]; vector<int>::iterator pocz[nax]; vector<int> v[nax]; void add(int a, int b, int c) { // printf("%d->%d (%d)\n", a, b, c); v[a].push_back(b); v[b].push_back(a); m[a][b] += c; } bool bst(int s, int t, int n) { FOR(i,0,n) odl[i] = inf; odl[s] = 0; int qbeg = 0, qend = 0; q[qend++] = s; while (qbeg < qend) { int x = q[qbeg++]; for (auto j: v[x]) if (m[x][j] > 0 && odl[j] == inf) { odl[j] = odl[x] + 1; q[qend++] = j; } } return odl[t] != inf; } int flow(int x, int t, int maximum) { if (x == t || maximum == 0) return maximum; int res = 0; for (vector<int>::iterator &it = pocz[x]; it != v[x].end(); it++) if (m[x][*it] > 0 && odl[*it] == odl[x] + 1) { int y = flow(*it,t,min(maximum,m[x][*it])); maximum -= y; m[x][*it] -= y; m[*it][x] += y; res += y; if (maximum == 0) return res; } return res; } int MaxFlow(int s, int t, int n) { int res = 0; while (bst(s,t,n)) { FOR(i,0,n) pocz[i] = v[i].begin(); res += flow(s,t,inf); } return res; } int from[nax], to[nax], req[nax]; int main() { int n, nodes; scanf("%d%d", &n, &nodes); set<int> s; for(int i = 0; i < n; ++i) { scanf("%d%d%d", &from[i], &to[i], &req[i]); s.insert(from[i]); s.insert(to[i]); } vector<int> sorted; for(int x : s) sorted.push_back(x); int S = 1, T = 2; auto getTask = [&](int i) { return 3 + i; }; auto getInterval = [&](int i) { return 3 + n + i; }; for(int i = 0; i < n; ++i) add(S, getTask(i), req[i]); for(int ii = 0; ii + 1 < (int) sorted.size(); ++ii) { int a = sorted[ii], b = sorted[ii+1]; for(int i = 0; i < n; ++i) if(from[i] <= a && b <= to[i]) add(getTask(i), getInterval(ii), b - a); add(getInterval(ii), T, nodes * (b - a)); } int should = 0; for(int i = 0; i < n; ++i) should += req[i]; int result = MaxFlow(S, T, n + (int) sorted.size() + 7); // printf("result = %d\n", result); puts(result == should ? "TAK" : "NIE"); }
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i = (a); i <= (b); ++i) const int inf = 1e9 + 5; const int nax = 1035; int odl[nax], q[nax]; int m[nax][nax]; vector<int>::iterator pocz[nax]; vector<int> v[nax]; void add(int a, int b, int c) { // printf("%d->%d (%d)\n", a, b, c); v[a].push_back(b); v[b].push_back(a); m[a][b] += c; } bool bst(int s, int t, int n) { FOR(i,0,n) odl[i] = inf; odl[s] = 0; int qbeg = 0, qend = 0; q[qend++] = s; while (qbeg < qend) { int x = q[qbeg++]; for (auto j: v[x]) if (m[x][j] > 0 && odl[j] == inf) { odl[j] = odl[x] + 1; q[qend++] = j; } } return odl[t] != inf; } int flow(int x, int t, int maximum) { if (x == t || maximum == 0) return maximum; int res = 0; for (vector<int>::iterator &it = pocz[x]; it != v[x].end(); it++) if (m[x][*it] > 0 && odl[*it] == odl[x] + 1) { int y = flow(*it,t,min(maximum,m[x][*it])); maximum -= y; m[x][*it] -= y; m[*it][x] += y; res += y; if (maximum == 0) return res; } return res; } int MaxFlow(int s, int t, int n) { int res = 0; while (bst(s,t,n)) { FOR(i,0,n) pocz[i] = v[i].begin(); res += flow(s,t,inf); } return res; } int from[nax], to[nax], req[nax]; int main() { int n, nodes; scanf("%d%d", &n, &nodes); set<int> s; for(int i = 0; i < n; ++i) { scanf("%d%d%d", &from[i], &to[i], &req[i]); s.insert(from[i]); s.insert(to[i]); } vector<int> sorted; for(int x : s) sorted.push_back(x); int S = 1, T = 2; auto getTask = [&](int i) { return 3 + i; }; auto getInterval = [&](int i) { return 3 + n + i; }; for(int i = 0; i < n; ++i) add(S, getTask(i), req[i]); for(int ii = 0; ii + 1 < (int) sorted.size(); ++ii) { int a = sorted[ii], b = sorted[ii+1]; for(int i = 0; i < n; ++i) if(from[i] <= a && b <= to[i]) add(getTask(i), getInterval(ii), b - a); add(getInterval(ii), T, nodes * (b - a)); } int should = 0; for(int i = 0; i < n; ++i) should += req[i]; int result = MaxFlow(S, T, n + (int) sorted.size() + 7); // printf("result = %d\n", result); puts(result == should ? "TAK" : "NIE"); } |