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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//============================================================================
// Name        : sze.cpp
// Author      : piotr
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================


//---------------max flow Edmonds-Karp algorithm by  George Stoyanov
#include<cstdio>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<iostream>
#include <set>

#define MAX_NODES 330  // the maximum number of nodes in the graph
#define INF 2147483646  // represents infity
#define UNINITIALIZED -1 // value for node with no parent

using namespace std;

// represents the capacities of the edges
int capacities[MAX_NODES][MAX_NODES];
// shows how much flow has passed through an edge
int flowPassed[MAX_NODES][MAX_NODES];
// represents the graph. The graph must contain the negative edges too!
vector<int> graph[MAX_NODES];
// shows the parents of the nodes of the path built by the BFS
int parentsList[MAX_NODES];
// shows the maximum flow to a node in the path built by the BFS
int currentPathCapacity[MAX_NODES];

int bfs(int startNode, int endNode)
{
	memset(parentsList, UNINITIALIZED, sizeof(parentsList));
	memset(currentPathCapacity, 0, sizeof(currentPathCapacity));

	queue<int> q;
	q.push(startNode);

	parentsList[startNode]=-2;
	currentPathCapacity[startNode]=INF;

	while(!q.empty())
	{
		int currentNode = q.front(); q.pop();

		for(int i=0; i<graph[currentNode].size(); i++)
		{
			int to = graph[currentNode][i];
			if(parentsList[to] == UNINITIALIZED)
			{
				if(capacities[currentNode][to] - flowPassed[currentNode][to] > 0)
				{
					// we update the parent of the future node to be the current node
					parentsList[to] = currentNode;

					// we check which is the minimum residual edge capacity so far
					currentPathCapacity[to] = min(currentPathCapacity[currentNode],
							capacities[currentNode][to] - flowPassed[currentNode][to]);

					// if we have reached the end node the bfs should terminate
					if(to == endNode) return currentPathCapacity[endNode];

					// we add our future node to the queue
					q.push(to);
				}
			}
		}
	}

	return 0;
}

int edmondsKarp(int startNode, int endNode)
{
	int maxFlow=0;

	while(true)
	{
		// we find an augmented path and the maximum flow corresponding to it
		int flow=bfs(startNode, endNode);

		// if we can't find anymore paths the flow will be 0
		if(flow==0)
		{
			break;
		}

		maxFlow +=flow;
		int currentNode=endNode;

		// we update the passed flow matrix
		while(currentNode != startNode)
		{
			int previousNode = parentsList[currentNode];
			flowPassed[previousNode][currentNode] += flow;
			flowPassed[currentNode][previousNode] -= flow;

			currentNode=previousNode;
		}
	}

	return maxFlow;
}
//---------------------------------------------------------
int main()
{
	int n,m;
	cin >> n >> m;
	int sumaCzasow=0;
	int p[n+1],k[n+1],c[n+1];

	std::set <int> przedzialy;
	for (int i = 1; i <= n; i++) {
		cin >> p[i] >> k[i] >>c[i];
		przedzialy.insert(p[i]);
		przedzialy.insert(k[i]);
		sumaCzasow+=c[i];

	}
	int liczbaPrzedzialow = przedzialy.size();

	int nodesCount, edgesCount;

	int source, sink;
	source = 0;
	sink = n + liczbaPrzedzialow ;

/*
//--------------------------------------print-----------
	cout << n << " " << m << endl;
	for (int i = 1; i <= n; i++) {
			cout <<  p[i] << " "<< k[i] << " " << c[i] <<endl;
		}

	cout << przedzialy.size() << endl;
	int temp=0;
/*	while (!przedzialy.empty()) {
		temp=*przedzialy.begin();
	    std::cout << ' ' << temp;
	    przedzialy.erase(przedzialy.begin());
	  }
	cout << endl;
	cout << przedzialy.size(); */
//------------------------------------------


//	przydzielenie zadan do source

	for(int i = 1; i<=n;i++) {
		int from, to, capacity;

		from = 0;
		to = i;
		capacity = c[i];

	//	cout << from << " " << to << " " << capacity << endl;

		capacities[from][to]=capacity;
		graph[from].push_back(to);

		//adding the negative edge
		graph[to].push_back(from);

	}

//  dodanie edge

	int poczatekPrzed = *przedzialy.begin();
	int koniecPrzed;
	int dlugoscPrzed;
	przedzialy.erase(przedzialy.begin());

	for (int i = 1; i <= liczbaPrzedzialow - 1; i++) {
		int from, to, capacity;

		koniecPrzed = *przedzialy.begin();
		dlugoscPrzed = koniecPrzed - poczatekPrzed;

		//----------------- dodanie sinku
		from = n + i;
		to = sink;
		capacity = dlugoscPrzed * m;

		//cout << from << " " << to << " " << capacity << endl;

		capacities[from][to]=capacity;
		graph[from].push_back(to);

		//adding the negative edge
		graph[to].push_back(from);
		//---------------------------------

		for( int j = 1; j <= n ;j++) {

			if(p[j] <= poczatekPrzed && k[j] >= koniecPrzed) {


				//----------------- dodanie edge
				from = j;
				to = n+i;
				capacity = dlugoscPrzed;

			//	cout << from << " " << to << " " << capacity << endl;

				capacities[from][to]=capacity;
				graph[from].push_back(to);

				//adding the negative edge
				graph[to].push_back(from);
				//---------------------------------

			}
		}




		poczatekPrzed = koniecPrzed;
		przedzialy.erase(przedzialy.begin());
	}














//	cin>>nodesCount>>edgesCount;


//	cin>>source>>sink;
/*
	for(int edge=0; edge<edgesCount; edge++)
	{
		int from, to, capacity;
		cin>>from>>to>>capacity;

		capacities[from][to]=capacity;
		graph[from].push_back(to);

		//adding the negative edge
		graph[to].push_back(from);
	}
*/
	int maxFlow = edmondsKarp(source, sink);


//	cout<<maxFlow<<endl;
//	cout<< sumaCzasow<< endl;
	if (sumaCzasow == maxFlow) {
		cout << "TAK" << endl;
	}
	else cout << "NIE" << endl;

	return 0;
}