您的位置:首页 > 编程语言 > Go语言

POJ 3767 I Wanna Go Home

2012-08-21 21:14 381 查看
[align=center]I Wanna Go Home[/align]

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2517 Accepted: 1048
Description

    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?

Input

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.

Output

    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.

Sample Input
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

Sample Output
100
90
540

package poj;

import java.util.Arrays;
import java.util.Scanner;
/**
* @description					POJ 3767 I Wanna Go Home
* @technique					Dijkstra
* @date						20120821
* @time						21:16
* @version						1.0
* @author 						Alex
*
*/
public class Poj3767_20120821_0 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, m, x, y, cost,i;
int [] master;
int [][] map;
while(0!=(n = in.nextInt())){
m = in.nextInt();
master = new int
;
map = new int

;
for(i = 0; i < m; ++i){
x = in.nextInt();
y = in.nextInt();
cost = in.nextInt();
map[x-1][y-1] = cost;
map[y-1][x-1] = cost;
}
for(i = 0; i < n; ++i){
master[i] = in.nextInt();
}
solve(map,master);
}
}
/**
* 利用 Dijkstra 算法计算到达区域边界的最短路径。
* @param map  地图。
* @param master 分属区域。
*/
private static void solve(int [][] map, int [] master){
int i,j,k,min=0,minPath;
int [] dis = new int[master.length];
Arrays.fill(dis, Integer.MAX_VALUE);
boolean [] tag = new boolean[dis.length];
dis[0] = 0; dis[1] = 0;
for(k = 1,min = 0; k <= 2; ++k,++min){  //分别计算 源点到各自所属区域各个城市最短路径。
for(i = 0; i < map.length; ++i){
minPath = Integer.MAX_VALUE;
for(j = 0; j < dis.length; ++j){
if(!tag[j] && master[j] == k && minPath > dis[j]){
minPath = dis[j];
min = j;
}
}
tag[min] = true;
for(j = 0; j < dis.length; ++j){
if( master[j] == k && map[min][j] != 0 && dis[j] > dis[min] + map[min][j]){
dis[j] = dis[min] + map[min][j];
}
}
}
}
calc(map,master,dis); //计算结果交结 calc 处理。
}
private static void calc(int [][] map, int [] master, int [] dis){
int i, j;
long minPath = Integer.MAX_VALUE;  //默认该商人到家最短路径为 整数最大值。
for(i = 0; i < map.length; ++i){
for(j = i; j < map[i].length; ++j){
if(map[i][j] != 0 && master[i] + master[j] == 3     //寻找边界交点。如果经过该边界的路径较小
&& minPath > dis[i] + (long)dis[j] + map[i][j]){ //则更新 minPath 的值。
minPath = dis[i] + dis[j] + map[i][j];
}
}
}

if(minPath == Integer.MAX_VALUE){  //如果不可达输出 -1
System.out.println(-1);
}else{
System.out.println(minPath);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息