cf739E Gosha is hunting (flows)

739E

There are $ a $ ordinary balls, $ b $ super ball, there are $ n $ th to capture the treasure can dream, for the first $ i $ a treasure can dream of an ordinary ball capture probability is $ p_i $, super ball catch probability is $ u_i $, each can only throw a ball to the same treasure can dream the same dream can be a treasure can throw two kinds of balls. Then ask the number of captures in the optimal policy expectations

Consider the probability of $ dp $, we found that the state can not be reduced to $ n ^ 2 $ level,It is not dp

Assume that each treasure can only be a dream to throw a ball that matches the problem, set $ A $ as an ordinary ball, $ B $ super ball, the source point to the $ A $, $ B $ and even the capacity of the ball number, cost of $ 0 $ side, $ a, B $ respectively each sprite even a capacity of $ 1 $, cost is $ P_i $ or $ u_i $ edges and each sprite to sink even capacity of $ 1 $ at a cost of $ 0 $ sides, the biggest cost is the cost flow of the answer.

Since a wizard can simultaneously throwing two balls, when he threw two balls, the probability of capture is $ 1- (1-p_i) times (1-u_i) $, simplification was $ p_i + u_i-p_i times u_i $ , this is actually the equivalent of $ a, B $ while the capacity of $ 1 $ flows through this wizard, if there is no $ p_i times u_i $ each sprite can be connected to the sink two sides, each side capacity of $ 1 $ and the cost is $ 0 $, however, to deal with this $ p_i times u_i $ can be considered one of the two sides in the cost changed to $ -p_i times u_i $, because the maximum cost flow path of the longest running time will certainly take priority spending $ 0 $ the piece, walk $ -p_i times u_i $ of this, while walking two and only when the two balls are thrown at the same time a wizard, and this time the cost just for the $ p_i + u_i-p_i times u_i $, is proved.

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      
      
using namespace std;
const double eps= 1e-8;
const int maxn = 2000 + 5 ;
int n,a,b;
struct {
int to;
double cost; int cap,rev;
edge( int to= 0, double cost= 0, int cap= 0, int rev= 0): to(to),cost(cost),cap(cap),rev(rev) {}
};
vector<edge> g[maxn];
void addedge(int from,int to,int cap,double cost)
{
cost=-cost;
g[from].push_back(edge(to,cost,cap,g[to].size()));
g[to].push_back(edge(from,-cost, 0,g[from].size() -1));
}
double p[maxn],u[maxn];
int S,T,A,B;
#define MP make_pair
bool inque[maxn];
double dis[maxn];
int preve[maxn],prevv[maxn];
queue< int > q;
double spfa()
{
for( int i=S;i<=B;i++) dis[i]= 1e60,inque[i]= false;
dis[S]= 0;inque[S]= true;
while(!q.empty()) q.pop();
q.push(S);
memset(prevv, 0, sizeof(prevv)); memset(preve, 0, sizeof(preve));
while(!q.empty())
{
int u=q.front();q.pop();inque[u]= false;
for( int i= 0;i<( int)g[u].size();i++){
edge &e=g[u][i];
if(e.cap && dis[e.to]-(dis[u]+e.cost)>eps) {
dis[e.to]=dis[u]+e.cost;
preve[e.to]=i,prevv[e.to]=u;
if(!inque[e.to]){
inque[e.to]= true;
q.push(e.to);
}
}
}
}
if(dis[T]>= 1e60) return 0;
int gap=INT_MAX;
for( int i=T;i!=S;i=prevv[i]) gap=min(gap,g[prevv[i]][preve[i]].cap);
for( int i=T;i!=S;i=prevv[i]){
edge &e=g[prevv[i]][preve[i]];
e.cap-=gap;
g[i][e.rev].cap+=gap;
}
return dis[T]*( double)gap;
}
double MaxcostMaxflow()
{
double res= 0,ret= 0;
while((res=spfa())!= 0) ret+=res,res= 0;
return ret;
}
int main()
{
scanf( "%d%d%d",&n,&a,&b);
for( int i= 1;i<=n;i++) scanf( "%lf",&p[i]);
for( int i= 1;i<=n;i++) scanf( "%lf",&u[i]);
S= 0,T=n+ 1,A=n+ 2,B=n+ 3;
addedge(S,A,a, 0),addedge(S,B,b, 0);
for( int i= 1;i<=n;i++) addedge(A,i, 1,p[i]),addedge(B,i, 1,u[i]),addedge(i,T, 1, 0),addedge(i,T, 1,-p[i]*u[i]);
printf( "%.4lfn",-MaxcostMaxflow());
return 0;
}

原文:大专栏  cf739E Gosha is hunting (flows)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11615146.html