#include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> using namespace std; #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; // ======== PUSH-RELABEL by Adam Polak ======== // maksymalny przeplyw w O(V^3) const int N = 500 + 10; struct edge { int v; LL cap, flow; int back_ind; edge *back; edge(int vi, LL ci) : v(vi), cap(ci) {} }; /* Usage: 1) n=...; s=...; t=...; 2) REP(i,n) g[i].clear(); 3) add_edge(...); 4) compute_flow(); */ int n, s, t; LL e[N], h[N]; vector<edge> g[N]; vector<edge>::iterator cur[N]; void bfs(int start, int start_h) { queue<int> q; h[start] = start_h; for (q.push(start); !q.empty(); q.pop()) { int u = q.front(); for (auto i : g[u]) if (i.back->flow < i.back->cap && h[i.v] > h[u] + 1) { h[i.v] = h[u] + 1; q.push(i.v); } } } LL compute_flow() { queue<int> q; REP(i, n) { for (auto &j : g[i]) { j.flow = 0; j.back = &g[j.v][j.back_ind]; } cur[i] = g[i].begin(); h[i] = e[i] = 0; } for (auto &i : g[s]) { i.flow = i.cap; i.back->flow = -i.flow; if (e[i.v] == 0 && i.v != t) q.push(i.v); e[i.v] += i.flow; } h[s] = n; int relabel_counter = 0; for (; !q.empty(); q.pop()) { int u = q.front(); while (e[u] > 0) { if (cur[u] == g[u].end()) { // relabel relabel_counter++; h[u] = 2 * n + 1; for (auto i : g[u]) if (i.flow < i.cap) h[u] = min(h[u], h[i.v] + 1); cur[u] = g[u].begin(); continue; } if (cur[u]->flow < cur[u]->cap && h[u] == h[cur[u]->v] + 1) { // push LL d = min(e[u], cur[u]->cap - cur[u]->flow); cur[u]->flow += d; cur[u]->back->flow -= d; e[u] -= d; e[cur[u]->v] += d; if (e[cur[u]->v] == d && cur[u]->v != t && cur[u]->v != s) q.push(cur[u]->v); } else cur[u]++; } if (relabel_counter >= n) { REP(i, n) h[i] = 2 * n + 1; bfs(t, 0); bfs(s, n); relabel_counter = 0; } } return e[t]; } void add_edge(int a, int b, LL c, int c_back = 0) { assert(a != b); // NIE wrzucac petelek! g[a].push_back(edge(b, c)); g[b].push_back(edge(a, c_back)); g[a].back().back_ind = g[b].size() - 1; g[b].back().back_ind = g[a].size() - 1; } #define MR 110 // zadania pair < int, pair<int, int>> tasks[MR]; bool can(int wsk, int beg, int end) { // czy mozna wykonywac zadanie wsk w przedziale [beg,end] // nie wykonac!! // jesli zaczyna sie lub konczy poza przedzialem, to nie mozna if (tasks[wsk].second.first >= end || tasks[wsk].second.second <= beg) return 0; return 1; } int main() { int z, m; scanf("%d%d", &z, &m); // posortuj zdarzenia set<int> S; REP(i, z) { scanf("%d%d%d", &tasks[i].second.first, &tasks[i].second.second, &tasks[i].first); S.insert(tasks[i].second.first); S.insert(tasks[i].second.second); } // buduj graf n = z + S.size() - 1 + 2; s = 0; t = n - 1; // czasy wykonania zadan int sum = 0; REP(i, z) { add_edge(s, i + 1, tasks[i].first); sum += tasks[i].first; } int last = z; for (auto it = S.begin(); it != S.end(); it++) { auto itn = it; itn++; if (itn == S.end()) break; // kolejny wierzcholek do zrobienia last++; int beg = *it, end = *itn; int tk = end - beg; add_edge(last, t, m*tk); REP(i, z) if (can(i, beg, end)) add_edge(i + 1, last, tk); else add_edge(i + 1, last, 0); } if (compute_flow() == sum) printf("TAK\n"); else printf("NIE\n"); 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 179 180 181 182 183 184 185 186 187 | #include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> using namespace std; #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; // ======== PUSH-RELABEL by Adam Polak ======== // maksymalny przeplyw w O(V^3) const int N = 500 + 10; struct edge { int v; LL cap, flow; int back_ind; edge *back; edge(int vi, LL ci) : v(vi), cap(ci) {} }; /* Usage: 1) n=...; s=...; t=...; 2) REP(i,n) g[i].clear(); 3) add_edge(...); 4) compute_flow(); */ int n, s, t; LL e[N], h[N]; vector<edge> g[N]; vector<edge>::iterator cur[N]; void bfs(int start, int start_h) { queue<int> q; h[start] = start_h; for (q.push(start); !q.empty(); q.pop()) { int u = q.front(); for (auto i : g[u]) if (i.back->flow < i.back->cap && h[i.v] > h[u] + 1) { h[i.v] = h[u] + 1; q.push(i.v); } } } LL compute_flow() { queue<int> q; REP(i, n) { for (auto &j : g[i]) { j.flow = 0; j.back = &g[j.v][j.back_ind]; } cur[i] = g[i].begin(); h[i] = e[i] = 0; } for (auto &i : g[s]) { i.flow = i.cap; i.back->flow = -i.flow; if (e[i.v] == 0 && i.v != t) q.push(i.v); e[i.v] += i.flow; } h[s] = n; int relabel_counter = 0; for (; !q.empty(); q.pop()) { int u = q.front(); while (e[u] > 0) { if (cur[u] == g[u].end()) { // relabel relabel_counter++; h[u] = 2 * n + 1; for (auto i : g[u]) if (i.flow < i.cap) h[u] = min(h[u], h[i.v] + 1); cur[u] = g[u].begin(); continue; } if (cur[u]->flow < cur[u]->cap && h[u] == h[cur[u]->v] + 1) { // push LL d = min(e[u], cur[u]->cap - cur[u]->flow); cur[u]->flow += d; cur[u]->back->flow -= d; e[u] -= d; e[cur[u]->v] += d; if (e[cur[u]->v] == d && cur[u]->v != t && cur[u]->v != s) q.push(cur[u]->v); } else cur[u]++; } if (relabel_counter >= n) { REP(i, n) h[i] = 2 * n + 1; bfs(t, 0); bfs(s, n); relabel_counter = 0; } } return e[t]; } void add_edge(int a, int b, LL c, int c_back = 0) { assert(a != b); // NIE wrzucac petelek! g[a].push_back(edge(b, c)); g[b].push_back(edge(a, c_back)); g[a].back().back_ind = g[b].size() - 1; g[b].back().back_ind = g[a].size() - 1; } #define MR 110 // zadania pair < int, pair<int, int>> tasks[MR]; bool can(int wsk, int beg, int end) { // czy mozna wykonywac zadanie wsk w przedziale [beg,end] // nie wykonac!! // jesli zaczyna sie lub konczy poza przedzialem, to nie mozna if (tasks[wsk].second.first >= end || tasks[wsk].second.second <= beg) return 0; return 1; } int main() { int z, m; scanf("%d%d", &z, &m); // posortuj zdarzenia set<int> S; REP(i, z) { scanf("%d%d%d", &tasks[i].second.first, &tasks[i].second.second, &tasks[i].first); S.insert(tasks[i].second.first); S.insert(tasks[i].second.second); } // buduj graf n = z + S.size() - 1 + 2; s = 0; t = n - 1; // czasy wykonania zadan int sum = 0; REP(i, z) { add_edge(s, i + 1, tasks[i].first); sum += tasks[i].first; } int last = z; for (auto it = S.begin(); it != S.end(); it++) { auto itn = it; itn++; if (itn == S.end()) break; // kolejny wierzcholek do zrobienia last++; int beg = *it, end = *itn; int tk = end - beg; add_edge(last, t, m*tk); REP(i, z) if (can(i, beg, end)) add_edge(i + 1, last, tk); else add_edge(i + 1, last, 0); } if (compute_flow() == sum) printf("TAK\n"); else printf("NIE\n"); return 0; } |