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
#include <cstdlib>
#include <cstdio>
#include<algorithm>
using namespace std;
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define INFINITE 1000000001
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 v;
  int min_height = INFINITE;
  for (v = 0; v < NODES; v++) {
    if (C[u][v] - F[u][v] > 0) {
      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] > 0) && (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];
  int n;
  for (n = i; n > 0; n--){
    A[n] = A[n-1];
  }
  A[0] = temp;
}

int pushRelabel(const int * const * C, int ** F, int source, int sink) {
  int *excess, *height, *list, *seen, i, p;

  excess = (int *) calloc(NODES, sizeof(int));
  height = (int *) calloc(NODES, sizeof(int));
  seen = (int *) calloc(NODES, sizeof(int));

  list = (int *) calloc((NODES-2), sizeof(int));

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

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

  p = 0;
  while (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 += 1;
  }
  int maxflow = 0;
  for (i = 0; i < NODES; i++)
    maxflow += F[source][i];

  free(list);

  free(seen);
  free(height);
  free(excess);

  return maxflow;
}

void printMatrix(const int * const * M) {
  int i,j;
  for (i = 0; i < NODES; i++) {
    for (j = 0; j < NODES; j++)
      printf("%d\t",M[i][j]);
    printf("\n");
  }
}

struct tri
{
    int p,k,d;
};
inline int prze(const int p1,const int k1,const int p2,const int k2)
{
    return MAX(MIN(k1,k2)-MAX(p1,p2),0);
}
int main() {
    int i,j,n,m,kol[220];
    scanf("%d%d",&n,&m);
    tri b[111];
    for(i=0;i<n;++i)
    {
        scanf("%d%d%d",&b[i].p,&b[i].k,&b[i].d);
        kol[2*i]=b[i].p;
        kol[2*i+1]=b[i].k;
    }
    sort(kol,kol+2*n);
    NODES=3*n+1;
    int **flow, **cap;
    flow = (int **) calloc(NODES, sizeof(int*));
    cap = (int **) calloc(NODES, sizeof(int*));
    for (i = 0; i < NODES; i++) {
        flow[i] = (int *) calloc(NODES, sizeof(int));
        cap[i] = (int *) calloc(NODES, sizeof(int));
    }
    //pus pus pusen
    //pusatko
    //pusabzuch
    //boli
    //socek
    //meow meow
    //dobre pus?
    for(i=0;i<n;++i)
    {
        cap[0][i+1]=b[i].d;
        for(j=1;j<2*n;++j)
            cap[i+1][n+j]=prze(kol[j-1],kol[j],b[i].p,b[i].k);
    }
    for(j=1;j<2*n;++j)
        cap[n+j][3*n]=(kol[j]-kol[j-1])*m;
  //  printf("Capacity:\n");
//    printMatrix(cap);
    int w=0;
    for(i=0;i<n;++i)
        w+=b[i].d;
//    printf("%d ",pushRelabel(cap, flow, 0, 3*n));
    printf(pushRelabel(cap, flow, 0, 3*n)==w?"TAK\n":"NIE\n");

//    printf("Flows:\n");
//    printMatrix(flow);

    return 0;
}