#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; namespace flow { #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) const int INF = 1000000001; const int N=330; VI kraw[N]; VI::iterator begin[N]; int c[N][N]; int dist[N]; void AddEdge(int a,int b,int cc){ c[a][b]+=cc; kraw[a].PB(b); kraw[b].PB(a); } int q[N]; int bfs(int s,int t,int n){ REP(i,n) dist[i]=-1; dist[s]=0; int beg=0,end=0; q[end++]=s; while (beg<end){ int v=q[beg++]; FOREACH(it,kraw[v]) if (c[v][*it]>0 && dist[*it]==-1){ dist[*it]=dist[v]+1; q[end++]=*it; } } return dist[t]!=-1; } int dfs(int x,int t,int minimum){ int res=0; if (x==t || minimum==0) return minimum; for(VI::iterator &it=begin[x];it!=kraw[x].end(); ++it){ if (dist[x]+1==dist[*it] && c[x][*it]>0){ int y=dfs(*it,t,min(minimum,c[x][*it])); c[x][*it]-=y; c[*it][x]+=y; minimum-=y; res+=y; if (minimum==0) break; } } return res; } int Maxflow(int s,int t,int n){ int res=0; while (bfs(s,t,n)){ REP(i,n) begin[i]=kraw[i].begin(); res+=dfs(s,t,INF); } return res; } } const int MXN = 103; int p[MXN]; int k[MXN]; int c[MXN]; const int czas = 1e6+2; VI add[czas+1]; VI rem[czas+1]; int main() { int n, m; scanf("%d%d", &n, &m); int mxk = 0; int sumc = 0; FOR(i, 1, n) { scanf("%d%d%d", &p[i], &k[i], &c[i]); p[i]++; add[p[i]].PB(i); rem[k[i]].PB(i); sumc += c[i]; maxi(mxk, k[i]); } FOR(i, 1, n) { flow::AddEdge(0, i, c[i]); } VI now; int cnt = 0; int ID = 1; FOR(i, 1, mxk) { for(auto it:add[i]) { now.PB(it); } cnt++; if(rem[i].size() || add[i+1].size()) { //nowy wierzchołek for(auto it:now) { flow::AddEdge(it, n + ID, cnt); } flow::AddEdge(n + ID, n * 3 + 5, cnt * m); ID++; cnt = 0; } for(auto it:rem[i]) { REP(j, now.size()) { if(now[j] == it) { now.erase(now.begin() + j); break; } } } } int flow = flow::Maxflow(0, n * 3 + 5, n * 3 + 10); puts(flow == sumc ? "TAK" : "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 149 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; namespace flow { #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) const int INF = 1000000001; const int N=330; VI kraw[N]; VI::iterator begin[N]; int c[N][N]; int dist[N]; void AddEdge(int a,int b,int cc){ c[a][b]+=cc; kraw[a].PB(b); kraw[b].PB(a); } int q[N]; int bfs(int s,int t,int n){ REP(i,n) dist[i]=-1; dist[s]=0; int beg=0,end=0; q[end++]=s; while (beg<end){ int v=q[beg++]; FOREACH(it,kraw[v]) if (c[v][*it]>0 && dist[*it]==-1){ dist[*it]=dist[v]+1; q[end++]=*it; } } return dist[t]!=-1; } int dfs(int x,int t,int minimum){ int res=0; if (x==t || minimum==0) return minimum; for(VI::iterator &it=begin[x];it!=kraw[x].end(); ++it){ if (dist[x]+1==dist[*it] && c[x][*it]>0){ int y=dfs(*it,t,min(minimum,c[x][*it])); c[x][*it]-=y; c[*it][x]+=y; minimum-=y; res+=y; if (minimum==0) break; } } return res; } int Maxflow(int s,int t,int n){ int res=0; while (bfs(s,t,n)){ REP(i,n) begin[i]=kraw[i].begin(); res+=dfs(s,t,INF); } return res; } } const int MXN = 103; int p[MXN]; int k[MXN]; int c[MXN]; const int czas = 1e6+2; VI add[czas+1]; VI rem[czas+1]; int main() { int n, m; scanf("%d%d", &n, &m); int mxk = 0; int sumc = 0; FOR(i, 1, n) { scanf("%d%d%d", &p[i], &k[i], &c[i]); p[i]++; add[p[i]].PB(i); rem[k[i]].PB(i); sumc += c[i]; maxi(mxk, k[i]); } FOR(i, 1, n) { flow::AddEdge(0, i, c[i]); } VI now; int cnt = 0; int ID = 1; FOR(i, 1, mxk) { for(auto it:add[i]) { now.PB(it); } cnt++; if(rem[i].size() || add[i+1].size()) { //nowy wierzchołek for(auto it:now) { flow::AddEdge(it, n + ID, cnt); } flow::AddEdge(n + ID, n * 3 + 5, cnt * m); ID++; cnt = 0; } for(auto it:rem[i]) { REP(j, now.size()) { if(now[j] == it) { now.erase(now.begin() + j); break; } } } } int flow = flow::Maxflow(0, n * 3 + 5, n * 3 + 10); puts(flow == sumc ? "TAK" : "NIE"); } |