标签:
Description
Input
Output
Sample Input
Sample Output
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define INF 1<<30 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 1010; const int MAXM = 200010; struct node{ int to; int val; int next; }edge[MAXM*2]; int x[MAXN],y[MAXN],z[MAXN]; int pre[MAXN],vis[MAXN],ind,k,n,m,S,T; void add(int x,int y,int z){ edge[ind].to = y; edge[ind].val = z; edge[ind].next = pre[x]; pre[x] = ind ++; } bool bfs(){ queue<int>q; memset(vis,-1,sizeof(vis)); vis[S] = 0; q.push(S); while(!q.empty()){ int tp = q.front(); q.pop(); for(int i = pre[tp]; i != -1; i = edge[i].next){ int t = edge[i].to; if(vis[t] == -1 && edge[i].val){ vis[t] = vis[tp] + 1; q.push(t); } } } return vis[T] != -1; } int dfs(int rt,int low){ int used = 0; if(rt == T){ return low; } for(int i = pre[rt]; i != -1 && used < low; i = edge[i].next){ int t = edge[i].to; if(vis[t] == vis[rt] + 1 && edge[i].val){ int b = dfs(t,min(low-used,edge[i].val)); edge[i].val -= b; edge[i^1].val += b; used += b; } } if(used == 0){ vis[rt] = -1; } return used; } int main(){ int t,ff = 0; scanf("%d",&t); while(t--){ scanf("%d%d",&m,&k); ind = 0; memset(pre,-1,sizeof(pre)); n = 0; ll all = 0; for(int i = 1; i <= m; i++){ int fx,fy,fz; scanf("%d%d%d",&fx,&fy,&fz); x[i] = fy; y[i] = fx; z[i] = fz; n = max(n,z[i]); all += y[i]; } for(int i = 1; i <= m; i++){ for(int j = x[i]; j <= z[i]; j++){ add(i,j+m,1),add(j+m,i,0); } } S = 0,T = m + n + 1; for(int i = 1; i <= m; i++){ add(S,i,y[i]),add(i,S,0); } for(int i = 1; i <= n; i++){ add(i+m,T,k),add(T,i+m,0); } ll ans = 0; while(bfs()){ while(1){ ll a = dfs(S,INF); if(!a)break; ans += a; } } //cout<<S<<" "<<T<<endl; printf("Case %d: ",++ff); if(ans == all){ printf("Yes\n"); } else { printf("No\n"); } printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/5598609.html