HDU 5988 Coding Contest(最小费用最大流变形)

Problem Description
A coding contest will be held in this university, in a huge playground. The whole playground would be divided into N blocks, and there would be M directed paths linking these blocks. The i-th path goes from the ui-th block to the vi-th block. Your task is to solve the lunch issue. According to the arrangement, there are si competitors in the i-th block. Limited to the size of table, bi bags of lunch including breads, sausages and milk would be put in the i-th block. As a result, some competitors need to move to another block to access lunch. However, the playground is temporary, as a result there would be so many wires on the path.
For the i-th path, the wires have been stabilized at first and the first competitor who walker through it would not break the wires. Since then, however, when a person go through the i - th path, there is a chance of pi to touch
the wires and affect the whole networks. Moreover, to protect these wires, no more than ci competitors are allowed to walk through the i-th path.
Now you need to find a way for all competitors to get their lunch, and minimize the possibility of network crashing.

Input
The first line of input contains an integer t which is the number of test cases. Then t test cases follow.
For each test case, the first line consists of two integers N (N ≤ 100) and M (M ≤ 5000). Each of the next N lines contains two integers si and bi (si , bi ≤ 200).
Each of the next M lines contains three integers ui , vi and ci(ci ≤ 100) and a float-point number pi(0 < pi < 1).
It is guaranteed that there is at least one way to let every competitor has lunch.

Output
For each turn of each case, output the minimum possibility that the networks would break down. Round it to 2 digits.

Sample Input
1
4 4
2 0
0 3
3 0
0 3
1 2 5 0.5
3 2 5 0.5
1 4 5 0.5
3 4 5 0.5

Sample Output
0.50

题意

n个点m条边,每个点有s个人,b个食物,每条单向边u,v,c,p,c为一条边最多经过c次,p为路断的概率,第一个人经过一定不会断,第二个人开始每个人有p的概率使得路断

问每个人都有食物并且使得破坏网络概率最小

题解

显然不能直接算路断的概率,要算最大不断概率,再用1-它

可以知道如果有a条边被破坏,那么概率就是(1-p)^a

最小费用流跑得是加法,显然得变成乘法,可以两边取对数log10,这样跑的话是最小不断概率,再同*-1,就可以得到最大了

还有一点第一个人经过不会断,可以单独拿1流量概率为0就行了

注意在SPFA跑的时候会有浮点数的比较,需要加1个eps,不然会TLE

答案就是10^(-最大不断概率)

代码

 #include<bits/stdc++.h>
using namespace std; const int N=1e5+;
const int M=2e5+;
const int INF=0x3f3f3f3f; int FIR[N],FROM[M],TO[M],CAP[M],FLOW[M],NEXT[M],tote;
double COST[M],dist[N];
int pre[N],q[];
bool vis[N];
int n,m,S,T;
void init()
{
tote=;
memset(FIR,-,sizeof(FIR));
}
void addEdge(int u,int v,int cap,double cost)
{
FROM[tote]=u;
TO[tote]=v;
CAP[tote]=cap;
FLOW[tote]=;
COST[tote]=cost;
NEXT[tote]=FIR[u];
FIR[u]=tote++; FROM[tote]=v;
TO[tote]=u;
CAP[tote]=;
FLOW[tote]=;
COST[tote]=-cost;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
bool SPFA(int s, int t)
{
for(int i=;i<=n+;i++)
{
dist[i]=1e9;
vis[i]=;
pre[i]=-;
}
dist[s]=;vis[s]=true;q[]=s;
int head=,tail=;
while(head!=tail)
{
int u=q[++head];vis[u]=false;
for(int v=FIR[u];v!=-;v=NEXT[v])
{
if(dist[TO[v]]>dist[u]+COST[v]+1e-&&CAP[v]>FLOW[v])
{
dist[TO[v]]=dist[u]+COST[v];
pre[TO[v]]=v;
if(!vis[TO[v]])
{
vis[TO[v]] = true;
q[++tail]=TO[v];
}
}
}
}
return pre[t]!=-;//可达返回true
}
void MCMF(int s, int t, double &cost, int &flow)
{
flow=;
cost=;
while(SPFA(s,t))
{
int Min=INF;
for(int v=pre[t];v!=-;v=pre[TO[v^]])
Min=min(Min,CAP[v]-FLOW[v]);
for(int v=pre[t];v!=-;v=pre[TO[v^]])
{
FLOW[v]+=Min;
FLOW[v^]-=Min;
cost+=COST[v]*Min;
}
flow+=Min;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
S=,T=n+;
for(int i=,s,b;i<=n;i++)
{
scanf("%d%d",&s,&b);
if(s>b)addEdge(S,i,s-b,0.0);
if(s<b)addEdge(i,T,b-s,0.0);
}
double p;
for(int i=,u,v,c;i<m;i++)
{
scanf("%d%d%d%lf",&u,&v,&c,&p);
if(c>)addEdge(u,v,,0.0);
if(c>)addEdge(u,v,c-,-log10(1.0-p));
}
int flow;
double cost;
MCMF(S,T,cost,flow);
printf("%.2f\n",1.0-pow(10.0,-cost));
}
return ;
}
上一篇:Coding Contest(费用流变形题,double)


下一篇:HDU 5988 Coding Contest 最小费用流 cost->double