Coding Contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1751    Accepted Submission(s): 374

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
 
Source
 
 
 
题意:有n个区域,每个区域有一些人数si和食物bi,区域之间有m条定向路径,每条路径有人数通过上限ci。路径之间铺了电线,每当有人通过路径时有pi的概率会触碰到电线,但是第一个通过的人一定不会触碰到电线。求每个人都通过路径获取到食物后触碰到电线的最小概率。
思路:首先每个区域的一些人都直接获取自己区域的食物,如果人过量,源点链接本区域,容量是过量的人数,费用是0;如果食物过量,本区域连接汇点,容量是过量的食物,费用是0。剩下的人通过路径到其他区域获取食物,m条路径之间建立边,首先是一定不会触碰,容量是1,花费为0,;接下来的才是容量为ci-1的边,并且还拥有一个费用,这就是一个最小费用最大流的问题,但是这个费用怎么转换呢?把每个概率取log(1-pi),并且取负数-log(1-pi)。这样的话最小花费(-log(x1))+(-log(x2))+(-log(x3))+....=-(log(x1)+log(x2)+log(x3)+...)=-log(x1*x2*x3...)。最后最小费用ans取负数再作为e的指数,即exp(-ans)是最大乘积。
 
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<double,int> P;
#define PI acos(-1.0)
const int maxn=,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
const double eps=0.000001;
struct edge
{
int from,to;
int cap;
double cost;
int rev;
};
int NN;
vector<edge>G[maxn];
double h[maxn];
///顶点的势,取h(u)=(s到u的最短距离),边e=(u,v)的长度变成d`(e)=d(e)+h(u)-h(v)>=0
double dist[maxn];
int prevv[maxn],preve[maxn];///前驱结点和对应的边
void addedge(int u,int v,int cap,double cost)
{
edge e;
e.from=u,e.to=v,e.cap=cap,e.cost=cost,e.rev=G[v].size();
G[u].push_back(e);
e.from=v,e.to=u,e.cap=,e.cost=-cost,e.rev=G[u].size()-;
G[v].push_back(e);
}
double min_cost_flow(int s,int t,int f)
{
double res=0.0;
fill(h,h+NN,0.0);
while(f>)
{
priority_queue<P,vector<P>,greater<P> >q;
fill(dist,dist+NN,inf);
dist[s]=0.0;
q.push(P(dist[s],s));
while(!q.empty())
{
P p=q.top();
q.pop();
int u=p.second;
if(dist[u]<p.first) continue;
for(int i=; i<G[u].size(); i++)
{
edge e=G[u][i];
if(e.cap>&&dist[e.to]-(dist[u]+e.cost+h[u]-h[e.to])>=eps)
{
dist[e.to]=dist[u]+e.cost+h[u]-h[e.to];
prevv[e.to]=u;
preve[e.to]=i;
q.push(P(dist[e.to],e.to));
}
}
}
if(fabs(dist[t]-inf)<=eps) return res;
for(int i=; i<NN; i++) h[i]+=dist[i];
int d=f;
for(int i=t; i!=s; i=prevv[i])
d=min(d,G[prevv[i]][preve[i]].cap);
f-=d;
res+=d*h[t];
//cout<<d<<" "<<h[t]<<" "<<d*h[t]<<endl;
for(int i=t; i!=s; i=prevv[i])
{
//cout<<i<<" ";
edge &e=G[prevv[i]][preve[i]];
e.cap-=d;
G[i][e.rev].cap+=d;
}
//cout<<s<<endl;
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
int s=,t=n+,f;
NN=t+;
for(int i=; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
int x=min(a,b);
a-=x,b-=x;
if(a>) addedge(s,i,a,);
else if(b>) addedge(i,t,b,);
}
for(int i=; i<=m; i++)
{
int u,v,cap;
double cost;
scanf("%d%d%d%lf",&u,&v,&cap,&cost);
if(cap>) addedge(u,v,,);
if(cap->) addedge(u,v,cap-,-*log(1.0-cost));
}
double sum=min_cost_flow(s,t,inf);
printf("%.2f\n",1.0-exp(-1.0*sum));
for(int i=; i<NN; i++) G[i].clear();
}
return ;
}

最小费用最大流

HDU 5988.Coding Contest 最小费用最大流的更多相关文章

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

    Problem DescriptionA coding contest will be held in this university, in a huge playground. The whole ...

  2. HDU5988/nowcoder 207G - Coding Contest - [最小费用最大流]

    题目链接:https://www.nowcoder.com/acm/contest/207/G 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5988 ...

  3. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  4. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

  5. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. POJ 2195 & HDU 1533 Going Home(最小费用最大流)

    这就是一道最小费用最大流问题 最大流就体现到每一个'm'都能找到一个'H',但是要在这个基础上面加一个费用,按照题意费用就是(横坐标之差的绝对值加上纵坐标之差的绝对值) 然后最小费用最大流模板就是再用 ...

  9. HDU 5988 Coding Contest 最小费用流 cost->double

    Problem Description A coding contest will be held in this university, in a huge playground. The whol ...

随机推荐

  1. SAP 自定义进度条

    *&---------------------------------------------------------------------* *& Report ZCHENH028 ...

  2. Oracle VM VirtulBox 安装Ubuntu16.04

    曾经自己在电脑中检索到Ubuntu kylin 16-10.vmdk 后就通过. 这种方式就进行了新建. 后自己从http://www.gaofumei.net/linux-download/783. ...

  3. Redis原子计数器incr

    一.前言在一些对高并发请求有限制的系统或者功能里,比如说秒杀活动,或者一些网站返回的当前用户过多,请稍后尝试.这些都是通过对同一时刻请求数量进行了限制,一般用作对后台系统的保护,防止系统因为过大的流量 ...

  4. Mysql 5.7 忘记root密码或重置密码的详细方法

    在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码 在Centos中安装完MySQL数据库以后,不知道密码,这可怎么办,下面给大家说一下怎么重置密码 1. ...

  5. centos7 搭建keepalived+Nginx+tomcat

    准备1台 192.168.2.224  安装Nginx,2台安装tomcat   192.168.2.222   192.168.2.223 1.安装Nginx: 上传pcre-8.36.tar.gz ...

  6. cdh5.13.1 hadoop hdfs HA模式无法启动

    经过观察日志发现,JN三个节点启动正常,只有NN节点启动时提示JN节点没有格式化 停止HDFS下面所有服务 先启动JN节点 然后启动一个NN节点,观察三个JN节点日志 发现其中一个节点的日志正常,没有 ...

  7. 第三章 列表(d)选择排序

  8. JTAG-测试数据寄存器

    1.问题:JTAG中的数据寄存器的结构 JTAG标准规定了两个必须的数据寄存器: 1.旁通寄存器 2.边界扫描寄存器(已经在可测性设计-扫描通路中介绍了) 可选的的寄存器有: 1.器件标示寄存器(32 ...

  9. C#的扩展方法解说

    扩展方法的目的就是为一个现有类型添加一个方法,现有类型既可以是int,string等数据类型,也可以是自定义的数据类型. 为数据类型的添加一个方法的理解:一般来说,int数据类型有个Tostring的 ...

  10. 利用crontab每天定时备份MySQL数据库

    当数据库服务器建立并正式投入生产使用后,我们不得不担忧一个问题:当数据库遭到破坏后,怎样安然恢复到最后一次正常的状态,使得数据的损失达到最小. 我这里以本博客的wordpress数据为例,来讨论并实现 ...