#include<bits/stdc++.h> #define FOR(i,s,e) for(int i=(s);i<=(e);i++) #define FORD(i,s,e) for(int i=(s);i>=(e);i--) #define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i) #define ALL(k) (k).begin(),(k).end() #define e1 first #define e2 second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; const bool print=false; const int MAXN=411; int kr[MAXN][MAXN]; vector<int> krl[MAXN]; vector<int> odw; int ojc[MAXN]; void bfs(int st,int en){ ojc[st]=-1; odw.pb(st); queue<int> Q;Q.push(st); bool end=false; while(!Q.empty()){ int v=Q.front();Q.pop(); for(auto b:krl[v]){ if(ojc[b]!=0||kr[v][b]==0) continue; ojc[b]=v; odw.pb(b); Q.push(b); if(b==en) {end=true;break;} } if(end) break; } } int maxflow(int n){ int st=n+1,en=n+2; int wyn=0; while(true){ bfs(st,en); if(print)puts("BFS1"); //FOR(i,1,9) printf("%d %d\n",i,ojc[i]); if(ojc[en]==0){ for(auto it:odw) ojc[it]=0; odw.clear(); return wyn; } int ob=en; int mn=kr[ojc[en]][en]; vector<int> pth; while(ob!=st){ if(print)pth.pb(ob); if(kr[ojc[ob]][ob]<mn) mn=kr[ojc[ob]][ob]; ob=ojc[ob]; } if(print){ pth.pb(st); while(!pth.empty()) printf("%d ",pth.back()),pth.pop_back(); printf(" ww=%d\n",mn); } ob=en; wyn+=mn; while(ob!=st) kr[ojc[ob]][ob]-=mn,kr[ob][ojc[ob]]+=mn,ob=ojc[ob]; for(auto it:odw) ojc[it]=0; odw.clear(); } return wyn; } int on[MAXN]; main(){ int n,m;scanf("%d%d",&n,&m); vector<PII> V; int ss=0; FOR(i,1,n){ int a,b,c;scanf("%d%d%d",&a,&b,&c); kr[n+1][i]=c; ss+=c; krl[i].pb(n+1);krl[n+1].pb(i); V.eb(a,i);V.eb(b,i); } sort(ALL(V)); int ob=n+3; int popp=V[0].e1; int act=0; for(auto it:V){ if(it.e1-popp>0&&act>0){ if(print)printf("OB %d %d\n",popp,it.e1); kr[ob][n+2]=(it.e1-popp)*m; krl[ob].pb(n+2);krl[n+2].pb(ob); FOR(i,1,n){ if(print&&on[i]) printf("\t%d %d: %d %d\n",ob,i,popp,it.e1); if(on[i]) { kr[i][ob]=(it.e1-popp); krl[i].pb(ob);krl[ob].pb(i); } } ob++; } if(on[it.e2]) on[it.e2]=0,act--; else on[it.e2]=1,act++; popp=it.e1; } if(print)FOR(i,1,n) printf("K1 %d %d:: %d\n",n+1,i,kr[n+1][i]); if(print)FOR(i,1,n){ FOR(j,n+3,ob-1) if(kr[i][j]) printf("K2 %d %d:: %d\n",i,j,kr[i][j]); } if(print)FOR(j,n+3,ob-1) printf("K3 %d %d:: %d\n",j,n+2,kr[j][n+2]); int wyn=maxflow(n); puts(wyn==ss?"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 | #include<bits/stdc++.h> #define FOR(i,s,e) for(int i=(s);i<=(e);i++) #define FORD(i,s,e) for(int i=(s);i>=(e);i--) #define FOREACH(i,c) for( __typeof((c).begin()) i=(c).begin();i!=(c).end();++i) #define ALL(k) (k).begin(),(k).end() #define e1 first #define e2 second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; const bool print=false; const int MAXN=411; int kr[MAXN][MAXN]; vector<int> krl[MAXN]; vector<int> odw; int ojc[MAXN]; void bfs(int st,int en){ ojc[st]=-1; odw.pb(st); queue<int> Q;Q.push(st); bool end=false; while(!Q.empty()){ int v=Q.front();Q.pop(); for(auto b:krl[v]){ if(ojc[b]!=0||kr[v][b]==0) continue; ojc[b]=v; odw.pb(b); Q.push(b); if(b==en) {end=true;break;} } if(end) break; } } int maxflow(int n){ int st=n+1,en=n+2; int wyn=0; while(true){ bfs(st,en); if(print)puts("BFS1"); //FOR(i,1,9) printf("%d %d\n",i,ojc[i]); if(ojc[en]==0){ for(auto it:odw) ojc[it]=0; odw.clear(); return wyn; } int ob=en; int mn=kr[ojc[en]][en]; vector<int> pth; while(ob!=st){ if(print)pth.pb(ob); if(kr[ojc[ob]][ob]<mn) mn=kr[ojc[ob]][ob]; ob=ojc[ob]; } if(print){ pth.pb(st); while(!pth.empty()) printf("%d ",pth.back()),pth.pop_back(); printf(" ww=%d\n",mn); } ob=en; wyn+=mn; while(ob!=st) kr[ojc[ob]][ob]-=mn,kr[ob][ojc[ob]]+=mn,ob=ojc[ob]; for(auto it:odw) ojc[it]=0; odw.clear(); } return wyn; } int on[MAXN]; main(){ int n,m;scanf("%d%d",&n,&m); vector<PII> V; int ss=0; FOR(i,1,n){ int a,b,c;scanf("%d%d%d",&a,&b,&c); kr[n+1][i]=c; ss+=c; krl[i].pb(n+1);krl[n+1].pb(i); V.eb(a,i);V.eb(b,i); } sort(ALL(V)); int ob=n+3; int popp=V[0].e1; int act=0; for(auto it:V){ if(it.e1-popp>0&&act>0){ if(print)printf("OB %d %d\n",popp,it.e1); kr[ob][n+2]=(it.e1-popp)*m; krl[ob].pb(n+2);krl[n+2].pb(ob); FOR(i,1,n){ if(print&&on[i]) printf("\t%d %d: %d %d\n",ob,i,popp,it.e1); if(on[i]) { kr[i][ob]=(it.e1-popp); krl[i].pb(ob);krl[ob].pb(i); } } ob++; } if(on[it.e2]) on[it.e2]=0,act--; else on[it.e2]=1,act++; popp=it.e1; } if(print)FOR(i,1,n) printf("K1 %d %d:: %d\n",n+1,i,kr[n+1][i]); if(print)FOR(i,1,n){ FOR(j,n+3,ob-1) if(kr[i][j]) printf("K2 %d %d:: %d\n",i,j,kr[i][j]); } if(print)FOR(j,n+3,ob-1) printf("K3 %d %d:: %d\n",j,n+2,kr[j][n+2]); int wyn=maxflow(n); puts(wyn==ss?"TAK":"NIE"); } |