首页 > 专题系列 > Java解POJ > POJ 3308 Paratroopers [解题报告] Java
2013
11-12

POJ 3308 Paratroopers [解题报告] Java

Paratroopers

问题描述 :

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 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.

输出:

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.

样例输入:

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

样例输出:

16.0000

解题代码:

import java.util.ArrayList;  
    import java.util.LinkedList;  
    import java.util.List;  
    import java.util.Queue;  
    import java.util.Scanner;  
      
    public class Main{  
      
        static List< Integer> map[];  
        static double cap[][];  
        static double flow[][];  
        static int s,t;  
        static int R,C;  
        public static final double INF = 1e8;  
        static double minCost;  
        static int dist[];  
          
        public static void main(String[] args) {  
              
            Scanner scan = new Scanner(System.in);  
              
            int N = scan.nextInt();  
              
            for(int c=0;c< N;c++){  
                R = scan.nextInt();  
                C = scan.nextInt();  
                int L = scan.nextInt();  
                  
                minCost = 0;  
                cap = new double[R+C+2][R+C+2];  
                flow = new double[R+C+2][R+C+2];  
                map = new ArrayList[R+C+2];  
                for(int i=0;i< R+C+2;i++)  
                    map[i] = new ArrayList< Integer>();  
                  
                s = 0;  
                t = R+C+1;  
                for(int i=1;i<=R;i++){  
                    cap[0][i] = Math.log10(scan.nextDouble());  
                    map[0].add(i);  
                    map[i].add(0);  
                }  
                for(int i=1;i<=C;i++){  
                    cap[i+R][t] = Math.log10(scan.nextDouble());  
                    map[i+R].add(t);  
                    map[t].add(i+R);  
                }  
                  
                for(int i=0;i< L;i++){  
                    int a = scan.nextInt();  
                    int b = scan.nextInt();  
                    cap[a][b+R] = INF;  
                    map[a].add(b+R);  
                    map[b+R].add(a);  
                }  
                  
                while(BFS())  
                    minCost += DFS(s,INF);  
                  
                System.out.printf("%.4f",Math.pow(10, minCost));  
                System.out.println();  
                  
            }  
      
        }  
      
        public static double DFS(int s, double flow) {  
              
            if(s == t)  
                return flow;  
            double subFlow = 0;  
              
            for(int i=0;i< map[s].size();i++){  
                int j = map[s].get(i);  
                if(dist[s]+1==dist[j]&&eps(cap[s][j])>0){  
                    double adjustFlow = DFS(j,Math.min(flow-subFlow, cap[s][j]));  
                    subFlow += adjustFlow;  
                      
                    cap[s][j] -= adjustFlow;  
                    cap[j][s] += adjustFlow;  
                }  
            }  
              
            return subFlow;  
        }  
      
        public static boolean BFS() {  
            Queue< Integer> q = new LinkedList< Integer>();  
            dist = new int[R+C+2];  
            boolean visit[] = new boolean[R+C+2];  
            q.add(s);  
            visit[s] = true;  
            dist[s] = 1;  
            while(!q.isEmpty()){  
                int v = q.poll();  
                for(int i=0;i< map[v].size();i++){  
                    int j = map[v].get(i);  
                    if(!visit[j]&&eps(cap[v][j])>0){  
                        visit[j] = true;  
                        dist[j] = dist[v] + 1;  
                        q.add(j);  
                    }  
                }  
            }  
              
            return visit[t];  
        }  
      
        public static double eps(double ds) {  
              
            return ds< 1e-8?0:ds;  
        }  
    }

  1. 微博-默认微博-心情微博-浪小花微博-拜年微博-柏夫微博-小幺鸡微博-天气微博-休闲微博-搞怪微博-大熊微博-BOBO和TOTO微博-管不着微博-阿狸微博-懒猫猫微博-大耳兔微博-哈皮兔微博-星座微博-爱心微博-亚运会微博-张小盒微博-悠嘻猴微博-小新小

  2. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  3. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。

  4. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  5. 学算法中的数据结构学到一定程度会乐此不疲的,比如其中的2-3树,类似的红黑树,我甚至可以自己写个逻辑文件系统结构来。