HDU-1529 Cashier Employment
据网上说这是到差分约束四星题。。。
可我觉得难吗?
比推DP方程容易。。。
两种约束方式,当然实现到程序就变成六种了。。。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <fstream> #include <iostream> #include <deque> #define rep(i, l, r) for(int i=l; i<=r; i++) #define down(i, l, r) for(int i=l; i>=r; i--) #define N 24 #define MAX 1<<30 using namespace std; int read() { int x=0, f=1; char ch=getchar(); while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } struct node{int y, n, v;} e[N*10]; int fir[N], en; int n, m, v[N], k[N], c[N], d[N]; bool b[N], ans; void Add(int x, int y, int v) { en++, e[en].y=y, e[en].v=v, e[en].n=fir[x], fir[x]=en; } bool Can(int m) { en=0; rep(i, 0, 23) fir[i]=0; rep(i, 0, 7) Add(i+16, i, v[i]-m); rep(i, 8, 23) Add(i-8, i, v[i]); rep(i, 1, 23) { Add(i-1, i, 0); Add(i, i-1, -k[i]); } Add(23, 0, -m); Add(0, 23, m-k[0]); deque <int> q; rep(i, 0, 23) b[i]=1, c[i]=1, d[i]=0, q.push_back(i); bool ans=1; while (!q.empty()) { int x=q.front(), o=fir[x], y=e[o].y; b[x]=0; q.pop_front(); if (c[x]>N) { ans=0; break; } while (o) { if (d[y] < d[x]+e[o].v) { d[y] = d[x]+e[o].v; if (!b[y]) b[y]=1, c[y]++, q.push_back(y); } o=e[o].n, y=e[o].y; } } return ans; } int main() { int t=read(); while (t--) { rep(i, 0, 23) v[i]=k[i]=0; rep(i, 0, 23) v[i]=read(); n=m=read(); rep(i, 1, m) k[read()]++; int l=0, r=m+1; while (l != r) { m=(l+r)/2; if (Can(m)) r=m; else l=m+1; } if (l==n+1) printf("No Solution\n"); else printf("%d\n", l); } return 0; }