• 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^(-最大不断概率)

    代码

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 
      4 const int N=1e5+5;
      5 const int M=2e5+5;
      6 const int INF=0x3f3f3f3f;
      7 
      8 int FIR[N],FROM[M],TO[M],CAP[M],FLOW[M],NEXT[M],tote;
      9 double COST[M],dist[N];
     10 int pre[N],q[400000];
     11 bool vis[N];
     12 int n,m,S,T;
     13 void init()
     14 {
     15     tote=0;
     16     memset(FIR,-1,sizeof(FIR));
     17 }
     18 void addEdge(int u,int v,int cap,double cost)
     19 {
     20     FROM[tote]=u;
     21     TO[tote]=v;
     22     CAP[tote]=cap;
     23     FLOW[tote]=0;
     24     COST[tote]=cost;
     25     NEXT[tote]=FIR[u];
     26     FIR[u]=tote++;
     27 
     28     FROM[tote]=v;
     29     TO[tote]=u;
     30     CAP[tote]=0;
     31     FLOW[tote]=0;
     32     COST[tote]=-cost;
     33     NEXT[tote]=FIR[v];
     34     FIR[v]=tote++;
     35 }
     36 bool SPFA(int s, int t)
     37 {
     38     for(int i=0;i<=n+1;i++)
     39     {
     40         dist[i]=1e9;
     41         vis[i]=0;
     42         pre[i]=-1;
     43     }
     44     dist[s]=0;vis[s]=true;q[1]=s;
     45     int head=0,tail=1;
     46     while(head!=tail)
     47     {
     48         int u=q[++head];vis[u]=false;
     49         for(int v=FIR[u];v!=-1;v=NEXT[v])
     50         {
     51             if(dist[TO[v]]>dist[u]+COST[v]+1e-8&&CAP[v]>FLOW[v])
     52             {
     53                 dist[TO[v]]=dist[u]+COST[v];
     54                 pre[TO[v]]=v;
     55                 if(!vis[TO[v]])
     56                 {
     57                     vis[TO[v]] = true;
     58                     q[++tail]=TO[v];
     59                 }
     60             }
     61         }
     62     }
     63     return pre[t]!=-1;//可达返回true
     64 }
     65 void MCMF(int s, int t, double &cost, int &flow)
     66 {
     67     flow=0;
     68     cost=0;
     69     while(SPFA(s,t))
     70     {
     71         int Min=INF;
     72         for(int v=pre[t];v!=-1;v=pre[TO[v^1]])
     73             Min=min(Min,CAP[v]-FLOW[v]);
     74         for(int v=pre[t];v!=-1;v=pre[TO[v^1]])
     75         {
     76             FLOW[v]+=Min;
     77             FLOW[v^1]-=Min;
     78             cost+=COST[v]*Min;
     79         }
     80         flow+=Min;
     81     }
     82 }
     83 int main()
     84 {
     85     int t;
     86     scanf("%d",&t);
     87     while(t--)
     88     {
     89         scanf("%d%d",&n,&m);
     90         init();
     91         S=0,T=n+1;
     92         for(int i=1,s,b;i<=n;i++)
     93         {
     94             scanf("%d%d",&s,&b);
     95             if(s>b)addEdge(S,i,s-b,0.0);
     96             if(s<b)addEdge(i,T,b-s,0.0);
     97         }
     98         double p;
     99         for(int i=0,u,v,c;i<m;i++)
    100         {
    101             scanf("%d%d%d%lf",&u,&v,&c,&p);
    102             if(c>0)addEdge(u,v,1,0.0);
    103             if(c>1)addEdge(u,v,c-1,-log10(1.0-p));
    104         }
    105         int flow;
    106         double cost;
    107         MCMF(S,T,cost,flow);
    108         printf("%.2f
    ",1.0-pow(10.0,-cost));
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    再学 GDI+[47]: 路径 CloseFigure
    再学 GDI+[46]: 路径 Create、FillPath、DrawPath
    学习官方示例 TApplication.ExeName
    再学 GDI+[45]: 文本输出 在矩形中格式化输出
    再学 GDI+[50]: 路径 GetPathPoints、GetPathTypes、TPathData、GetPathData
    再学 GDI+[49]: 路径 GetPointCount、GetPathPoints、GetLastPoint、GetBounds
    再学 GDI+[48]: 路径 StartFigure、CloseFigure、CloseAllFigures
    微软DevWow博客达人征文大赛获奖名单
    开始组建博客园北京俱乐部
    .NET技术大会
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9766072.html
Copyright © 2020-2023  润新知