zoukankan      html  css  js  c++  java
  • poj3308 最小点权覆盖

    Paratroopers
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8837   Accepted: 2663

    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


    首先用log化×为+,然后源点S向每一行连边,容量是log10(r[i]),每一列向T连边,容量是log10(c[i]),
    然后对于每个attacker,把其对应的行列连起来,容量是INF,跑最大流就可以了。

    马虎的错误
    T和样例个数的T又重了,memset(head)忘掉

    然后INF不能开大,开的大一点就WA,同时判断增路的时候要用fabs判断

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=580;
    const int M=60008;
    const double INF=10.0;
    int head[N],tot,S,T;
    int q[N],dis[N],n,m,Q;
    bool vis[N];
    struct node
    {
        int next,v;
        double w;
    } e[M<<2];
    void add(int u,int v,double w)
    {
        e[tot].v=v;
        e[tot].w=w;
        e[tot].next=head[u];
        head[u]=tot++;
    }
    bool bfs()
    {
        memset(dis,-1,sizeof(dis));
        dis[S]=0;
        int l=0,r=0;
        q[r++]=S;
        while(l<r)
        {
            int u=q[l++];
            for(int i=head[u]; ~i; i=e[i].next)
            {
                int v=e[i].v;
                if(dis[v]==-1&&fabs(e[i].w-0)>1e-8)
                {
                    q[r++]=v;
                    dis[v]=dis[u]+1;
                    if(v==T)  return true;
                }
            }
        }
        return false;
    }
    double dfs(int s,double low)
    {
        if(s==T||!low) return low;
        double ans=low,a;
        for(int i=head[s]; ~i; i=e[i].next)
        {
            if(fabs(e[i].w-0)>1e-8&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans))))
            {
                e[i].w-=a;
                e[i^1].w+=a;
                ans-=a;
                if(fabs(ans-0)<1e-8) return low;
            }
        }
        if(low==ans) dis[s]=-1;
        return low-ans;
    }
    int main(){
        int Ta,r,c;
        for(scanf("%d",&Ta);Ta--;){
            scanf("%d%d%d",&n,&m,&Q);
            S=0,T=n+m+1;
            memset(head,-1,sizeof(head));
            tot=0;
            double x;
            for(int i=1;i<=n;++i) {
                scanf("%lf",&x);
                add(S,i,log10(x));
                add(i,S,0);
            }
            for(int i=1;i<=m;++i) {
                scanf("%lf",&x);
                add(i+n,T,log10(x));
                add(T,i+n,0);
            }
            while(Q--){
                scanf("%d%d",&r,&c);
                add(r,c+n,INF);
                add(c+n,r,0);
            }
            double ans=0;
            while(bfs()) ans+=dfs(S,INF);
            printf("%.4f
    ",pow(10,ans));
        }
    }
  • 相关阅读:
    Atitit 函数调用的原理与本质attilax总结 stdcall cdecl区别
    Atitit 图像处理 halcon类库的使用  范例边缘检测 attilax总结
    互联网创业原则与创业模式attilax大总结
    Atitit SeedFilling种子填充算法attilax总结
    Atitit 软件体系的进化,是否需要一个处理中心
    Atitit 获取剪贴板内容
    Atitit 架构的原则attilax总结
    Atitit Atitit 零食erp数据管理世界著名零食系列数据.docx世界著名零食
    Atitit 个人信息数据文档知识分类
    Atitti python2.7安装 numpy attilax总结
  • 原文地址:https://www.cnblogs.com/mfys/p/7492938.html
Copyright © 2011-2022 走看看