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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <cstdio>
#include <climits>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

#define DBG(X)

struct flow_graph {
	int MAX_V, E, s, t;
	int *cap, *to, *next, *last;
	bool *visited;

	flow_graph(int V, int MAX_E) {
		MAX_V = V; E = 0;
		cap = new int[2 * MAX_E], to = new int[2 * MAX_E], next = new int[2 * MAX_E];
		last = new int[MAX_V], visited = new bool[MAX_V];
		fill(last, last + MAX_V, -1);
	}

	void clear() {
		fill(last, last + MAX_V, -1);
		E = 0;
	}

	void add_edge(int u, int v, int uv, int vu = 0) {
		to[E] = v, cap[E] = uv, next[E] = last[u]; last[u] = E++;
		to[E] = u, cap[E] = vu, next[E] = last[v]; last[v] = E++;
		DBG(printf("Add edge %d->%d cap: %d\n", u, v, uv));
	}

	int dfs(int v, int f) {
		if (v == t || f <= 0) return f;
		if (visited[v]) return 0;
		visited[v] = true;

		for (int e = last[v]; e != -1; e = next[e]) {
			int ret = dfs(to[e], min(f, cap[e]));

			if (ret>0) {
				cap[e] -= ret;
				cap[e ^ 1] += ret;
				return ret;
			}
		}

		return 0;
	}

	int max_flow(int source, int sink) {
		s = source, t = sink;
		int f = 0, x;

		while (true) {
			fill(visited, visited + MAX_V, false);
			x = dfs(s, INT_MAX);
			if (x == 0) break;
			f += x;
		}

		return f;
	}
};


typedef vector<int> VI;
typedef vector<VI> VVI;

const int INF = 1000000000;

struct MaxFlow {
	int N;
	VVI cap, flow;
	VI dad, Q;

	MaxFlow(int N) :
		N(N), cap(N, VI(N)), flow(N, VI(N)), dad(N), Q(N) {}

	void AddEdge(int from, int to, int cap) {
		this->cap[from][to] += cap;
	}

	void add_edge(int from, int to, int cap) {
		AddEdge(from, to, cap);
	}

	int BlockingFlow(int s, int t) {
		fill(dad.begin(), dad.end(), -1);
		dad[s] = -2;

		int head = 0, tail = 0;
		Q[tail++] = s;
		while (head < tail) {
			int x = Q[head++];
			for (int i = 0; i < N; i++) {
				if (dad[i] == -1 && cap[x][i] - flow[x][i] > 0) {
					dad[i] = x;
					Q[tail++] = i;
				}
			}
		}

		if (dad[t] == -1) return 0;

		int totflow = 0;
		for (int i = 0; i < N; i++) {
			if (dad[i] == -1) continue;
			int amt = cap[i][t] - flow[i][t];
			for (int j = i; amt && j != s; j = dad[j])
				amt = min(amt, cap[dad[j]][j] - flow[dad[j]][j]);
			if (amt == 0) continue;
			flow[i][t] += amt;
			flow[t][i] -= amt;
			for (int j = i; j != s; j = dad[j]) {
				flow[dad[j]][j] += amt;
				flow[j][dad[j]] -= amt;
			}
			totflow += amt;
		}

		return totflow;
	}

	int GetMaxFlow(int source, int sink) {
		int totflow = 0;
		while (int flow = BlockingFlow(source, sink))
			totflow += flow;
		return totflow;
	}

	int max_flow(int source, int sink) {
		return GetMaxFlow(source, sink);
	}
};

int czas_trwania_wszystkich_zadan = 0;
vector<pair<int, pair<int, int> > > jobs;
set<int> points;
vector<pair<int, int> > intervals;

flow_graph* G_max_flow_first_impl;
MaxFlow * G_max_flow_second_impl;

void init_graph(int n) {
	//G_max_flow_first_impl = new flow_graph(n, n*n);
	G_max_flow_second_impl = new MaxFlow(n);
}

void add_edge(int u, int v, int cap) {
	if (G_max_flow_first_impl) {
		G_max_flow_first_impl->add_edge(u, v, cap);
	}
	else {
		G_max_flow_second_impl->add_edge(u, v, cap);
	}
}

int get_max_flow(int source, int sink) {
	if (G_max_flow_first_impl) {
		return G_max_flow_first_impl->max_flow(source, sink);
	}
	else {
		return G_max_flow_second_impl->max_flow(source, sink);
	}
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);


	for (int i = 0; i < n; i++) {
		int poczatek, koniec, dlugosc;
		scanf("%d%d%d", &poczatek, &koniec, &dlugosc);
		jobs.push_back(make_pair(dlugosc, make_pair(poczatek, koniec)));
		czas_trwania_wszystkich_zadan += dlugosc;
		points.insert(poczatek);
		points.insert(koniec);
	}
	int last_point = 0;
	for (set<int>::iterator it = points.begin(); it != points.end(); it++) {
		int p = *it;
		if (p != last_point) {
			intervals.push_back(make_pair(last_point, p));
			last_point = p;
		}
	}
#ifdef K3_DEBUG
	printf("Intervals:\n");
	for (int i = 0; i < intervals.size(); i++) {
		printf("[%d, %d),", intervals[i].first, intervals[i].second);
	}
	printf("\n");
	printf("Dlugosc wszystkich zadan: %d\n", czas_trwania_wszystkich_zadan);
#endif
	// wierzchołoek 0 -
	// layer 1 - wierzcholki reprezentujace Zadania
	// layer 2 - wierzcholki reprezentujace przedzialy
	// vierzcholek ostatni
	// razem: 1 + jobs.size() + intervals.size() + 1
	int graph_n = 1 + jobs.size() + intervals.size() + 1;
	int Sv = 0, Tv = graph_n - 1;
	//flow_graph G(graph_n, graph_n * graph_n);
	//G.clear();

	init_graph(graph_n);

	for (int i = 0; i < n; i++) {
		int release_date = jobs[i].second.first;
		int due_date = jobs[i].second.second;
		int dlugosc_zadania = jobs[i].first;
		int jv = i + 1;
		DBG(printf("Job %d: relase %d due %d, dlugosc %d, jv=%d\n", i, release_date, due_date, dlugosc_zadania, jv));
		add_edge(Sv, jv, dlugosc_zadania);
		for (int k = 0; k < intervals.size(); k++) {
			int b = intervals[k].first;
			int e = intervals[k].second;
			int interval_v = n + 1 + k;
			int interval_len = e - b;
			if (release_date <= b && e <= due_date) {
				DBG(printf("Node->Interval edge [%d, %d) interval_len=%d cap: %d\n", b, e, interval_len, interval_len));
				add_edge(jv, interval_v, interval_len);
			}
			DBG(printf("Interval->Target edge %d -> %d cap; %d\n", interval_v, Tv, m * interval_len));
		}
	}
	for (int k = 0; k < intervals.size(); k++) {
		int interval_v = n + 1 + k;
		int b = intervals[k].first;
		int e = intervals[k].second;
		int interval_len = e - b;
		add_edge(interval_v, Tv, m * interval_len);
	}
	int max_flow_capacity = get_max_flow(Sv, Tv);

	DBG(printf("%d\n", max_flow_capacity));

	printf((max_flow_capacity >= czas_trwania_wszystkich_zadan) ? "TAK" : "NIE");

	return 0;
}

/*
3 2
3 8 3
2 5 2
3 7 3


*/