隧道TunnelsMeiju

来源:互联网 发布:发条js调试工具 2.0 编辑:程序博客网 时间:2024/04/29 06:11

package work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TunnelsMeiju {
 static int N, V, H;
 static int[][] S;
 static int C1, R1, M1, C2, R2, M2;
 //static int min_rowcost;// 一排最少花费
 static int[] mincost;// 每排花费数组
 static int totalcost;//
 public static void main(String args[]) throws FileNotFoundException {
  /* Scanner sc=new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/tunnels.txt"));
  int T = sc.nextInt();
  for (int t = 0; t < T; t++) {
   N = sc.nextInt();
   H = sc.nextInt();
   V = sc.nextInt();
   S = new int[V][H];
   mincost = new int[V];
   for (int i = 0; i < V; i++) {
    for (int j = 0; j < H; j++) {
     S[i][j] = sc.nextInt();
    }
   }
   C1 = sc.nextInt();
   R1 = sc.nextInt();
   M1 = sc.nextInt();
   C2 = sc.nextInt();
   R2 = sc.nextInt();
   M2 = sc.nextInt();
   for (int v = 0; v < V; v++) {
    mincost[v] = find(v);
   }
   totalcost = 0xfffffff;
            for (int i = 0; i <= V-1-(N-1)*2; i++) {
    dfs(1,i,mincost[i]);
   }
            System.out.println("Case#"+" "+(t+1));
   System.out.println(totalcost);
  }
 }

 private static void dfs(int step, int last, int cost) {
  if(step==N){
   if(totalcost>cost)
    totalcost=cost;
   return;
  }
  for (int next = last+2; next <V-1-(N-1-step)*2 ; next++) {
   cost =cost +mincost[next]+(M1*M1+M2*M2)*(next-last);
   dfs(step+1,next,cost );
  }
 }

 private static int find(int v) {
  int min_rowcost = 0xfffffff;
  for (int i = 0; i <= H; i++) {
   int sum = 0;
   for (int j = 0; j < i; j++){
    sum += S[v][j] * C1;}
   for (int j = i; j < H; j++)
    {sum += S[v][j] * C2;}
   if (H - i > i)
    sum += (H - 2 * i - 1) * R2;
   if (H - i < i)
    sum += (i - (H - i) - 1) * R1;
   if (sum < min_rowcost)
    min_rowcost = sum;

  }
  return min_rowcost;
 }

}

、、input

3
1 3 1
42 3 99
4 19 3
4 1 5
1 5 1
1 4 1 3 11
7 2 3
2 6 4
3 5 6
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
50 50 50 50 50
10 10 10
20 10 5

、、output

Case# 1
576
Case# 2
57
Case# 3
8120

0 0
原创粉丝点击