HDU 5988 费用流

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  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条路,然后是每个点的人和食物的数量,m条路是起点,终点,流量,概率。

每条边第一次经过不会触碰到电线,从第二次开始,每经过一次都有p的概率碰到,这条边最大允许通过是c,求如何让每个同学都取到食物而且碰到电线的概率最小。


题解:费用流。

定义每条边的费用是此边碰到线的概率

我们要优先走碰到概率小的边  但是概率不是线性增加的  所以要乘起来

最后跑出来答案为所有的都不损坏的概率  1-概率就是答案

建立一个虚拟源点和汇点  跑费用流即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const   int oo=1e9;//inf 
const   int mm=105*105*6;//边  
const   int mn=105;//点 
int node,src,dest,cnt; //点的个数  起点  终点   边数 
int ver[mm],flow[mm];
double cost[mm],dis[mn];//边权  距离 
int nex[mm];  
int head[mn],p[mn],q[mn],vis[mn],peo[mn],food[mn];
void prepare(int nodes,int srcc,int dests)  
{  
    node=nodes,src=srcc,dest=dests;  
    for(int i=0; i<node; i++)head[i]=-1,vis[i]=0;  
    cnt=0;  
}  
void addedge(int u,int v,double f,int c) //建边 
{  
    ver[cnt]=v,flow[cnt]=c,cost[cnt]=f,nex[cnt]=head[u],head[u]=cnt++;  
    ver[cnt]=u,flow[cnt]=0,cost[cnt]=-f,nex[cnt]=head[v],head[v]=cnt++;  
}  
bool spfa()  
{  
    int i,u,v,l,r=0,tmp;  
    for(i=0; i<node; ++i)dis[i]=0;  
    dis[q[r++]=src]=1;  
    p[src]=p[dest]=-1;  
    for(l=0; l!=r; (++l>=mn)?l=0:l)  
        for(i=head[u=q[l]],vis[u]=0; i>=0; i=nex[i])  
            if(flow[i]&&dis[v=ver[i]]<dis[u]*(1-cost[i]))  //边的距离  距离就是这条边不坏的概率  越大越好
            {  
                dis[v]=dis[u]*(1-cost[i]);  
                p[v]=i^1;  
                if(vis[v]) continue;  
                vis[q[r++]=v]=1;  
                if(r>=mn)r=0;  
            }  
    return p[dest]>-1;  
}  
double SpfaFlow()  
{  
    int i,delta;  
    double ret=1;
    while(spfa())  
    {  
        for(i=p[dest],delta=oo; i>=0; i=p[ver[i]])   
            if(flow[i^1]<delta)delta=flow[i^1];  
        for(i=p[dest]; i>=0; i=p[ver[i]])  
            flow[i]+=delta,flow[i^1]-=delta;  
        ret*=pow(dis[dest],delta);  //概率就是这条边  不坏的概率的n次方  n是走的次数
    }  
    return ret;  
}  
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m,i,j,x,y,cs;
		double fs;
		scanf("%d%d",&n,&m);
		prepare(mn,101,102);
		for(i=1;i<=n;i++){
			scanf("%d%d",&peo[i],&food[i]);	
		}
		for(i=1;i<=m;i++){
			scanf("%d%d%d%lf",&x,&y,&cs,&fs);
			addedge(x,y,fs,cs-1);//拆边  即拆为一条流量为1费用为0的和一条流量为原边-1  费用为原边的
			addedge(x,y,0,1);
		}
		int sta=101,en=102;
		for(i=1;i<=n;i++){
			if(peo[i]){
				addedge(sta,i,0,peo[i]);
			}
			if(food[i]){
				addedge(i,en,0,food[i]);
			}
		}
		printf("%.2f\n",1-SpfaFlow());
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值