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
|
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> using namespace std;
const int INF = ~0u>>1; typedef pair <int,int> P; #define MID(x,y) ((x+y)>>1) #define iabs(x) ((x)>0?(x):-(x)) #define REP(i,a,b) for(int i=(a);i<(b);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define pb push_back #define mp make_pair #define print() cout<<"--------"<<endl typedef long long ll; const int maxn = 400 + 10; const int inf = 0x7ffffff; struct SAP{ int cap[maxn][maxn]; int flow[maxn][maxn]; int g[maxn][maxn]; int n; int h[maxn]; int vh[maxn]; int source, sink; int mk[maxn]; void init(int n){ this->n = n; memset(cap, 0, sizeof(cap)); } void addCap(int i, int j, int val){ cap[i][j] = val; } int sap(const int idx, const int maxCap){ if (idx == sink) return maxCap; int li = maxCap, d, minH = n; for (int i = 0; i < n; i ++){ if (cap[idx][i] - flow[idx][i] > 0){ if (h[idx] == h[i] + 1){ d = sap(i, min(li, cap[idx][i] - flow[idx][i])); flow[idx][i] += d; flow[i][idx] -= d; li -= d; if (h[source] == n || li == 0) return maxCap - li; } minH = min(minH, h[i] + 1); } } if (li == maxCap){ vh[h[idx]] --; vh[minH] ++; if (vh[h[idx]] == 0) h[source] == n; h[idx] = minH; } return maxCap - li; } int solve(int source, int sink){ if (source == sink) return inf; this->sink = sink; this->source = source; memset(flow, 0, sizeof(flow)); memset(h, 0, sizeof(h)); memset(vh, 0, sizeof(vh)); int ans = 0; while (h[source] != n) ans += sap(source, inf); return ans; }
}mf;
int week[10]; int main(){ int T; cin >> T; while (T--){ int n,d,w; scanf("%d", &n); mf.init(401); int source = 0,sink = 400, sum = 0; for (int i = 1; i <= n; i ++){ for (int j = 1; j <= 7; j ++){ scanf("%d", &week[j]); } scanf("%d%d", &d, &w); sum += d; mf.addCap(source, i, d); for (int j = 1; j <= 7; j ++){ if (week[j]){ for (int k = 0; k < w; k ++){ int id = n + k * 7 + j; mf.addCap(i, id, 1); mf.addCap(id, sink, 1); } } } } int maxflow = mf.solve(source, sink); if (maxflow == sum) puts("Yes"); else puts("No");
} return 0; }
|