hdu-5988 Coding Contest(费用流)

时间:2022-01-02 08:47:33

题目链接:

Coding Contest

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 65536/65536 K (Java/Others)

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 sicompetitors 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条边,每个点有si个人,bi份食物,每条边一开始可以通过一个人,后来的人每通过一个就有pi的概率使整个系统崩溃,问崩溃的最小的概率是多少;
思路:
求出概率后取log后变成了费用流的模型,然后一搞就好了,RP如此重要;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=500;
const double inf=1e9;
const double eps=1e-8;
struct Edge
{
int from,to,cap,flow;
double cost;
};
int n,m,s,t,M;
std::vector<int> G[maxn];
std::vector<Edge> edge;
int inq[maxn],a[maxn],p[maxn];
double d[maxn];
inline void add_edge(int from,int to,int cap,double cost)
{
edge.push_back((Edge){from,to,cap,0,cost});
edge.push_back((Edge){to,from,0,0,-cost});
m=edge.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bellmanford(int &flow,double &cost)
{
for(int i=0;i<=t+1;i++)d[i]=inf;
memset(inq,0,sizeof(inq));
d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;
queue<int>qu;
qu.push(s);
while(!qu.empty())
{
int fr=qu.front();qu.pop();
inq[fr]=0;
int len=G[fr].size();
for(int i=0;i<len;i++)
{
Edge& e=edge[G[fr][i]];
if(e.cap>e.flow&&d[e.to]>d[fr]+e.cost+eps)
{
d[e.to]=d[fr]+e.cost;
p[e.to]=G[fr][i];
a[e.to]=min(a[fr],e.cap-e.flow);
if(!inq[e.to]){qu.push(e.to);inq[e.to]=1;}
}
}
}
if(d[t]>=inf)return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s)
{
edge[p[u]].flow+=a[t];
edge[p[u]^1].flow-=a[t];
u=edge[p[u]].from;
}
return true;
}
double mincostflow()
{
int flow=0;double cost=0;
while(bellmanford(flow,cost));
return cost;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int u,v,w;
double xp;
scanf("%d%d",&n,&M);
s=0,t=n+1;
edge.clear();
for(int i=0;i<=t;i++)G[i].clear();
for(int i=1;i<=n;i++)
{
scanf("%d%d",&u,&v);
add_edge(s,i,u,0.0);
add_edge(i,t,v,0.0);
}
for(int i=1;i<=M;i++)
{
scanf("%d%d%d%lf",&u,&v,&w,&xp);
xp=-log(1-xp);
add_edge(u,v,w-1,xp);
add_edge(u,v,1,0.0);
}
//cout<<"&&&&\n";
double ans=-mincostflow();
printf("%.2f\n",1-exp(ans));
}
return 0;
}