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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

#define NODES 302

struct Zad
{
    int pocz, kon, trwa;
} zad[100];

struct W
{
    int odw, pojWy[302], przeplWy[302];
    vector<int> krWy, krWe;
} w[302];//zr, uj, zad, przedz

void dodajKr(int pocz, int kon, int poj)
{
    w[pocz].krWy.push_back(kon);
    w[kon].krWe.push_back(pocz);
    w[pocz].pojWy[kon]=poj;
}

int pushRelabel(const int * const * C, int ** F, int source, int sink);

int main()
{
    ios_base::sync_with_stdio(false);
    int lZad, lProc, sumaTrwa=0;
    cin>>lZad>>lProc;
    vector<int> pkty;
    for (int i=0; i<lZad; ++i)
    {
        cin>>zad[i].pocz>>zad[i].kon>>zad[i].trwa;
        pkty.push_back(zad[i].pocz);
        pkty.push_back(zad[i].kon);
        sumaTrwa+=zad[i].trwa;
    }
    sort(pkty.begin(), pkty.end());
    int lPktow=unique(pkty.begin(), pkty.end())-pkty.begin();

    for (int i=0; i<lZad; ++i)
        dodajKr(2+i, 1, zad[i].trwa);

    for (int i=1; i<lPktow; ++i)
    {
        int szer=pkty[i]-pkty[i-1], ww=2+lZad+i-1;
        dodajKr(0, ww, szer*lProc);
        for (int j=0; j<lZad; ++j)
            if (zad[j].pocz<=pkty[i-1] && pkty[i]<=zad[j].kon)
                dodajKr(ww, 2+j, szer);
    }

    int **flow, **capacities;
     flow = (int **) calloc(NODES, sizeof(int*));
     capacities = (int **) calloc(NODES, sizeof(int*));
     for (int i = 0; i < NODES; i++) {
       flow[i] = w[i].przeplWy;
       capacities[i] = w[i].pojWy;
     }


     sumaTrwa-=pushRelabel(capacities, flow, 0, 1);

   // cerr<<sumaTrwa<<endl;
    cout<<(sumaTrwa ? "NIE" : "TAK")<<endl;
    return 0;
}

//Ponizszy kod skopiowany z: https://en.wikipedia.org/wiki/Relabel-to-front_algorithm
//Licencja: http://creativecommons.org/licenses/by-sa/3.0/

#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define INFINITE 200000000

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;
}