传送门
题目描述
你要抓神奇宝贝!
现在一共有
N
N
N 只神奇宝贝。
你有
a
a
a 个『宝贝球』和
b
b
b 个『超级球』。
『宝贝球』抓到第
i
i
i 只神奇宝贝的概率是
p
i
p_i
pi ,『超级球』抓到的概率则是
u
i
u_i
ui 。
不能往同一只神奇宝贝上使用超过一个同种的『球』,但是可以往同一只上既使用『宝贝球』又使用『超级球』(都抓到算一个)。
请合理分配每个球抓谁,使得你抓到神奇宝贝的总个数期望最大,并输出这个值。
分析
比较容易看出来是一个费用流,设一个超级源点和一个超级汇点,然后敲一个费用流的板子上去,很快啊,就
T
L
E
TLE
TLE了
后来发现这道题是实数费用流,在松弛操作那里需要加上
e
p
s
eps
eps在进行判断
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const double INF = 1e9;;
const int N = 1e5 + 10,M = 3 * N;
const ll mod = 1000000007;
const double eps = 1e-8;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],ne[M],e[M],f[M],incf[N],idx;
int n,m,S,T,a,b;
double pa[N],pb[N];
double w[M],d[N];
int q[N],pre[N];
bool st[N];
void add(int a,int b,int c,double d){
ne[idx] = h[a],e[idx] = b,f[idx] = c,w[idx] = d,h[a] = idx++;
ne[idx] = h[b],e[idx] = a,f[idx] = 0,w[idx] = -d,h[b] = idx++;
}
bool spfa(){
int hh = 0,tt = 1;
for(int i = 0;i < N;i++) d[i] = -INF;
q[0] = S,d[S] = 0;
while(hh != tt){
int t = q[hh++];
if(hh == N) hh = 0;
st[t] = false;
for(int i = h[t];~i;i = ne[i]){
int ver = e[i];
if(f[i] && d[ver] + eps < d[t] + w[i]){
pre[ver] = i;
d[ver] = d[t] + w[i];
if(!st[ver]){
q[tt++] = ver;
if(tt == N) tt = 0;
st[ver] = true;
}
}
}
}
return d[T] > -INF;
}
double EK(){
double cost = 0;
while(spfa()){
int t = 0x3f3f3f3f;
for(int i = T;i != S;i = e[pre[i] ^ 1]){
t = min(f[pre[i]],t);
}
for(int i = T;i != S;i = e[pre[i] ^ 1]){
f[pre[i]] -= t;
f[pre[i] ^ 1] += t;
}
cost += (double)t * d[T];
}
return cost;
}
int main() {
memset(h,-1,sizeof h);
read(n),read(a),read(b);
S = 0,T = 2 + n + 1;
add(S,1,a,0);
add(S,2,b,0);
for(int i = 1;i <= n;i++){
double x;
scanf("%lf",&x);
pa[i] = x;
add(1,i + 2,1,x);
}
for(int i = 1;i <= n;i++){
double x;
scanf("%lf",&x);
pb[i] = x;
add(2,i + 2,1,x);
add(i + 2,T,1,0);
add(i + 2,T,1,-pa[i] * pb[i]);
}
double res = EK();
printf("%.6lf\n",res);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
本文介绍了一种利用实数费用流算法来最大化抓取神奇宝贝期望数量的方法。通过构建特殊的图模型并应用费用流算法,文章详细展示了如何合理分配『宝贝球』和『超级球』以实现最优抓取。
1099

被折叠的 条评论
为什么被折叠?



