状态DP显然可以解,发现T了,不知道优化后能不能过。
然后发现费用流可以解。trick是对need拆解成need/K, need%K两种情况讨论。

 /* 4312 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
int v, w, f, nxt;
} edge_t; const int INF = 0x3f3f3f3f;
const int maxv = ;
const int maxe = 1e5+;
int need[maxv];
int a[][];
int head[maxv], l;
edge_t E[maxe];
int dis[maxv];
int pre[maxv], ID[maxv];
bool visit[maxv];
int cost, flow;
int n, m, K;
const int ed = maxv - ;
const int st = maxv - ; void init() {
l = ;
memset(head, -, sizeof(head));
} void addEdge(int u, int v, int w, int c) {
E[l].v = v;
E[l].w = w;
E[l].f = c;
E[l].nxt = head[u];
head[u] = l++; E[l].v = u;
E[l].w = -w;
E[l].f = ;
E[l].nxt = head[v];
head[v] = l++;
} bool bfs() {
queue<int> Q;
int u, v, k; memset(dis, INF, sizeof(dis));
memset(visit, false, sizeof(visit));
Q.push(st);
visit[st] = true;
dis[st] = ; while (!Q.empty()) {
u = Q.front();
Q.pop();
visit[u] = false;
for (k=head[u]; k!=-; k=E[k].nxt) {
v = E[k].v;
if (E[k].f && dis[v]>dis[u]+E[k].w) {
dis[v] = dis[u] + E[k].w;
pre[v] = u;
ID[v] = k;
if (!visit[v]) {
visit[v] = true;
Q.push(v);
}
}
}
} return dis[ed]==INF;
} void MCMF() {
int u, v, k;
int tmp; flow = , cost = ; while () {
if (bfs())
break; tmp = INF;
for (v=ed, u=pre[v]; v!=st; v=u, u=pre[v]) {
k = ID[v];
tmp = min(tmp, E[k].f);
} for (v=ed, u=pre[v]; v!=st; v=u, u=pre[v]) {
k = ID[v];
E[k].f -= tmp;
E[k^].f += tmp;
} flow += tmp;
cost += tmp * dis[ed];
}
} void solve() {
int tot = , tmp; init();
rep(i, , m)
addEdge(st, i+n, , ); rep(i, , n) {
tot += need[i];
rep(j, , m) {
if (a[i][j])
addEdge(j+n, i, , );
}
tmp = need[i] / K;
if (tmp) {
addEdge(i, ed, -K, tmp);
}
tmp = need[i] % K;
if (tmp > ) {
addEdge(i, ed, -tmp, );
}
} MCMF(); cost = -cost;
if (cost+m-flow < tot)
puts("NO");
else
puts("YES");
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
rep(tt, , t+) {
scanf("%d %d %d", &m, &n, &K);
rep(i, , n)
scanf("%d", &need[i]);
rep(i, , n)
rep(j, , m)
scanf("%d", &a[i][j]);
printf("Case #%d: ", tt);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据发生器。

 from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 50
bound = 10**18
fout.write("%d\n" % (t))
for tt in xrange(t):
m = randint(1, 13)
n = randint(1, 13)
k = randint(2, 10)
fout.write("%d %d %d\n" % (m, n, k))
L = []
for i in xrange(n):
x = randint(1, 20)
L.append(x)
fout.write(" ".join(map(str, L)) + "\n")
for i in xrange(n):
L = []
for j in xrange(m):
x = randint(0, 1)
L.append(x)
fout.write(" ".join(map(str, L)) + "\n") def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
05-11 09:30