2012-05-20 38 views
1

UVA鏈接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=464UVA#523最低運輸成本

我失去了我現在正在做的事情。我不知道我爲什麼會產生不正確的值。我查找最短路徑後打印出陣列。它產生這樣的:

[0, 3, 8, 8, 4] 
[3, 0, 5, 11, 7] 
[8, 5, 0, 9, 12] 
[8, 11, 9, 0, 4] 
[4, 7, 12, 4, 0] 

樣品輸入:

1 

0 3 22 -1 4 
3 0 5 -1 -1 
22 5 0 9 20 
-1 -1 9 0 4 
4 -1 20 4 0 
5 17 8 3 1 
1 3 
3 5 
2 4 

與樣品輸入,我產生輸出:

From 1 to 3 : 
Path: 1-->2-->3 

Total cost :8 
From 3 to 5 : 
Path: 3-->2-->5 
Total cost :12 

From 2 to 4 : 
Path: 2-->5-->4 
Total cost :11 

如果我看我的陣列,它似乎是正確的。但是,正確的答案是:

From 1 to 3 : 
Path: 1-->5-->4-->3 
Total cost : 21 

From 3 to 5 : 
Path: 3-->4-->5 
Total cost : 16 

From 2 to 4 : 
Path: 2-->1-->5-->4 
Total cost : 17 

我想我錯過了加入稅,但我不知道在哪裏添加。我嘗試加tax[j]path[i][j]做最短路徑,然後減去tax[x-1]tax[y-1]x-1是我的來源,y-1是我的目的地)。它不起作用,弄亂了我的路徑數組(它賦予循環的價值;我不需要這樣做)。我真的不知道在哪裏繳稅。

這裏是我的代碼以供參考:

import java.util.*; 
public class Main{ 
public static final int INF = 9999999; 
public static int[][] path; 
public static int[][] next; 
public static int[] tax; 
public static void main(String[] adsf){ 
    Scanner pp = new Scanner(System.in); 
    int testCases = pp.nextInt(); 
    boolean first = true; 
    pp.nextLine(); 
    pp.nextLine(); 
    while(testCases-- >0){ 
     String[] s1 = pp.nextLine().split(" "); 
     int size = s1.length; 
     path = new int[size][size]; 
     next = new int[size][size]; 
     for(int i = 0; i < path.length; i++) 
      Arrays.fill(path[i],INF); 
     tax = new int[size]; 
     for(int j = 0; j < path[0].length; j++){ 
      path[0][j] = Integer.parseInt(s1[j]); 
      if(path[0][j]==-1) 
       path[0][j]=INF; 
     } 
     for(int i = 1; i < path.length;i++){ 
      s1 = pp.nextLine().split(" "); 
      for(int j = 0; j < path[0].length; j++){ 
       path[i][j] = Integer.parseInt(s1[j]); 
       if(path[i][j]==-1) 
        path[i][j]=INF; 
      } 
     } 
     for(int k=0; k<tax.length;k++){ 
      tax[k] = pp.nextInt(); 
     } pp.nextLine();  
     apsp(); 
     int x,y; 
     //for(int i = 0; i < size; i++) 
     //System.out.println(Arrays.toString(path[i])); 
     while(pp.hasNextInt()){ 
      if(!first) 
      System.out.println(); 
      x=pp.nextInt(); 
      y=pp.nextInt(); 
      int cost = path[x-1][y-1]; 
      System.out.println("From "+x+" to "+y+" :"); 
      System.out.print("Path: "); 
      ArrayList<Integer> print = getpath(x-1,y-1); 
      for(int l = 0; l < print.size(); l++){ 
       System.out.print(print.get(l)+1); 
       if(l!=print.size()-1) System.out.print("-->"); 
       else System.out.println(); 
      } 
      System.out.println("Total cost :"+cost); 
      first = false; 
     } 
    } 
} 
public static void apsp(){ 
    for(int k=0;k<path.length;k++){ 
     for(int i=0;i<path.length;i++){ 
      for(int j=0;j<path.length;j++){ 
       if (path[i][k] + path[k][j] + tax[j] < path[i][j] + tax[j]) { 
        path[i][j] = path[i][k]+path[k][j]; 
        next[i][j] = k; 
       } else{ 
        path[i][j] = path[i][j]; 
       } 
      } 
     } 
    } 
} 
public static ArrayList<Integer> getpath (int i, int j) { 
    ArrayList<Integer> pat = getMidPath(i,j); 
    pat.add(0,i); 
    pat.add(j); 
    return pat; 
} 
public static ArrayList<Integer> getMidPath(int i, int j){ 
    if(next[i][j]==0) 
     return new ArrayList<Integer>(); 
    ArrayList<Integer> pat = new ArrayList<Integer>(); 
    pat.addAll(getMidPath(i,next[i][j])); 
    pat.add(next[i][j]); 
    pat.addAll(getMidPath(next[i][j],j)); 
    return pat; 
} 
} 

回答

2

稅收應用於:

只要通過一個城市的過客,除了源和目的地城市的任何貨物。

所以,你如果你有一個路徑:

一個 - 「乙 - 」ç - > d

那麼你應該包括路徑的成本(A,B)+稅(二)+(b,c)+稅(c)+(c,d)。

爲了實現這到你的算法,你應該能夠對城市的稅收增加了路徑長度是這樣的:

如果你知道你開始在和d結束,那麼任何直接路徑一個城市x,其中x不是a或b,應該被視爲x + x城市稅的成本。

您需要恢復的路徑開銷值恢復到原有的你要計算的價值,併爲新的出發點和歸宿重新添加稅值的下一個路徑。