Idea: because the weight of the I edge w [I] = 2 ^ I, then the sum of the weight of the edge of the front I-1 is w [0] + w [1] + · + w [I-1] = (2 ^ I) -1 means that the sum of the weights of the first I-1 edge is smaller than that of the first I edge. Based on this property, when an I-th edge (a, B) is added: that is, the sum of the weights of the first I-1 edge is smaller than that of the I-th edge. Based on this nature, when an I-th edge (a, B) is added: If a and B are not connected, the shortest path between a and B is w [I]; if a and B are connected, the shortest paths between a and B remain unchanged. C language source code: [cpp] # include <stdio. h> # define maxsize 110 # define M 100000 int T [maxsize]; int E [maxsize] [maxsize]; int visited [maxsize]; int findroot (int x) {int temp; if (T [x] =-1) return x; else {temp = findroot (T [x]); T [x] = temp; return temp ;}} void dfs (int x, int n, int sum) {int I; for (I = 0; I <n; I ++) if (visited [I] = 0 & E [x] [I]! = 0) {visited [I] = 1; T [I] = sum + E [x] [I]; dfs (I, n, T [I]);} int main () {int m, n, a, k, B, I, j, num, sum, roota, rootb; while (scanf ("% d ", & n, & m )! = EOF) {for (I = 0; I <n; I ++) {T [I] =-1; visited [I] = 0 ;} for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) E [I] [j] = 0; k = 1; for (I = 0; I <m; I ++) {scanf ("% d", & a, & B ); roota = findroot (a); rootb = findroot (B); if (roota! = Rootb) {T [rootb] = roota; E [a] [B] = k % M; E [B] [a] = k % M ;} k = (k * 2) % M;} I = 0; sum = 0; visited [0] = 1; for (I = 1; I <n; I ++) T [I] =-1; dfs (0, n, sum); for (I = 1; I <n; I ++) printf ("% d \ n ", T [I] % M );}}