zoukankan      html  css  js  c++  java
  • Paratroopers

    Paratroopers
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 7881 Accepted: 2373

    Description

    It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the m × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.

    In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.

    Input

    Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.

    Output

    For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.

    Sample Input

    1
    4 4 5
    2.0 7.0 5.0 2.0
    1.5 2.0 2.0 8.0
    1 1
    2 2
    3 3
    4 4
    1 4

    Sample Output

    16.0000

    Source
    Amirkabir University of Technology Local Contest 2006
    好恶心的题啊,一直超时,后来也没有怎么改经过一大波的TLE后就过了,好奇怪,难道精度没有控制好?

    #include <map>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const double  INF = 10000.0;
    
    const double eps = 1e-8;
    
    const int Max = 3000;
    
    struct Edge
    {
        int v;
        int next;
        double cap;
    }E[Max];
    
    int Head[120];
    int Du[120];
    int top;
    int n,m,L;
    int s,t;
    void AddEdge(int u,int v,double w)
    {
        E[top].cap=w;E[top].v=v;
        E[top].next=Head[u];Head[u]=top++;
        E[top].cap=0;E[top].v=u;
        E[top].next=Head[v];Head[v]=top++;
    }
    double Eps(double s)
    {
        return fabs(s)<eps?0:s;
    }
    double min(double a,double b)
    {
        return a<b?a:b;
    }
    int bfs()
    {
        memset(Du,0,sizeof(Du));
        queue<int>Q;
        Du[s]=1;
        Q.push(s);
        while(!Q.empty())
        {
            int a=Q.front();
            Q.pop();
            for(int i=Head[a];i!=-1;i=E[i].next)
            {
                if(Du[E[i].v]==0&&Eps(E[i].cap)>0)
                {
                    Du[E[i].v]=Du[a]+1;
                    Q.push(E[i].v);
                }
            }
        }
        return Du[t];
    }
    
    
    double dfs(int star,double num)
    {
    
        if(star==t)
        {
            return num;
        }
        double S=0;
        double ant;
        for(int i=Head[star];i!=-1;i=E[i].next)
        {
            if(Du[star]+1==Du[E[i].v]&&Eps(E[i].cap)>0)
            {
                ant=dfs(E[i].v,min(E[i].cap,num));
                E[i].cap-=ant;
                E[i^1].cap+=ant;
                num-=ant;
                S+=ant;
                if(Eps(num)==0)
                {
                    break;
                }
            }
        }
        return S;
    }
    double Dinic()
    {
        double ant=0;
        while(bfs())
        {
            ant+=dfs(0,INF);
        }
        return ant;
    }
    
    int main()
    {
        int T;
        double w;
        int u,v;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d %d",&n,&m,&L);
            s=0;
            t=n+m+1;
            top=0;
            memset(Head,-1,sizeof(Head));
            for(int i=1;i<=n;i++)
            {
                scanf("%lf",&w);
                AddEdge(s,i,log(w));
            }
            for(int i=1;i<=m;i++)
            {
                scanf("%lf",&w);
                AddEdge(n+i,t,log(w));
            }
            for(int i=1;i<=L;i++)
            {
                scanf("%d %d",&u,&v);
                AddEdge(u,v+n,INF);
            }
            printf("%.4f
    ",exp(Dinic()));
        }
        return 0;
    }
    
  • 相关阅读:
    曹工说Redis源码(8)--面试时,redis 内存淘汰总被问,但是总答不好
    曹工说JDK源码(4)--抄了一小段ConcurrentHashMap的代码,我解决了部分场景下的Redis缓存雪崩问题
    曹工说JDK源码(3)--ConcurrentHashMap,Hash算法优化、位运算揭秘
    曹工说JDK源码(2)--ConcurrentHashMap的多线程扩容,说白了,就是分段取任务
    曹工说JDK源码(1)--ConcurrentHashMap,扩容前大家同在一个哈希桶,为啥扩容后,你去新数组的高位,我只能去低位?
    曹工说Spring Boot源码(29)-- Spring 解决循环依赖为什么使用三级缓存,而不是二级缓存
    曹工说mini-dubbo(2)--分析eureka client源码,想办法把我们的服务提供者注册到eureka server(上)
    @Spring Boot程序员,我们一起给程序开个后门吧:让你在保留现场,服务不重启的情况下,执行我们的调试代码
    python处理txt大文本文件
    Matlab读写文件时的定位
  • 原文地址:https://www.cnblogs.com/juechen/p/5256013.html
Copyright © 2011-2022 走看看