//Zrodlo: //http://was.zaa.mimuw.edu.pl/sites/default/files/file/s2008-w03/flow.cpp #include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #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 RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int INF=1e9; const int N=500; vector<int> krawedzie[N]; vector<int>::iterator poczatek[N]; int przepustowosc[N][N], odleglosc[N], q[N]; void AddEdge(int a, int b, int c) { //debug("%d %d %d\n", a, b, c); przepustowosc[a][b]+=c; krawedzie[a].push_back(b); krawedzie[b].push_back(a); } int bfs(int s, int t, int n) { for(int i=0; i<n; ++i) odleglosc[i]=-1; odleglosc[s]=0; int beg=0, end=0; q[end++]=s; while(beg<end) { int v=q[beg++]; for(vector<int>::iterator it=krawedzie[v].begin(); it!=krawedzie[v].end(); ++it) if(przepustowosc[v][*it]>0 && odleglosc[*it]==-1) { odleglosc[*it]=odleglosc[v]+1; q[end++]=*it; } } return odleglosc[t]!=-1; } int dfs(int x, int t, int minimum) { int res=0; if(x==t || minimum==0) return minimum; for(vector<int>::iterator &it=poczatek[x]; it!=krawedzie[x].end(); ++it) { if(odleglosc[x]+1==odleglosc[*it] && przepustowosc[x][*it]>0) { int y=dfs(*it,t,min(minimum,przepustowosc[x][*it])); przepustowosc[x][*it]-=y; przepustowosc[*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)) { for(int i=0; i<n; ++i) poczatek[i]=krawedzie[i].begin(); res+=dfs(s, t, INF); } return res; } int n, m, sum; PII T[105]; vector<PII> ev[1000005]; bool B[105]; vector<PII> toadd; int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); RE(i, n) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a++; b++; T[i]=MP(a, b); AddEdge(0, i, c); ev[a].PB(MP(1, i)); ev[b].PB(MP(0, i)); sum+=c; } int t=n+1, st=0, open=0; RE(i, 1000004) { if(ev[i].empty()) continue; if(open>0) { int len=i-st; RE(j, n) if(B[j]) AddEdge(j, t, len); toadd.PB(MP(t, len*m)); t++; } REP(j, siz(ev[i])) { B[ev[i][j].S]=ev[i][j].F; if(ev[i][j].F) open++; else open--; } st=i; } REP(i, siz(toadd)) AddEdge(toadd[i].F, t, toadd[i].S); int lol=Maxflow(0, t, t+1); //debug("%d\n", lol); if(lol==sum) printf("TAK\n"); else printf("NIE\n"); 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 | //Zrodlo: //http://was.zaa.mimuw.edu.pl/sites/default/files/file/s2008-w03/flow.cpp #include<bits/stdc++.h> using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #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 RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) const int INF=1e9; const int N=500; vector<int> krawedzie[N]; vector<int>::iterator poczatek[N]; int przepustowosc[N][N], odleglosc[N], q[N]; void AddEdge(int a, int b, int c) { //debug("%d %d %d\n", a, b, c); przepustowosc[a][b]+=c; krawedzie[a].push_back(b); krawedzie[b].push_back(a); } int bfs(int s, int t, int n) { for(int i=0; i<n; ++i) odleglosc[i]=-1; odleglosc[s]=0; int beg=0, end=0; q[end++]=s; while(beg<end) { int v=q[beg++]; for(vector<int>::iterator it=krawedzie[v].begin(); it!=krawedzie[v].end(); ++it) if(przepustowosc[v][*it]>0 && odleglosc[*it]==-1) { odleglosc[*it]=odleglosc[v]+1; q[end++]=*it; } } return odleglosc[t]!=-1; } int dfs(int x, int t, int minimum) { int res=0; if(x==t || minimum==0) return minimum; for(vector<int>::iterator &it=poczatek[x]; it!=krawedzie[x].end(); ++it) { if(odleglosc[x]+1==odleglosc[*it] && przepustowosc[x][*it]>0) { int y=dfs(*it,t,min(minimum,przepustowosc[x][*it])); przepustowosc[x][*it]-=y; przepustowosc[*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)) { for(int i=0; i<n; ++i) poczatek[i]=krawedzie[i].begin(); res+=dfs(s, t, INF); } return res; } int n, m, sum; PII T[105]; vector<PII> ev[1000005]; bool B[105]; vector<PII> toadd; int main() { //ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); RE(i, n) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a++; b++; T[i]=MP(a, b); AddEdge(0, i, c); ev[a].PB(MP(1, i)); ev[b].PB(MP(0, i)); sum+=c; } int t=n+1, st=0, open=0; RE(i, 1000004) { if(ev[i].empty()) continue; if(open>0) { int len=i-st; RE(j, n) if(B[j]) AddEdge(j, t, len); toadd.PB(MP(t, len*m)); t++; } REP(j, siz(ev[i])) { B[ev[i][j].S]=ev[i][j].F; if(ev[i][j].F) open++; else open--; } st=i; } REP(i, siz(toadd)) AddEdge(toadd[i].F, t, toadd[i].S); int lol=Maxflow(0, t, t+1); //debug("%d\n", lol); if(lol==sum) printf("TAK\n"); else printf("NIE\n"); return 0; } |