首页 > 专题系列 > Java解POJ > POJ 1734 Sightseeing trip [解题报告] Java
2013
11-10

POJ 1734 Sightseeing trip [解题报告] Java

Sightseeing trip

问题描述 :

There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is necessary to find the shortest route which begins and ends at the same place. Your task is to write a program which finds such a route.

In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, …, y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,...,x_k should be different.The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y_1)+L(y_2)+...+L(y_k) where L(y_i) is the length of the road y_i (1<=i<=k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible,because there is no sightseeing route in the town.

输入:

The first line of input contains two positive integers: the number of crossing points N<=100 and the number of roads M<=10000. Each of the next M lines describes one road. It contains 3 positive integers: the number of its first crossing point, the number of the second one, and the length of the road (a positive integer less than 500).

输出:

There is only one line in output. It contains either a string ‘No solution.’ in case there isn’t any sightseeing route, or it contains the numbers of all crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.

样例输入:

5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20

样例输出:

1 3 5 2

解题代码:

import java.util.Scanner;
public class Main{
 static final int INF = 0x3f3f3f3f ;  
 private int n;  
 private int[][] maze;  //邻接矩阵
 private int[][] dis;  //dis[][]保存可以达到的最短距离,会变化; 
 private int[][] fa;  
 private int[] res;
 private int temp;

  public Main(int n,int[][] fa,int[][] maze){
     this.n=n;
     this.fa=fa;
     this.maze=maze;
     dis=new int[n+1][n+1];
     res=new int[n+1];
     
  }
  

 private void solve(int i, int j ,int k){            //记录最小环的路径    
    temp = 0 ;  
    while(j != i){  
        res[temp++] = j  ;  
        j = fa[i][j] ;  
    }  
    res[temp++] = i ;  
    res[temp++] = k ;  
}  

 private void Floyd(){  
    for(int i=1;i<=n;i++){  
        for(int j=1;j<=n;j++){  
            dis[i][j] = maze[i][j] ;  
        }  
    }  
    int ans = INF ;  
    for(int k=1;k<=n;k++){  
        for(int i=1;i< k;i++){  
            for(int j=i+1;j< k;j++){  
                if(dis[i][j]< INF && maze[i][k]< INF && maze[k][j]< INF && ans>dis[i][j] + maze[i][k] + maze[k][j]){  
                    ans = dis[i][j] + maze[i][k] + maze[k][j] ; //最大节点为k的环的最短路径
                    solve(i,j,k); //记录环的各顶点 
                }  
            }  
        }  
        for(int i=1;i<=n;i++){  
            for(int j=1;j<=n;j++){  
                if(dis[i][k]< INF && dis[k][j]< INF && dis[i][j]>dis[i][k] + dis[k][j]){  
                    dis[i][j] = dis[i][k] + dis[k][j] ;  
                    fa[i][j] = fa[k][j] ;  
                    fa[j][i] = fa[k][i] ;  
                }  
            }  
        }     
    }  
    if(ans == INF){  
        System.out.printf("No solution.\n");  
    }  
    else{  
        for(int i=0;i< temp;i++){  
            System.out.printf("%d%c",res[i],i==temp-1?'\n':' ');  
        }  
    }  
} 
 public static void  main(String args[]){  
    Scanner in=new Scanner(System.in);
    int a ,b ,c ;  
    while(in.hasNext()){  
       int n=in.nextInt();
       int m=in.nextInt();
       int maze[][]=new int[n+1][m+1];
       int fa[][]=new int[n+1][n+1];
        for(int i=1;i<=n;i++){  
            for(int j=1;j<=m;j++){  
                if(i == j)  maze[i][j] = 0;   
                else        maze[i][j] = INF ;  
            }  
        }  
        for(int i=1;i<=m;i++){  
           a=in.nextInt();
           b=in.nextInt();
           c=in.nextInt();
           if(maze[a][b] > c){  
                maze[a][b] = maze[b][a] = c ;  
                fa[a][b] = a ; //标记(i,j)最短路径上,距离j最近的那个结点    
                fa[b][a] = b ;    
            }     
        }  
       Main ma=new Main(n,fa,maze);
        ma.Floyd() ;  
    }     
 }
}

  1. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?