/ *
* Maximum flow, each week, each day is split into a point src origin to the point of flow 1
* Corresponding to each of the movie a certain days of the week, and these days is connected to the corresponding film flow 1
* Then each movie for the week number of the flow to the exchange des
* Establish plans should be like this: src-> day-> film-> des
* /
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 2000
#define MAXM 200000
#define INF 0x7fffffff
#define MAXDAY 7
#define MAXFILM 25
struct Edge{
int v, w, next;
}edge[MAXM];
int work[MAXFILM][MAXDAY+3], e_cnt, head[MAXN];
int cur[MAXN], dis[MAXN], gap[MAXN], pre[MAXN];
void insert_arc(int u, int v, int w)
{
edge[e_cnt].v = v; edge[e_cnt].w = w;
edge[e_cnt].next = head[u]; head[u] = e_cnt ++;
swap(u, v);
edge[e_cnt].v = v; edge[e_cnt].w = 0;
edge[e_cnt].next = head[u]; head[u] = e_cnt ++;
}
void re_bfs(int des, int vn)
{
int u, v;
queue<int> q;
memset(gap, 0, sizeof(gap));
fill(dis, dis+vn+1, vn);
dis[des] = 0; gap[0] = 1;
q.push(des);
while( !q.empty() ) {
u = q.front(); q.pop();
for(int i = head[u]; -1 != i; i = edge[i].next) {
v = edge[i].v;
if( edge[i].w || vn > dis[v] ) {
continue;
}
q.push(v); gap[ dis[v] = dis[u]+1 ] ++;
}
}
}
int i_sap(int src, int des, int vn)
{
int u(src), v, min_dis, path_flow(INF), max_flow(0);
re_bfs(des, vn); pre[u] = u;
for(int i = 0; i <= vn; i ++) {
cur[i] = head[i];
}
while( dis[src] < vn ) {
loop:
for(int &i = cur[u]; -1 != i; i = edge[i].next) {
v = edge[i].v;
if( !edge[i].w || dis[u] != dis[v]+1 ) {
continue;
}
path_flow = min(path_flow, edge[i].w);
pre[v] = u; u = v;
if( v == des ) {
for(u = pre[u]; v != src; v = u, u = pre[u]) {
edge[cur[u]].w -= path_flow;
edge[cur[u]^0x1].w += path_flow;
}
max_flow += path_flow; path_flow = INF;
}
goto loop;
}
min_dis = vn;
for(int i = head[u]; -1 != i; i = edge[i].next) {
v = edge[i].v;
if( edge[i].w && min_dis > dis[v] ) {
cur[u] = i; min_dis = dis[v];
}
}
if( !gap[ --dis[u] ] ) {
break;
}
gap[ dis[u] = min_dis+1 ] ++;
u = pre[u];
}
return max_flow;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas, film_cnt, src, des, max_week, all_flow;
scanf("%d", &cas);
while( cas-- ) {
scanf("%d", &film_cnt);
max_week = 0; e_cnt = 0; all_flow = 0;
for(int i = 1; i <= film_cnt; i ++) {
for(int j = 1; j <= MAXDAY; j ++) {
scanf("%d", &work[i][j]);
}
scanf("%d %d", &work[i][MAXDAY+1], &work[i][MAXDAY+2]);
max_week = max(max_week, work[i][MAXDAY+2]);
all_flow += work[i][MAXDAY+1];
}
memset(head, -1, sizeof(head));
src = max_week*MAXDAY+film_cnt+1; des = src+1;
for(int i = 1; i <= film_cnt; i ++) {
insert_arc(max_week*MAXDAY+i, des, work[i][MAXDAY+1]);
for(int d = 1; d <= MAXDAY; d ++) {
if( work[i][d] ) {
for(int w = 0; w < work[i][MAXDAY+2]; w ++) {
insert_arc(w*MAXDAY+d, max_week*MAXDAY+i, 1);
}
}
}
}
for(int i = 1; i <= max_week*MAXDAY; i ++) {
insert_arc(src, i, 1);
}
if( all_flow == i_sap(src, des, des+1) ) {
printf("Yes\n");
}
else {
printf("No\n");
}
}
return 0;
}
poj_1698
最新推荐文章于 2018-03-22 13:32:33 发布