9 degree tutorial 78th questions

Source: Internet
Author: User

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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.