hdu 5988 Coding Contest (费用流)

思路:求网线被破坏的最小概率可以转化为不被破坏的最大概率来求,这个概率显然就是你要经过哪些点的(1-p)的乘积嘛,那么取个log就变成加法的了,令源点连向人数多余的点,汇点连向食物多余的点,跑一次最小费用最大流然后再取回对数就可以了

坑点:注意精度,在最短路松弛的时候要加个eps


#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn = 205;
#define inf 1e9
#define eps 1e-8
struct Edge
{
    int from,to,cap,flow;
	double cost;
	Edge(){}
	Edge(int f,int t,int c,int fl,double co):from(f),to(t),cap(c),flow(fl),cost(co){}
};
struct MCMF
{
    int n,m,s,t;
	vector<Edge>edges;
	vector<int>g[maxn];
	bool inq[maxn];
	double d[maxn];
	int p[maxn];
	int a[maxn];
	void init(int n,int s,int t)
	{
	    this->n=n;
		this->s=s;
		this->t=t;
		edges.clear();
		for(int i = 0;i<=n;i++)g[i].clear();
	}
	void AddEdge(int from,int to,int cap,double cost)
	{
	    edges.push_back(Edge(from,to,cap,0,cost));
		edges.push_back(Edge(to,from,0,0,-cost));
		m=edges.size();
		g[from].push_back(m-2);
		g[to].push_back(m-1);
	}
	bool BellmanFord(int &flow,double &cost)
	{
	    for(int i = 0;i<=n;i++)d[i]=inf;
		memset(inq,0,sizeof(inq));
		d[s]=0,a[s]=inf,inq[s]=1,p[s]=0;
		queue<int>q;
		q.push(s);
		while(!q.empty())
		{
		    int u = q.front();
			q.pop();
			inq[u]=0;
			for(int i = 0;i<g[u].size();i++)
			{
			    Edge &e = edges[g[u][i]];
				if(e.cap>e.flow&&d[e.to]>d[u]+e.cost+eps)
				{
				    d[e.to]=d[u]+e.cost;
					p[e.to]=g[u][i];
					a[e.to]=min(a[u],e.cap-e.flow);
					if(!inq[e.to])
					{
					    q.push(e.to);
						inq[e.to]=1;
					}
				}
			}
		}
		if(d[t]==inf)return false;
		flow+=a[t];
		cost+=a[t]*d[t];
		int u = t;
		while(u!=s)
		{
            edges[p[u]].flow+=a[t];
			edges[p[u]^1].flow-=a[t];
			u=edges[p[u]].from;
		}
		return true;
	}
    double Min_cost(int &flow,double &cost)
	{
	    flow=0,cost=0;
		while(BellmanFord(flow,cost));
		return cost;
	}
}mc;
int a[maxn],b[maxn],c[maxn];
int main()
{
    int T;
	scanf("%d",&T);
	while(T--)
	{
	    int n,m;
		scanf("%d%d",&n,&m);
		int s= 0,t=2*n+1;
		mc.init(n*2+1,s,t);
        for(int i = 0;i<n;i++)
		{
		    scanf("%d%d",&a[i],&b[i]);
			c[i]=a[i]-b[i];
		}
		for(int i = 0;i<m;i++)
		{
		    int u,v,c;double p;
			scanf("%d%d%d%lf",&u,&v,&c,&p);
			p = -log(1-p);
			if(c==0)continue;
			if(c==1)mc.AddEdge(u,v,1,0);
			else if(c>1)
			{
			    mc.AddEdge(u,v,1,0);
			    mc.AddEdge(u,v,c-1,p);
			}
		}
		for(int i = 0;i<n;i++)
		{
		    if(c[i]==0)continue;
			if(c[i]>0)mc.AddEdge(s,i+1,c[i],0);
			if(c[i]<0)mc.AddEdge(i+1,t,-c[i],0);
		}
		int flow=0;double cost=0;
		cost = mc.Min_cost(flow,cost);
		cost = exp(-cost);
		printf("%.2lf\n",1-cost);
	}
}  

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
 

Source

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值