Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1
-----------------------------------------------
附加测试数据:
2 1
0 1 2
1 1
5 5
0 1 3
2 1 1
0 2 1
1 4 20
2 4 60
0 4
4 4
1 2 1
2 3 1
3 2 1
2 1 1
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1
-----------------------------------------------
附加测试数据:
2 1
0 1 2
1 1
5 5
0 1 3
2 1 1
0 2 1
1 4 20
2 4 60
0 4
4 4
1 2 1
2 3 1
3 2 1
2 1 1
1 3
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author YouYuan
* @eMail E-mail:1265161633@qq.com
* @Time 创建时间:2015年8月12日 下午4:27:20
* @Explain 说明:起点和终点可能相同,要输出0,城镇之间的道路可能不止一条,要取最小的。被坑了一直WA。
*/
public class Hdu1874最短路径_畅通工程续 {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException { in.nextToken(); return (int) in.nval;}
static int MAXN = 201;
static int maxNum = 0xfffffff;
static int[][] arc = new int[MAXN][MAXN];//邻接矩阵储存图
static int[] sum = new int[MAXN];//走到当前位置的最小代价
static int n, m, Start, End, MIN;
static Queue<Integer> queue = new LinkedList<Integer>();
public static void main(String[] args) throws IOException {
while(in.nextToken() != in.TT_EOF) {
n = (int) in.nval;
m = nextInt();
initialise();
structure();
Start = nextInt();
End = nextInt();
if (Start == End) {
System.out.println(0);
continue;
}
bfs();
}
}
/**
* 使用广搜搜索最短路径
*/
private static void bfs() {
queue.add(Start);
while(!queue.isEmpty()) {
int a = queue.poll();
if (a == End) {
if (sum[a] < MIN) {//判断是否是较短的路径
MIN = sum[a];//记录当前最短路径,直到所有路径搜索完毕
}
}
for (int i = 0; i < n; i++) {
if (arc[a][i] != maxNum) {
int s = (sum[a] == maxNum ? 0 : sum[a]) + arc[a][i];//计算到当前顶点的路径长度
if (s < sum[i]) {//是否小于原路径长度
sum[i] = s;//更新路径长度
} else {
continue;
}
queue.add(i);
}
}
}
System.out.println(MIN == maxNum ? -1 : MIN);
}
/**
* 构图
* @throws IOException
*/
private static void structure() throws IOException {
for (int i = 0; i < m; i++) {
int a = nextInt();
int b = nextInt();
int x = nextInt();
if (arc[a][b] > x || arc[b][a] > x) {//可能同样路径有不同的距离,取距离短的
arc[a][b] = x;//双向路,两个方向都要保存
arc[b][a] = x;
}
}
}
/**
* 初始化
*/
private static void initialise() {
queue.clear();
for (int i = 0; i < n; i++) {
sum[i] = maxNum;
for (int j = 0; j < n; j++) {
arc[i][j] = maxNum;
}
}
MIN = maxNum;
}
}