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
#include <stdio.h>
#include <stdlib.h>

// C implementation from wikipedia Push-relabel_maximum_flow_algorithm

static int min(int a, int b) {
  return a < b ? a : b;
}

#define INFINITE 1000000000
int NODES;

void push(const int * const * C, int ** F, int * excess, int u, int v) {
  int send = min(excess[u], C[u][v] - F[u][v]);
  F[u][v] += send;
  F[v][u] -= send;
  excess[u] -= send;
  excess[v] += send;
}

void relabel(const int * const * C, const int * const * F, int * height, int u) {
  int min_height = INFINITE;
  for (int v = 0; v < NODES; ++v) {
    if (C[u][v] > F[u][v]) {
      min_height = min(min_height, height[v]);
      height[u] = min_height + 1;
    }
  }
}

void discharge(const int * const * C, int ** F, int * excess, int * height, int * seen, int u) {
  while (excess[u] > 0) {
    if (seen[u] < NODES) {
      int v = seen[u];
      if ((C[u][v] > F[u][v]) && (height[u] > height[v]))
        push(C, F, excess, u, v);
      else
        seen[u] += 1;
    } else {
      relabel(C, F, height, u);
      seen[u] = 0;
    }
  }
}

void moveToFront(int i, int * A) {
  int temp = A[i];
  for (int j = i; j > 0; --j) A[j] = A[j - 1];
  A[0] = temp;
}

int pushRelabel(const int * const * C, int ** F, int source, int sink) {
  int *excess = (int *)calloc(NODES, sizeof(int));
  int *height = (int *)calloc(NODES, sizeof(int));
  int *seen   = (int *)calloc(NODES, sizeof(int));
  int *list   = (int *)calloc((NODES - 2), sizeof(int));

  for (int i = 0, p = 0; i < NODES; ++i)
    if ((i != source) && (i != sink))
      list[p++] = i;

  height[source] = NODES;
  excess[source] = INFINITE;
  for (int i = 0; i < NODES; ++i)
    push(C, F, excess, source, i);

  for (int p = 0; p < NODES - 2; ) {
    int u = list[p];
    int old_height = height[u];
    discharge(C, F, excess, height, seen, u);
    if (height[u] > old_height) {
      moveToFront(p,list);
      p = 0;
    } else
      ++p;
  }
  int maxflow = 0;
  for (int i = 0; i < NODES; ++i)
    maxflow += F[source][i];

  free(list);
  free(seen);
  free(height);
  free(excess);
  return maxflow;
}

int cmp_int(const void * a, const void * b) {
  const int * u = (const int *)a;
  const int * v = (const int *)b;
  if (*u < *v) return -1;
  if (*u > *v) return +1;
  return 0;
}

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

  struct {
    int p, k, c;
  } in[n];
  int T[n + n];
  int sum_c = 0;

  for (int i = 0; i < n; ++i) {
    scanf("%d%d%d", &in[i].p, &in[i].k, &in[i].c);
    T[2 * i + 0] = in[i].p;
    T[2 * i + 1] = in[i].k;
    sum_c += in[i].c;
  }

  qsort(T, n + n, sizeof(T[0]), cmp_int);

  int prev = -1;
  int Tcnt = 0;
  for (int i = 0; i < n + n; ++i)
    if (T[i] != prev) prev = T[Tcnt++] = T[i];

  NODES = 1 + Tcnt-1 + n + 1;
  int source = 0;
  int time0 = 1;
  int task0 = Tcnt;
  int sink = Tcnt + n;

  int **flow = (int **)calloc(NODES, sizeof(int*));
  for (int i = 0; i < NODES; ++i)
    flow[i] = (int *)calloc(NODES, sizeof(int));
  int **capacities = (int **)calloc(NODES, sizeof(int*));
  for (int i = 0; i < NODES; ++i)
    capacities[i] = (int *)calloc(NODES, sizeof(int));

  for (int i = 1; i < Tcnt; ++i) {
    int start = T[i - 1];
    int end = T[i];
    int len = end - start;
    capacities[source][time0 + i-1] += m * len;
  }
  for (int i = 0; i < n; ++i) {
    int p = in[i].p;
    int k = in[i].k;
    int c = in[i].c;
    for (int j = 1; j < Tcnt; ++j) {
      int start = T[j - 1];
      int end = T[j];
      int len = end - start;
      if (start < p) continue;
      if (end > k) continue;
      capacities[time0 + j-1][task0 + i] += len;
    }
    capacities[task0 + i][sink] += c;
  }

  int maxflow = pushRelabel(capacities, flow, source, sink);

  if (maxflow == sum_c)
    printf("TAK\n");
  else
    printf("NIE\n");
  return 0;
}