FIG special - no minimum hoop FIG.

The smallest ring

Bare question:
https://vjudge.net/problem/HDU-1599

First, this must be a simple ring.

Consider the highest numbered ring node u.

f [u-1] [x] [y] and (u, x), (u, y) together form a ring.

Enumeration u (where i, j is less than u) In the process of Floyd, and the minimum can be calculated.

import java.util.Scanner;

public class Main{

    private int[][] path = new int[110][110];
    private int[][] G =new int[110][110];
    private int size;

    public void get_data(Scanner in){
        int maxx = 1000000;
        for(int i=0;i<110;i++){
            for(int j=0;j<110;j++){
                if(i==j){
                    path[i][j] =G[i][j] =0;
                    continue;
                }
                path[i][j] =maxx;
                G[i][j] =maxx;
            }
        }


        size = in.nextInt();
        int m = in.nextInt();

        for(int i=0;i<m;i++){
            int start = in.nextInt();
            int end = in.nextInt();
            int cost = in.nextInt();
            if(path[start][end]>cost){
                path[start][end] = path[end][start] = cost;
                G[start][end] = G[end][start] = cost;
            }
        }

    }


    public void flody(){
        int minny = 1000000;
        for(int k=1;k<=size;k++) {
			//从i到j和从j到i的距离是相同的,而且要剔除i到i的点,因为环中点的个数要大于2
            for(int i=1;i<k;i++){
                for(int j=i+1;j<k;j++){
                    minny = Math.min(path[i][j]+G[k][i]+G[k][j],minny);
                }
            }
			//flody
            for (int i = 1; i <= size; i++) {
                for (int j = 1; j <= size; j++) {
                    path[j][i] = path[i][j] = Math.min(path[i][j],path[i][k]+path[k][j]);
                }
            }
        }


        if(minny>=1000000){
            System.out.println("It's impossible.");
        }else{
            System.out.println(minny);
        }

    }


    public static void main(String arg[]){
        Main obj = new Main();
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            obj.get_data(in);
            obj.flody();
        }

    }

}

Guess you like

Origin blog.csdn.net/baidu_41560343/article/details/90711122