Problem C: the shortest path

Description

N cities, numbered from 0 to N-1, M road, road length article K (K starting from 0) is 2 ^ K, numbered seeking other cities to the city 0 shortest distance.

Input

The first line of two positive integers N (2 <= N <= 100) M (M <= 500), expressed N cities, roads M,
the next two rows integers M, represents the two cities connected Numbering.

Output

Row N-1, represents a number of 0 to city shortest other cities, if not reach, the output of -1, the result value is too large to output MOD 100,000.

Sample Input

4 3
0 1
1 2
2 0

Sample Output

1
3
-1

Reference Links: 1956 C Shortest Path Problem

The final AC code is as follows:

#include <bits/stdc++.h>
using namespace std;
const int MAX=105, INF=-1;
int father[MAX], d[MAX][MAX];
int findFather(int x){ //找父节点 
    if(x == father[x]) return x;
    else return findFather(father[x]);
}
int quickMi(int a, int k, int mod){
    int ans = 1;
    long long int res = a;
    the while (K) {
         IF (K & . 1 ) ANS = (ANS * RES)% MOD; 
        RES = (RES * RES)% MOD; // here not written = RES (RES * A) MOD%; 
        K = >> . 1 ; 
    } 
    return ANS; 
} 
int main () {
     int I, J, K, U, V, X, Y, n-, m, DIS;
     the while (Scanf ( " % D% D " !, & n-, & m) = the EOF) { 
        Fill (D [ 0 ], D [ 0 ] + * MAX MAX, INF); // initialize array distance 
        for (I = 0 ; I <n-; I ++ ) {
            Father [I] = I; // initialize disjoint-set 
            D [I] [I] = 0 ; // initialize array diagonal distance must be wrong answer here forgot 
        }
         for (K = 0 ; K <m; K ++ ) { 
            Scanf ( " % D% D " , & U, & V); 
            X = findFather (U); 
            Y = findFather (V);
             IF (! X = Y) { // instructions u, v is not connected 
                DIS = quickMi ( 2 , K, 100000 ); // Since the index using flash much power 
                for (I = 0; I <n-; I ++ ) {
                     IF (x == findFather (I)) { // find all the points on the x set as the root node 
                        for (J = 0 ; J <n-; J ++ ) {
                             IF (Y == findFather (J)) { // find all points in the set as a root node y 
                                d [i] [j] = d [j] [i] = (d [i] [u] + dis + d [v] [J])% 100000 ; // update 
                            } 
                        } 
                    } 
                } 
                Father [Y] = X; // merge the two collections 
            } 
        } 
        for (J = . 1; j<n; j++) printf("%d\n", d[0][j]);
    }
    return 0;
}

Summary: The key problem is the index of this trouble can be large, so it can not be solved by way of the half for the most short-circuited. Then, by the power of fast modulo seeking ways to solve exponential power of a great situation. Then the modulo path after storage, and the like and then try Dijkstra shortest path, the result is still WA. Later, when reference other people's blog, found that people with fast power + and check very easy way to solve the set, the main idea is: if two points are already in a collection, because the input side of the back is increasing, so no longer updated; if two points on two different collections, then that edge is not connected to the two sets, the update array of paths, and then combined set.

Guess you like

Origin www.cnblogs.com/heyour/p/12636148.html