// tested by Hightail: https://github.com/dj3500/hightail #include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> #include <sstream> #include <stack> #include <cstring> #include <iomanip> #include <ctime> #include <cassert> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); namespace flow { const int N = 100007; // max nodes const int M = 1000007; // max edges typedef int T; // w razie zmiany na long long, zmienić też INF na 10^18 int nodes, src, dest, nedge; int *point, *next, *flow, *capa; int head[N], dist[N], Q[N], work[N]; void init (int _nodes, int _src, int _dest) { nodes = _nodes + 2; // na wszelki wypadek src = _src; dest = _dest; point = new int[M]; next = new int[M]; flow = new int[M]; capa = new int[M]; FOR(i,nodes) head[i] = -1; nedge = 0; } void addEdge(int u, int v, int c1, int c2 = 0) { point[nedge]=v, capa[nedge]=c1, flow[nedge]=0, next[nedge]=head[u], head[u]=(nedge++); point[nedge]=u, capa[nedge]=c2, flow[nedge]=0, next[nedge]=head[v], head[v]=(nedge++); } bool dinicBfs() { FOR(i,nodes) dist[i] = -1; dist[src] = 0; int szQ = 1; Q[0] = src; FOR(cl,szQ) { for (int k = Q[cl], i = head[k]; i >= 0; i = next[i]) { if (flow[i] < capa[i] && dist[point[i]] < 0) { dist[point[i]] = dist[k] + 1; Q[szQ++] = point[i]; } } } return dist[dest] >= 0; } T dinicDfs (int x, T exp) { if (x == dest) return exp; T res = 0; for (int& i = work[x]; i >= 0; i = next[i]) { int v = point[i]; T tmp; if (flow[i] < capa[i] && dist[x]+1 == dist[v] && (tmp = dinicDfs(v, min(exp, (T)capa[i] - flow[i]))) > 0) { flow[i] += tmp; flow[i^1] -= tmp; res += tmp; exp -= tmp; if (0 == exp) break; } } return res; } T dinicFlow () { T res = 0; while (dinicBfs()) { FOR(i,nodes) work[i] = head[i]; res += dinicDfs(src,INF); } return res; } }; int a[107], b[107], c[107]; int main () { wez2(n,m); int sumWork = 0; FORI(i,n) { scanf("%d %d %d", a+i, b+i, c+i); sumWork += c[i]; } int src = 0, sink = 3*n+1; flow::init(3*n+2, src, sink); set<int> _times; FORI(i,n) { _times.insert(a[i]); _times.insert(b[i]); } const vi times(ALL(_times)); FORI(i,n) { flow::addEdge(src, i, c[i]); } REP(j,0,SZ(times)-2) { // times[j], times[j+1] flow::addEdge(n+1+j, sink, m * (times[j+1] - times[j])); FORI(i,n) { if (a[i] <= times[j] && times[j+1] <= b[i]) { flow::addEdge(i, n+1+j, times[j+1] - times[j]); } } } if (flow::dinicFlow() == sumWork) { printf("TAK"); } else { printf("NIE"); } }
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 | // tested by Hightail: https://github.com/dj3500/hightail #include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> #include <sstream> #include <stack> #include <cstring> #include <iomanip> #include <ctime> #include <cassert> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); namespace flow { const int N = 100007; // max nodes const int M = 1000007; // max edges typedef int T; // w razie zmiany na long long, zmienić też INF na 10^18 int nodes, src, dest, nedge; int *point, *next, *flow, *capa; int head[N], dist[N], Q[N], work[N]; void init (int _nodes, int _src, int _dest) { nodes = _nodes + 2; // na wszelki wypadek src = _src; dest = _dest; point = new int[M]; next = new int[M]; flow = new int[M]; capa = new int[M]; FOR(i,nodes) head[i] = -1; nedge = 0; } void addEdge(int u, int v, int c1, int c2 = 0) { point[nedge]=v, capa[nedge]=c1, flow[nedge]=0, next[nedge]=head[u], head[u]=(nedge++); point[nedge]=u, capa[nedge]=c2, flow[nedge]=0, next[nedge]=head[v], head[v]=(nedge++); } bool dinicBfs() { FOR(i,nodes) dist[i] = -1; dist[src] = 0; int szQ = 1; Q[0] = src; FOR(cl,szQ) { for (int k = Q[cl], i = head[k]; i >= 0; i = next[i]) { if (flow[i] < capa[i] && dist[point[i]] < 0) { dist[point[i]] = dist[k] + 1; Q[szQ++] = point[i]; } } } return dist[dest] >= 0; } T dinicDfs (int x, T exp) { if (x == dest) return exp; T res = 0; for (int& i = work[x]; i >= 0; i = next[i]) { int v = point[i]; T tmp; if (flow[i] < capa[i] && dist[x]+1 == dist[v] && (tmp = dinicDfs(v, min(exp, (T)capa[i] - flow[i]))) > 0) { flow[i] += tmp; flow[i^1] -= tmp; res += tmp; exp -= tmp; if (0 == exp) break; } } return res; } T dinicFlow () { T res = 0; while (dinicBfs()) { FOR(i,nodes) work[i] = head[i]; res += dinicDfs(src,INF); } return res; } }; int a[107], b[107], c[107]; int main () { wez2(n,m); int sumWork = 0; FORI(i,n) { scanf("%d %d %d", a+i, b+i, c+i); sumWork += c[i]; } int src = 0, sink = 3*n+1; flow::init(3*n+2, src, sink); set<int> _times; FORI(i,n) { _times.insert(a[i]); _times.insert(b[i]); } const vi times(ALL(_times)); FORI(i,n) { flow::addEdge(src, i, c[i]); } REP(j,0,SZ(times)-2) { // times[j], times[j+1] flow::addEdge(n+1+j, sink, m * (times[j+1] - times[j])); FORI(i,n) { if (a[i] <= times[j] && times[j+1] <= b[i]) { flow::addEdge(i, n+1+j, times[j+1] - times[j]); } } } if (flow::dinicFlow() == sumWork) { printf("TAK"); } else { printf("NIE"); } } |