首页 > 专题系列 > Java解POJ > POJ 3767 I Wanna Go Home [解题报告] Java
2013
11-13

POJ 3767 I Wanna Go Home [解题报告] Java

I Wanna Go Home

问题描述 :

The country is facing a terrible civil war—-cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.

“For the sake of safety,”, said Mr.M, “your route should contain at most 1 road which connects two cities of different camp.”

Would you please tell Mr. M at least how long will it take to reach his sweet home?

输入:

The input contains multiple test cases.

The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.

The second line contains one integer M (0<=M<=10000), which is the number of roads.

The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].

Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.

To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.

Note that all roads are bidirectional and there is at most 1 road between two cities.

Input is ended with a case of N=0.

输出:

For each test case, output one integer representing the minimum time to reach home.

If it is impossible to reach home according to Mr. M’s demands, output -1 instead.

样例输入:

2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0

样例输出:

100
90
540

解题代码:

import java.util.Scanner; 

public class Main{ 

 public static void dijkstra(int[][] GA, int[] leader, int road) { 

  if (road == 0)
   System.out.println(-1);
  else { 

   int size = GA.length;
   // System.out.println(size);
   int[] dist = new int[size];
   int[] isUsed = new int[size];
   isUsed[1] = 1;
   dist[1] = Integer.MAX_VALUE;
   for (int i = 2; i < size; i++) {
    dist[i] = GA[1][i];
   } 

   for (int i = 1; i < size - 2; i++) {
    int min = Integer.MAX_VALUE;
    int m = 0;
    for (int j = 1; j < size; ++j) {
     if (isUsed[j] == 0 && dist[j] < min) {
      min = dist[j];
      m = j;
     }
    } 

    if (m != 0)
     isUsed[m] = 1;
    else
     break; 

    for (int j = 1; j < size; ++j) {
     if (isUsed[j] == 0
       && (leader[m] == leader[j] || leader[j] == 2)
       && GA[m][j] != Integer.MAX_VALUE
       && dist[m] + GA[m][j] < dist[j]) {
      dist[j] = dist[m] + GA[m][j];
     }
    }
   }
   System.out.println(dist[2] == Integer.MAX_VALUE ? -1 : dist[2]);
  }
 } 

 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  while (sc.hasNextInt()) {
   int countCity = sc.nextInt(); 

   if (countCity == 0)
    break; 

   int road = sc.nextInt(); 

   int[][] GA = new int[countCity + 1][countCity + 1]; 

   for (int i = 1; i <= countCity; ++i) {
    for (int j = 1; j <= countCity; ++j) {
     GA[i][j] = Integer.MAX_VALUE;
    }
   } 

   for (int i = 0; i < road; ++i) {
    int c1 = sc.nextInt();
    int c2 = sc.nextInt();
    GA[c1][c2] = sc.nextInt();
    GA[c2][c1] = GA[c1][c2];
   }
   int[] leader = new int[countCity + 1];
   for (int i = 1; i < countCity + 1; ++i)
    leader[i] = sc.nextInt();
   dijkstra(GA, leader, road); 

  }
 } 

}