Problem Description
Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little energy as possible to jump over the hurdles.
Obviously, it is not very difficult for a cow to jump over several very short hurdles, but one tall hurdle can be very stressful. Thus, the cows are only concerned about the height of the tallest hurdle they have to jump over.
The cows’ practice room has N (1 ≤ N ≤ 300) stations, conveniently labeled 1..N. A set of M (1 ≤ M ≤ 25,000) one-way paths connects pairs of stations; the paths are also conveniently labeled(标注) 1..M. Path i travels from station Si to station Ei and contains exactly one hurdle(障碍) of height Hi (1 ≤ Hi ≤ 1,000,000). Cows must jump hurdles in any path they traverse(穿过).
The cows have T (1 ≤ T ≤ 40,000) tasks to complete. Task i comprises(包含) two distinct(明显的) numbers, Ai and Bi (1 ≤ Ai ≤ N; 1 ≤ Bi ≤ N), which connote(意味着) that a cow has to travel from station Ai to station Bi (by traversing(穿越) over one or more paths over some route(按某路线发送)). The cows want to take a path the minimizes(使减到最少) the height of the tallest hurdle(障碍) they jump over when traveling from Ai to Bi . Your job is to write a program that determines the path whose tallest hurdle is smallest and report that height.
Input
- Line 1: Three space-separated integers(整数): N, M, and T
- Lines 2..M+1: Line i+1 contains three space-separated integers(整数): Si , Ei , and Hi
- Lines M+2..M+T+1: Line i+M+1 contains two space-separated integers(整数) that describe task i: Ai and Bi
Output
- Lines 1..T: Line i contains the result for task i and tells the smallest possible maximum height necessary to travel between the stations. Output(输出) -1 if it is impossible to travel between the two stations.
题意:
奶牛接任务从一个点A去点B,可能有多条路径(A直接到B,或者A到C在到B等等),点与点直接隔一个栏杆需要牛跨过去,帮牛找一条从点A到点B的路径,这条路径牛所要跨的栏的最高高度是所有路径中最低的。
思路:
栏高度即为各边的权,用Dijkstra算法,数组dist存放起始点到各点路径中栏高的最大值,先遍历中间点C,看有没有起始点A到C的权比A到B的小,有则dist中存A到C和C到B的最大值,没有则存A直接到B的值。循环迭代。
import java.util.Scanner;
public class Main {
class graph{
int [][] adj;
Path dist[];
int max[];
void setV0(int v0){
dist[v0].length = 0;
dist[v0].prevex = 0;
adj[v0][v0] = 1;
}
public graph(int n) {
// TODO 自动生成的构造函数存根
adj = new int[n][n];
max = new int[n];
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
if(i!=j){
adj[i][j]=Integer.MAX_VALUE;
}else{
adj[i][j]=0;
}
}
}
}
}
class Path{
int length;
int prevex;
public Path() {
// TODO 自动生成的构造函数存根
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Main demo = new Main();
int N,M,T;
int S,E,H;
int i,j,x,y;
int A,B;
int mv;
N = scanner.nextInt();
M = scanner.nextInt();
T = scanner.nextInt();
/**
* 生成题目给出的邻接矩阵
*/
graph gdemo = demo.new graph(N);
// int [][]graph = new int[N][N];
for(i=0;i<N;i++){
for(j=0;j<N;j++){
if(i!=j){
gdemo.adj[i][j] = Integer.MAX_VALUE;
}else{
gdemo.adj[i][j] = 0;
}
}
}
gdemo.dist = new Path[N];
for(j=0;j<N;j++){
gdemo.dist[j] = demo.new Path();
}
for(i=0;i<M;i++){
S = scanner.nextInt();
E = scanner.nextInt();
H = scanner.nextInt();
gdemo.adj[S-1][E-1] = H;
}
/**
* 计算ab之间路径最低高度
*/
for(i=0;i<T;i++){
A = scanner.nextInt();
B = scanner.nextInt();
/**
* 初始化矩阵
*/
// graph gdemo = demo.new graph(N);
// for(x=0;x<N;x++){
// for(y=0;y<N;y++){
// gdemo.adj[x][y] = graph[x][y];
// }
// }
for(x=0;x<N;x++){
gdemo.adj[x][x] = 0;
}
/**
* 设置出发点a
*/
gdemo.setV0(A-1);
/**
* 存放初始状态A点一步到其余各点距离的数组dist
*/
for(j=0;j<N;j++){ //存放第A点一步到其余各点距离的数组dist
gdemo.dist[j].length = gdemo.adj[A-1][j];
if(gdemo.dist[j].length!=Integer.MAX_VALUE){
gdemo.dist[j].prevex=A-1;
}
}
for(j=0;j<N;j++){
mv = -1;
for(x=0;x<N;x++){
if(gdemo.adj[x][x]==0&&gdemo.dist[x].length<gdemo.dist[B-1].length){
mv = x;
}
}
if(mv == -1){
break;
}
gdemo.adj[mv][mv] = 1;
for(y=0;y<N;y++){
if(gdemo.adj[y][y] == 0&& gdemo.dist[mv].length+gdemo.adj[mv][y]<Integer.MAX_VALUE){
if(gdemo.dist[mv].length+gdemo.adj[mv][y]>0){
if(gdemo.dist[y].length>gdemo.adj[mv][y]){
gdemo.dist[y].length = Math.max(gdemo.dist[mv].length, gdemo.adj[mv][y]);
gdemo.dist[y].prevex = mv;
}
// gdemo.dist[y].length = gdemo.dist[mv].length + gdemo.adj[mv][y];
}
}
}
}
if(gdemo.dist[B-1].length!=Integer.MAX_VALUE){
System.out.println(gdemo.dist[B-1].length);
}else {
System.out.println(-1);
}
}
}
}