http://acm.hdu.edu.cn/showproblem.php?pid=4494
题意:N个城市,编号0~N-1。1~N-1号城市每个城市i需要一定数量的不同类工人(种数<=5),且每个城市i要求这些工人一定要在某个确切的时间bi之前集全部集齐然后一起工作一段时间pi。不同类的工人之间不能替换,去完一个城市的工人如果时间允许可以赶去另一个城市(城市之间有一个距离),起始时工人们都在城市0,起始时间点为0,问最少需要多少工人。
分析:最小路径覆盖,用最少的工人覆盖所有有需要的城市。各种worker之间没有影响,一次建图,分别对求每种worker求一次费用流。
#include
#include
#include
#include
#include
using namespace std;
const int N = 460;
const int M = 6 * N + N * N * 2;
const int INF = 0x3f3f3f3f;
int n, m;
int x[N], y[N], b[N], p[N], need[10][N];
double d[N][N];
int en, S, T, NV, head[N], from[M], to[M], cost[M], flow[M], next[M];
int top, sta[N], dis[N], id[N];
bool vis[N];
inline void addEdge(int u, int v, int c) {
from[en] = u, to[en] = v, cost[en] = c, next[en] = head[u], head[u] = en++;
from[en] = v, to[en] = u, cost[en] = -c, next[en] = head[v], head[v] = en++;
}
bool SPFA() {
for (int i = 0; i < NV; i++) dis[i] = INF, vis[i] = false;
dis[S] = 0;
sta[top = 1] = S;
while (top) {
int u = sta[top--];
vis[u] = false;
for (int i = head[u]; i != -1; i = next[i]) if (flow[i] > 0) {
int v = to[i];
if (dis[v] > dis[u] + cost[i]) {
dis[v] = dis[u] + cost[i];
id[v] = i;
if (!vis[v]) sta[++top] = v, vis[v] = true;
}
}
}
return (dis[T] != INF);
}
int minCost() {
int u, v, aug, ret = 0;
while (SPFA()) {
aug = INF;
for (v = T; v != S; v = u) {
u = from[id[v]];
aug = min(aug, flow[id[v]]);
}
for (v = T; v != S; v = u) {
u = from[id[v]];
flow[id[v]] -= aug;
flow[id[v] ^ 1] += aug;
}
ret += aug * dis[T];
}
return ret;
}
int work(int k) {
for (int i = 1, en = 0; i < n; i++) {
flow[en++] = need[k][i], flow[en++] = 0;
flow[en++] = need[k][i], flow[en++] = 0;
flow[en++] = need[k][i], flow[en++] = 0;
flow[en++] = need[k][i], flow[en++] = 0;
for (int j = 1; j < n; j++) if (b[i] + p[i] + d[i][j] <= b[j])
flow[en++] = need[k][i], flow[en++] = 0;
}
return minCost();
}
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &n, &m);
scanf("%d%d", &x[0], &y[0]);
for (int i = 1; i < n; i++) {
scanf("%d%d%d%d", &x[i], &y[i], &b[i], &p[i]);
for (int j = 0; j < m; j++) scanf("%d", &need[j][i]);
}
#define sqr(x) ((x) * (x))
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) d[i][j] = d[j][i] = sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]));
#undef sqr
en = 0, S = 0, T = n, NV = 3 * n;
memset(head, -1, sizeof(head));
for (int i = 1; i < n; i++) {
addEdge(S, i, 1);
addEdge(S, i + n, 0);
addEdge(i, i + 2 * n, 0);
addEdge(i + 2 * n, T, 0);
for (int j = 1; j < n; j++) if (b[i] + p[i] + d[i][j] <= b[j])
addEdge(i + n, j + 2 * n, 0);
}
int ans = 0;
for (int i = 0; i < m; i++) ans += work(i);
printf("%d\n", ans);
}
return 0;
}
水题一枚,随便DP下即可。
#include
#include
#include
using namespace std;
const int N = 22;
int n, dp[N][N][N][N], a[N], b[N], sa[N], sb[N];
int DP(int s1, int t1, int s2, int t2) {
if (dp[s1][t1][s2][t2] != -1) return dp[s1][t1][s2][t2];
int ret = 0, sum = sa[t1] - sa[s1 - 1] + sb[t2] - sb[s2 - 1], tmp;
if (s1 <= t1) {
tmp = sum - DP(s1 + 1, t1, s2, t2);
ret = max(ret, tmp);
tmp = sum - DP(s1, t1 - 1, s2, t2);
ret = max(ret, tmp);
}
if (s2 <= t2) {
tmp = sum - DP(s1, t1, s2 + 1, t2);
ret = max(ret, tmp);
tmp = sum - DP(s1, t1, s2, t2 - 1);
ret = max(ret, tmp);
}
return dp[s1][t1][s2][t2] = ret;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
sa[0] = sb[0] = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sa[i] = sa[i - 1] + a[i];
for (int i = 1; i <= n; i++) scanf("%d", &b[i]), sb[i] = sb[i - 1] + b[i];
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= n; i++) dp[i][i][i][i] = max(a[i], b[i]);
printf("%d\n", DP(1, n, 1, n));
}
return 0;
}
这题还是要有些想法滴。做法是分两步:
1)对图进行奇偶染色,如果图中有奇环,则No,否则进行2);O(N^2)
2)在画图模拟中发现如果存在这样的四个点i,,j, k, l;其中i与j有边相连,k,l有边相连,其余的没有边直接相连,那么将构造不出有效的点权值来,此时是No,否则是Yes。O(N^3)
对于这两步,本人水平有效,暂时不能证明出这是充要条件。
#include
#include
#include
using namespace std;
const int N = 330;
bool flag;
int n, c[N], d[N];
char g[N][N];
void dfs(int i) {
if (!flag) return;
for (int j = 0; j < n; j++) if (g[i][j] == '1') {
if (c[j] == -1) {
c[j] = c[i] ^ 1;
dfs(j);
}
else if (c[j] == c[i]) { flag = false; break; }
}
}
int main() {
int T;
// freopen ("input.txt" , "r" , stdin);
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%s", g[i]);
flag = true;
for (int i = 0; i < n; i++) c[i] = -1;
for (int i = 0; i < n && flag; i++) if (c[i] == -1) {
c[i] = 0;
dfs(i);
}
if (!flag) puts("No");
else {
for (int i = 0; i < n; i++) if (c[i] == 1) {
d[i] = 0;
for (int j = 0; j < n; j++) if (g[i][j] == '1' && c[j] == 0) d[i]++;
}
for (int i = 0; i < n && flag; i++) if (c[i] == 1 && d[i] > 0) {
for (int j = i + 1 ; j < n && flag; j++) if ( c[j] == 1 && d[j] > 0) {
int tmp = 0;
for (int k = 0; k < n; k++) if (c[k] == 0 && g[i][k] == '1' && g[j][k] == '1') tmp++;
if (d[i] - tmp > 0 && d[j] - tmp > 0) flag = false;
}
}
if (flag) puts("Yes");
else puts("No");
}
}
return 0;
}
这题首先要推得有F(N) = (6^N - 1) / 5; H(N) = F(N) * 6; G(N) = 6M;
1)F(N)和H(N)的求得:
记dp[i]为已经掷出连续i个一样的的点数后到掷出连续N个一样点数时还需要掷色子的次数的期望,则F(N) = dp[1] + 1。有
dp[N] = 0;
dp[N - 1] = dp[N] * (1/ 6) + dp[1] * (5/6) +1;
... ...
dp[i] = dp[i + 1] * (1/6) + dp[1] * (5/6) + 1;
... ...
dp[1] = dp[2] * (1/6) + dp[1] * (5/6) + 1;
把前面N-1个式子带入第N个式子即得:dp[1] = dp[1] * (5/6) * [1 + 1/6 + (1/6)^2 +...+ (1/6)^(n-1)] + [1 + (1/6) + (1/6)^2 +...+ (1/6)^(n-1)];
利用等比数列求和即得F[N];
关于H(N) = F(N) * 6; 略。
2)求 G(M1) >= F(N); G(M2) >= H(N);
即 30M >= 6 ^ N - 1; 和 5M >= 6 ^ N - 1;
对于第一个式子(30M >= 6 ^ N - 1):这里将6^N 用快速幂化成(30k + x) *MOD + p的形式(MOD = 2011)。从前我们在快速幂中求的是就是p,顺便求出x不是很难,自行YY。然后算出w = (x * MOD + p - 1 + 30) % 30,即说6 ^ N - 1 除30得余数w,那么最小的M应该是M = ((30 * k + x) * MOD + p - 1 +30 + (30 - w)) / 30;这里的k就不用去求了,k对结果没有影响。
另外,如果觉得快速幂不好顺便求上面所说的x的话,可以直接快速幂6 ^ N % 30来求出6^N对30的余数w',则 w = (w' - 1 + MOD) % MOD。
两次快速幂也好,一次快速幂也好,算出p,w后,答案即:(p + (30 - w) + MOD) % MOD * inv(30, MOD),inv(30, MOD) 表示30对MOD的逆元。
第二个式子,同理可得。
代码略。