zoukankan      html  css  js  c++  java
  • HDU5988 Coding Contest(浮点费用流)

    题意:

    n(100)个点,每个点有人数和食物数,m(5000)条边,每条边有有几率破坏网络,第一次走的话保证不会破坏网络,

    然后再走就会有p的概率破坏网络,每条边有最大走的次数c(100)。

    现在可以通过边来移动人,使得每个人都有食物,题目保证有解,问你破坏网络的最小概率

    思路:

    先把第一次单拿出来之后,每次都有p的概率,那么不破坏的概率就是1-p

    然后多次之后不破坏的概率就是(1-p)^n,用取对数把它变成加法,把多的人连向原点,多的食物连向汇点,跑费用流然后再还原概率就可以了

    有个坑,不加eps会T

    /* ***********************************************
    Author        :devil
    ************************************************ */
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <stack>
    #include <map>
    #include <string>
    #include <time.h>
    #include <cmath>
    #include <stdlib.h>
    #define LL long long
    #define rep(i,a,b) for(int i=a;i<=b;i++)
    #define dep(i,a,b) for(int i=a;i>=b;i--)
    #define ou(a) printf("%d
    ",a)
    #define pb push_back
    #define pii pair<int,int>
    #define mkp make_pair
    #define IN freopen("in.txt","r",stdin);
    #define OUT freopen("out.txt","w",stdout);
    using namespace std;
    const int inf=0x3f3f3f3f;
    const int mod=1e9;
    const int N=1e2+10;
    const int M=3e4+10;
    const double eps=1e-6;
    int head[N],eid;
    int n,m,x,y,c;
    double p;
    double dis[N];
    bool vis[N];
    int a[N];
    int pre[N];
    struct node
    {
        int u,v,cap,next;
        double cost;
    }eg[M];
    void add(int u,int v,int cap,double cost)
    {
        eg[eid].u=u;
        eg[eid].v=v;
        eg[eid].cap=cap;
        eg[eid].next=head[u];
        eg[eid].cost=cost;
        head[u]=eid++;
        eg[eid].u=v;
        eg[eid].v=u;
        eg[eid].cap=0;
        eg[eid].next=head[v];
        eg[eid].cost=-cost;
        head[v]=eid++;
    }
    bool spfa(int s,int t,int &flow,double &cost)
    {
        memset(vis,false,sizeof(vis));
        for(int i=0;i<N;i++)
            dis[i]=inf;
        dis[s]=0;
        vis[s]=true;
        pre[s]=0;
        a[s]=inf;
        queue<int>q;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            vis[u]=false;
            for(int i=head[u];i!=-1;i=eg[i].next)
            {
                if(eg[i].cap&&dis[eg[i].v]>dis[u]+eg[i].cost+eps)
                {
                    dis[eg[i].v]=dis[u]+eg[i].cost;
                    pre[eg[i].v]=i;
                    a[eg[i].v]=min(a[u],eg[i].cap);
                    if(!vis[eg[i].v])
                    {
                        q.push(eg[i].v);
                        vis[eg[i].v]=true;
                    }
                }
            }
        }
        if(dis[t]==inf) return false;
        flow+=a[t];
        cost+=dis[t]*a[t];
        int u=t;
        while(u!=s)
        {
            eg[pre[u]].cap-=a[t];
            eg[pre[u]^1].cap+=a[t];
            u=eg[pre[u]].u;
        }
        return true;
    }
    double mincost(int s,int t)
    {
        int flow=0;
        double cost=0;
        while(spfa(s,t,flow,cost));
        return cost;
    }
    void init()
    {
        memset(head,-1,sizeof(head));
        eid=0;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            init();
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++)
            {
                scanf("%d%d",&x,&y);
                x-=y;
                if(x>0) add(0,i,x,0);
                else if(x<0) add(i,n+1,-x,0);
            }
            while(m--)
            {
                scanf("%d%d%d%lf",&x,&y,&c,&p);
                if(!c) continue;
                add(x,y,1,0);
                if(c>1) p=-log(1-p),add(x,y,c-1,p);
            }
            double ans=mincost(0,n+1);
            printf("%.2f
    ",1-exp(-ans));
        }
        return 0;
    }
  • 相关阅读:
    leetcode 131. Palindrome Partitioning
    leetcode 526. Beautiful Arrangement
    poj 1852 Ants
    leetcode 1219. Path with Maximum Gold
    leetcode 66. Plus One
    leetcode 43. Multiply Strings
    pytorch中torch.narrow()函数
    pytorch中的torch.repeat()函数与numpy.tile()
    leetcode 1051. Height Checker
    leetcode 561. Array Partition I
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/6106355.html
Copyright © 2011-2022 走看看