题意:一个人想把所有邀请她参演的电影都拍了,每个电影要在W周以内至少去D天,她一天只能去拍一部电影,问是否能全部都拍
构图: 日程表的每一周的每一天都作为一个点 源点到这些点有一条容量为1的边,代表她一天可以去拍一部电影
每一个电影能开拍的那一天 有一条指向这个电影的边,容量为1.每一个电影有一条到汇点的边 容量为该电影需要开拍的天数.
求最大流是否等于sum(D) 等于就yes 否则no
/*
* Dinic algo for max flow
*
* This implementation assumes that #nodes, #edges, and capacity on each edge <= INT_MAX,
* which means INT_MAX is the best approximation of INF on edge capacity.
* The total amount of max flow computed can be up to LLONG_MAX (not defined in this file),
* but each 'dfs' call in 'dinic' can return <= INT_MAX flow value.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <queue>
#include <vector>
#include <algorithm>
#include<iostream>
#define N (3000)
#define M (15000)
typedef long long LL;
using namespace std;
struct edge {
int v, cap, next;
};
edge e[M];
int head[N], level[N], cur[N];
int num_of_edges;
/*
* When there are multiple test sets, you need to re-initialize before each
*/
void dinic_init(void) {
num_of_edges = 0;
memset(head, -1, sizeof(head));
return;
}
int add_edge(int u, int v, int c1, int c2) {
int& i=num_of_edges;
assert(c1>=0 && c2>=0 && c1+c2>=0); // check for possibility of overflow
e[i].v = v;
e[i].cap = c1;
e[i].next = head[u];
head[u] = i++;
e[i].v = u;
e[i].cap = c2;
e[i].next = head[v];
head[v] = i++;
return i;
}
void print_graph(int n) {
for (int u=0; u<n; u++) {
printf("%d: ", u);
for (int i=head[u]; i>=0; i=e[i].next) {
printf("%d(%d)", e[i].v, e[i].cap);
}
printf("\n");
}
return;
}
/*
* Find all augmentation paths in the current level graph
* This is the recursive version
*/
int dfs(int u, int t, int bn) {
if (u == t) return bn;
int left = bn;
for (int i=cur[u]; i>=0; i=e[i].next) {
int v = e[i].v;
int c = e[i].cap;
if (c > 0 && level[u]+1 == level[v]) {
int flow = dfs(v, t, min(left, c));
if (flow > 0) {
e[i].cap -= flow;
e[i^1].cap += flow;
cur[u] = i;
left -= flow;
if (!left) break;
}
}
}
if (left > 0) level[u] = 0;
return bn - left;
}
bool bfs(int s, int t) {
memset(level, 0, sizeof(level));
level[s] = 1;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == t) return true;
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (!level[v] && e[i].cap > 0) {
level[v] = level[u]+1;
q.push(v);
}
}
}
return false;
}
LL dinic(int s, int t) {
LL max_flow = 0;
while (bfs(s, t)) {
memcpy(cur, head, sizeof(head));
max_flow += dfs(s, t, INT_MAX);
}
return max_flow;
}
int upstream(int s, int n) {
int cnt = 0;
vector<bool> visited(n);
queue<int> q;
visited[s] = true;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i=head[u]; i>=0; i=e[i].next) {
int v = e[i].v;
if (e[i].cap > 0 && !visited[v]) {
visited[v] = true;
q.push(v);
cnt++;
}
}
}
return cnt; // excluding s
}
int main() {
int caseNum;
cin>>caseNum;
for(int c=0;c<caseNum;c++)
{
dinic_init();
int arr[21][9];
int s,t;
int fnum;
cin>>fnum;
for(int i=0;i<fnum;i++)
{
for(int j=0;j<9;j++)
{
cin>>arr[i][j];
}
}
int week = 0;
for(int i=0;i<fnum;i++)
{
if(arr[i][8]>week)
week = arr[i][8];
}
for(int i=1;i<=7*week;i++)
{
add_edge(0,i,1,0);
}
for(int i=0;i<fnum;i++)//m:7*week+i+1 t:7*week+fnum+1
{
for(int w=0;w<arr[i][8];w++)
{
for(int j=0;j<7;j++)
{
if(arr[i][j] == 1)
{
int u,v;
u = w*7+j+1;
v = 7*week+i+1;
add_edge(u,v,1,0);
}
}
}
}
int sum = 0;
for(int i=0;i<fnum;i++)
{
int u = 7*week+i+1;
int v = 7*week+fnum+1;
int value = arr[i][7];
add_edge(u,v,value,0);
sum+=arr[i][7];
}
int flow = dinic(0,7*week+fnum+1);
if(sum == flow)
{
cout<<"Yes"<<endl;
}
else
cout<<"No"<<endl;
}
}