1. 题目描述
有$k$个伞兵跳伞,有$m$个汇点。当伞兵着陆后,需要走向离他最近的汇点。如何选择这$m$个结点,可以使得士兵最终行走的距离的期望最小。
求这个最小的期望。
2. 基本思路
假设已经选好了这$m$个汇点,这也就确定了每个士兵的可能着陆点需要继续行走的距离。因此,这个期望为
\begin{align}
E_{min} &= \sum_{i=1}^{k} \sum_{j=1}^{L_i} minDis(X_{ij}) * P_{ij}
\end{align}
显然这等价于
\begin{align}
xst &= set(X), nx = |xst| \\
E_{min} &= \sum_{i=1}^{nx} minDis(xst_i) * \sum_{i=1}^{k} \sum_{j=1}^{L_i} (X_{ij}==xst_i) * P{ij}
\end{align}
这也就意味着,最终的最小期望值与$k$大小无关,与$nx$大小有关,$\sum_{i=1}^{k} \sum_{j=1}^{L_i} (X_{ij}==xst_i) * P{ij} $其实是针对某个x的累加概率和。
而$nx \leq 1000$。因此,不妨设$dp[i][j]$表示当仅仅有i个汇点时,前j个x的最小期望值。显然有状态转移方程
\begin{align}
dp[i][j] &= \min (dp[i-1][k] + cost[k+1][j]), k \in [1, j)
\end{align}
关键就是求$cost[i][j]$,表示区间$[i,j]$内的最小费用(仅包含1个汇点)。
而这显然是一个区间DP。有如下推导
\begin{align*}
cost_k &= \sum_{i=1}^{k} (x_k-x_i) \cdot P_i + \sum_{i=k+1}^{n} (x_i-x_k) \cdot P_i \\
x_{k+1} &= x_k + \Delta \\
cost_{k+1} &= \sum_{i=1}^{k+1} (x_{k+1}-x_i) \cdot P_i + \sum_{i=k+2}^{n} (x_i-x_{k+1}) \cdot P_i \\
&= \sum_{i=1}^{k+1} (x_k+\Delta-x_i) \cdot P_i + \sum_{i=k+2}^{n} (x_i-x_k-\Delta) \cdot P_i \\
&= \sum_{i=1}^{k+1} (x_k-x_i) \cdot P_i + \sum_{i=k+2}^{n} (x_i-x_k) \cdot P_i + \Delta (\sum_{i=1}^{k+1}P_i - \sum_{i=k+2}^{n}P_i) \\
&= \sum_{i=1}^{k} (x_k-x_i) \cdot P_i + \sum_{i=k+1}^{n} (x_i-x_k) \cdot P_i - 2\Delta \cdot P_{k+1} + \Delta (\sum_{i=1}^{k+1}P_i - \sum_{i=k+2}^{n}P_i) \\
&= cost_k + \Delta (\sum_{i=1}^{k}P_i + P_{k+1} - \sum_{i=k+1}^{n}P_i + P_{k+1} - 2P_{k+1}) \\
&= cost_k + \Delta (\sum_{i=1}^{k}P_i - \sum_{i=k+1}^{n}P_i)
\end{align*}
这让我在已知$x_k$为$[1,n]$的汇点时,可以快速求出当$x_{k+1}$为汇点时的费用值。$\Delta > 0$恒成立,而$\sum_{i=1}^{k}P_i - \sum_{i=k+1}^{n}P_i$也是单调递增的,因此求最优的$x_k$时可以使用单调性优化。
3. 代码
/* 4412 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#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 node_t {
int x;
double p; node_t() {}
node_t(int x, double p):
x(x), p(p) {} friend bool operator< (const node_t& a, const node_t& b) {
return a.x < b.x;
} } node_t; const double INF = 1e18;
const int maxn = ;
const int maxm = ;
double cost[maxn][maxn];
double dp[maxm][maxn];
map<int,double> tb;
map<int,double>::iterator iter;
node_t nd[maxn];
int n, m; void init_Cost(int n) {
double mn, tmp; rep(i, , n) {
int k = i;
double pl = nd[i].p, pr = 0.0; mn = 0.0;
cost[i][i] = ; rep(j, i+, n) {
pr += nd[j].p;
mn += (nd[j].x - nd[k].x) * nd[j].p;
while (k<j && mn > mn + (nd[k+].x-nd[k].x) * (pl - pr)) {
mn += (nd[k+].x-nd[k].x) * (pl - pr);
++k;
pl += nd[k].p;
pr -= nd[k].p;
}
cost[i][j] = mn;
}
}
} void solve() {
int pn = ; for (iter=tb.begin(); iter!=tb.end(); ++iter) {
nd[pn].x = iter->fir;
nd[pn].p = iter->sec;
++pn;
} sort(nd, nd+pn); init_Cost(pn); rep(j, , pn)
dp[][j] = cost[][j]; rep(i, , m) {
rep(j, , pn) {
dp[i][j] = dp[i-][j];
rep(k, , j)
dp[i][j] = min(dp[i][j], dp[i-][k]+cost[k+][j]);
}
} double ans = INF;
ans = min(ans, dp[m-][pn-]);
printf("%.2lf\n", ans);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int l;
int x;
double p; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
tb.clr();
rep(i, , n) {
scanf("%d", &l);
rep(j, , l) {
scanf("%d%lf", &x, &p);
tb[x] += p;
}
}
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}