#include<algorithm> #include<cassert> #include<cassert> #include<cctype> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<deque> #include<iostream> #include<list> #include<map> #include<queue> #include<set> #include<string> #include<vector> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORD(i,a,b) for(int i=(a);i>=(b);i--) #define FOREACH(i,c) for(__typeof((c).begin())i = (c).begin();i!=(c).end(); ++i) #define PB push_back // MAKSYMALNY PRZEPLYW O(V^3) (algorytm Push-Relabel) const int N = 100*1000; struct Edge { int v,cap,flow; int back_ind; Edge *back; Edge(int vi, int ci):v(vi),cap(ci){} }; /* Usage: 1) n=...; s=...; t=...; 2) REP(i,n) g[i].clear(); 3) add_edge(...); 4) compute_flow(); */ int n,s,t; int e[N],h[N]; vector<Edge> g[N]; vector<Edge>::iterator cur[N]; void bfs(int start, int start_h) { queue<int> q; h[start] = start_h; for(q.push(start);!q.empty();q.pop()) { int u = q.front(); FOREACH(i,g[u]) if (i->back->flow < i->back->cap && h[i->v]>h[u]+1) { h[i->v] = h[u] + 1; q.push(i->v); } } } int compute_flow() { queue<int> q; REP(i,n) { FOREACH(j,g[i]) { j->flow = 0; j->back = &g[j->v][j->back_ind]; } cur[i] = g[i].begin(); h[i] = e[i] = 0; } FOREACH(i,g[s]) { i->flow = i->cap; i->back->flow = -i->flow; if (e[i->v]==0 && i->v!=t) q.push(i->v); e[i->v] += i->flow; } h[s] = n; int relabel_counter = 0; for(;!q.empty();q.pop()) { int u = q.front(); while (e[u]>0) { if (cur[u]==g[u].end()) { // relabel relabel_counter++; h[u] = 2*n+1; FOREACH(i,g[u]) if(i->flow < i->cap) h[u]=min(h[u],1+h[i->v]); cur[u] = g[u].begin(); continue; } if (cur[u]->flow < cur[u]->cap && h[u]==h[cur[u]->v]+1) { // push int d = min(e[u], cur[u]->cap - cur[u]->flow); cur[u]->flow += d; cur[u]->back->flow -= d; e[u] -= d; e[cur[u]->v] += d; if (e[cur[u]->v]==d && cur[u]->v!=t && cur[u]->v!=s) q.push(cur[u]->v); } else cur[u]++; } if (relabel_counter >= n) { REP(i,n) h[i]=2*n+1; bfs(t,0); bfs(s,n); relabel_counter = 0; } } return e[t]; } void add_edge(int a, int b, int c, int c_back=0) { assert(a != b); // NIE wrzucac petelek! g[a].push_back(Edge(b,c)); g[b].push_back(Edge(a,c_back)); g[a].back().back_ind = g[b].size()-1; g[b].back().back_ind = g[a].size()-1; } VI c, p, k; int main() { int u, w; scanf("%d %d", &u, &w); c.resize(u); p.resize(u); k.resize(u); int cc = 0; REP(i, u) { scanf("%d %d %d", &p[i], &k[i], &c[i]); cc += c[i]; } int a = u; int b = u*2; n = a+b+2; s = n-1; t = n-2; REP(i,n) g[i].clear(); VI v; REP(i, u) { v.PB(p[i]); v.PB(k[i]); } sort(v.begin(), v.end()); REP(i, u) { add_edge(s, i, c[i]); REP(j, v.size()-1) { if (p[i] <= v[j] && k[i] >= v[j+1]) { add_edge(i, a+j, v[j+1] - v[j]); } } } REP(j, v.size()-1) { add_edge(a+j, t, w * (v[j+1] - v[j])); } cout << (compute_flow() == cc ? "TAK" : "NIE") << endl; return 0; }
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<algorithm> #include<cassert> #include<cassert> #include<cctype> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<deque> #include<iostream> #include<list> #include<map> #include<queue> #include<set> #include<string> #include<vector> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORD(i,a,b) for(int i=(a);i>=(b);i--) #define FOREACH(i,c) for(__typeof((c).begin())i = (c).begin();i!=(c).end(); ++i) #define PB push_back // MAKSYMALNY PRZEPLYW O(V^3) (algorytm Push-Relabel) const int N = 100*1000; struct Edge { int v,cap,flow; int back_ind; Edge *back; Edge(int vi, int ci):v(vi),cap(ci){} }; /* Usage: 1) n=...; s=...; t=...; 2) REP(i,n) g[i].clear(); 3) add_edge(...); 4) compute_flow(); */ int n,s,t; int e[N],h[N]; vector<Edge> g[N]; vector<Edge>::iterator cur[N]; void bfs(int start, int start_h) { queue<int> q; h[start] = start_h; for(q.push(start);!q.empty();q.pop()) { int u = q.front(); FOREACH(i,g[u]) if (i->back->flow < i->back->cap && h[i->v]>h[u]+1) { h[i->v] = h[u] + 1; q.push(i->v); } } } int compute_flow() { queue<int> q; REP(i,n) { FOREACH(j,g[i]) { j->flow = 0; j->back = &g[j->v][j->back_ind]; } cur[i] = g[i].begin(); h[i] = e[i] = 0; } FOREACH(i,g[s]) { i->flow = i->cap; i->back->flow = -i->flow; if (e[i->v]==0 && i->v!=t) q.push(i->v); e[i->v] += i->flow; } h[s] = n; int relabel_counter = 0; for(;!q.empty();q.pop()) { int u = q.front(); while (e[u]>0) { if (cur[u]==g[u].end()) { // relabel relabel_counter++; h[u] = 2*n+1; FOREACH(i,g[u]) if(i->flow < i->cap) h[u]=min(h[u],1+h[i->v]); cur[u] = g[u].begin(); continue; } if (cur[u]->flow < cur[u]->cap && h[u]==h[cur[u]->v]+1) { // push int d = min(e[u], cur[u]->cap - cur[u]->flow); cur[u]->flow += d; cur[u]->back->flow -= d; e[u] -= d; e[cur[u]->v] += d; if (e[cur[u]->v]==d && cur[u]->v!=t && cur[u]->v!=s) q.push(cur[u]->v); } else cur[u]++; } if (relabel_counter >= n) { REP(i,n) h[i]=2*n+1; bfs(t,0); bfs(s,n); relabel_counter = 0; } } return e[t]; } void add_edge(int a, int b, int c, int c_back=0) { assert(a != b); // NIE wrzucac petelek! g[a].push_back(Edge(b,c)); g[b].push_back(Edge(a,c_back)); g[a].back().back_ind = g[b].size()-1; g[b].back().back_ind = g[a].size()-1; } VI c, p, k; int main() { int u, w; scanf("%d %d", &u, &w); c.resize(u); p.resize(u); k.resize(u); int cc = 0; REP(i, u) { scanf("%d %d %d", &p[i], &k[i], &c[i]); cc += c[i]; } int a = u; int b = u*2; n = a+b+2; s = n-1; t = n-2; REP(i,n) g[i].clear(); VI v; REP(i, u) { v.PB(p[i]); v.PB(k[i]); } sort(v.begin(), v.end()); REP(i, u) { add_edge(s, i, c[i]); REP(j, v.size()-1) { if (p[i] <= v[j] && k[i] >= v[j+1]) { add_edge(i, a+j, v[j+1] - v[j]); } } } REP(j, v.size()-1) { add_edge(a+j, t, w * (v[j+1] - v[j])); } cout << (compute_flow() == cc ? "TAK" : "NIE") << endl; return 0; } |