zoukankan      html  css  js  c++  java
  • Paratroopers 最大流问题 dinic算法 水平有待提高

    Problem 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 × 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
    ***************************************************************************************************************************
    最小点权覆盖=最大流,最小独立集=总和-最小点权覆盖
    ***************************************************************************************************************************
      1 #include<cmath>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<string>
      5 #include<cstring>
      6 #include<cstdio>
      7 #define MAX 1000
      8 #define eps 0.0001
      9 #define INF 9999999999
     10 #define min(a,b) (a<b?a:b)
     11 using namespace std;
     12 struct Edge
     13 {
     14     int st, ed;
     15     int next;
     16     double flow;
     17 } edge[MAX*10];
     18 
     19 int pre[MAX];  // pre[u]储存以u为终点的边的编号,它储存的是边
     20 int head[MAX], out[MAX];
     21 int leve[MAX]; // 层次图中每个点的层次
     22 int que[MAX], stk[MAX];
     23 int E, R, C, L, src, dest;
     24 
     25 void add_edge ( int u, int v, double val )
     26 {
     27     edge[E].st = u;
     28     edge[E].ed = v;
     29     edge[E].flow = val;
     30     edge[E].next = head[u];
     31     head[u] = E++;
     32 
     33     edge[E].st = v;
     34     edge[E].ed = u;
     35     edge[E].flow = 0;
     36     edge[E].next = head[v];
     37     head[v] = E++;
     38 }
     39 
     40 bool dinic_bfs ()
     41 {
     42     int front, rear, u, v, i;
     43     front = rear = 0;
     44     memset(leve,-1,sizeof(leve));
     45     que[rear++] = src;
     46     leve[src] = 0;
     47     while ( front != rear )
     48     {
     49         u = que[front];
     50         front = ( front + 1 ) % MAX;
     51         for ( i = head[u]; i != -1; i = edge[i].next )
     52         {
     53             v = edge[i].ed;
     54             if ( leve[v] == -1 && edge[i].flow > eps )
     55             {
     56                 leve[v] = leve[u] + 1;
     57                 que[rear] = v;
     58                 rear = ( rear + 1 ) % MAX;
     59             }
     60         }
     61     }
     62     return leve[dest] >= 0; // leve[dest] == -1 说明没有找到增广路径
     63 
     64 }
     65 
     66 double dinic_dfs ()
     67 {
     68     int top = 0, u, v, pos,i;
     69     double minf, res = 0;
     70     memcpy(out,head,sizeof(head)); // 将head的值拷给out
     71     stk[top++] = u = src;
     72     while ( top != 0 ) //栈空说明在当前层次图中,所有的增广路径寻找完毕
     73     {
     74         while ( u != dest )
     75         {
     76             for ( i = out[u]; i != -1; i = edge[i].next )
     77             {
     78                 v = edge[i].ed;
     79                 if ( edge[i].flow > eps && leve[u] + 1 == leve[v] )
     80                 {
     81                     out[u] = edge[i].next;  // 下一次访问的时候,就可以直接得到edge[i].next
     82                     pre[v] = i; // 存储边的编号
     83                     stk[top++] = v; // 栈中放的是点
     84                     u = v;
     85                     break;
     86                 }
     87             }
     88             if ( i == -1 ) // 若从某点出发找不到合法的边
     89             {
     90                 top--;  // 把该点从栈中删除
     91                 if ( top <= 0 ) break;
     92                 u = stk[top-1];  // 返回前一个点
     93             }
     94         }
     95 
     96         if ( u == dest )
     97         {
     98             minf = INF;
     99             while ( u != src )
    100             {
    101                 minf = min ( edge[pre[u]].flow, minf );
    102                 u = edge[pre[u]].st;
    103             }
    104 
    105             u = dest;
    106             while ( u != src )
    107             {
    108                 edge[pre[u]].flow -= minf;
    109                 edge[pre[u]^1].flow += minf;
    110                 if ( edge[pre[u]].flow < eps ) pos = edge[pre[u]].st; // 记录下零流出现的顶点。取最前面的哪一个。
    111                 u = edge[pre[u]].st;
    112             }
    113 
    114             while ( top > 0 && stk[top-1] != pos ) top--; // 退回到零流出现的顶点
    115             if ( top > 0 ) u = stk[top-1];
    116             res += minf;
    117         }
    118     }
    119     return res;
    120 }
    121 
    122 
    123 double Dinic ()
    124 {
    125     double ans = 0, temp;
    126     while ( dinic_bfs() )
    127     {
    128         temp = dinic_dfs();
    129         if ( temp > eps ) ans+=temp;
    130         else break;
    131     }
    132     return ans;
    133 }
    134 
    135 int main()
    136 {
    137     int T, a, b, i;
    138     double val;
    139     scanf("%d",&T);
    140     while ( T-- )
    141     {
    142         scanf("%d%d%d",&R,&C,&L);
    143         memset(head,-1,sizeof(head));
    144         src = E = 0;
    145         dest = R + C + 1;
    146         for ( i = 1; i <= R; i++ )
    147         {
    148             scanf("%lf",&val);
    149             add_edge ( src, i, log(val) );
    150         }
    151 
    152         for ( i = 1; i <= C; i++ )
    153         {
    154             scanf("%lf",&val);
    155             add_edge ( i+R, dest, log(val) );
    156         }
    157 
    158         while ( L-- )
    159         {
    160             scanf("%d%d",&a,&b);
    161             add_edge(a,b+R,INF);
    162         }
    163         val = Dinic();
    164         printf("%.4f
    ",exp(val));
    165     }
    166     return 0;
    167 }
    View Code
  • 相关阅读:
    拦截器getmodel方法什么时候被调用(没搞懂有什么鸟用,自己搭的项目中用到了这个)
    Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
    Spring组件扫描<context:component-scan/>使用详解
    Eclipse从数据库逆向生成Hibernate带注解的实体类
    HibernateTool的安装和使用(Eclipse中)
    将一字符串首字母转大写
    二分(折半)查找算法
    按位与运算符
    注解及自定义注解
    根据前序遍历和中序遍历得出后序遍历
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3361973.html
Copyright © 2011-2022 走看看