Floyd algorithm solves the shortest path problem

Source: Internet
Author: User

Time limit: 10000ms

Single point time limit: 1000ms

Memory Limit: 256MB

Describe

At noon of Halloween, A and b came to a new haunted house after lunch. There are a total of n locations in the Haunted house, numbered 1, respectively. N, there are some road connections between these n locations, there may be multiple roads connected between the two locations, but there is no road where both ends are the same location. Because there is no oppression of the stomach, A and B decided to stroll around the haunted house, shopping stroll, a produced such a problem: haunted house in any of the two locations between the shortest path is how much?

Input

Each test point (input file) has and has only one set of test data.

In a set of test data:

The 1th Act is 2 integers n, M, respectively, indicating the number of places in the haunted house and the number of roads.

The next M-line, each line describes a road: where the I behavior of three integers u_i, v_i, Length_i, indicates that there is a road length v_i between the place numbered U_i and the place numbered length_i.

For 100% of data, meet N<=10^2,m<=10^3, 1 <= length_i <= 10^3.

For 100% of the data, meet any of the two locations in the maze can reach each other.

Output

For each set of test data, output a n*n matrix A, in which row I, Column J, indicates the length of the shortest path from the first place to the J location, when the i=j should be 0.

Sample input

5 12

1 2 967

2 3 900

3 4 771

4 5 196

2 4 788

3 1 637

1 4 883

2 4 82

5 2 647

1 4 198

2 4 181

5 2 665

Sample output

0 280 637) 198 394

280 0 853) 82 278

637 853 0) 771 967

198 82 771) 0 196

394 278 967) 196 0

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intN, M, map[101][101];7 8 voidFlody () {9      for(intK =1; K <= N; ++k) {Ten          for(inti =1; I <= N; ++i) { One              for(intj =1; J <= N; ++j) { AMap[i][j] = min (Map[i][j], map[i][k] +map[k][j]); -             } -         } the     } - } -  - intScan () { +     CharC; -      while(c = GetChar (), C <'0'||'9'<c) +         ; A     intRET = C-'0'; at      while(c = GetChar (),'0'<= C && C <='9') -RET = RET *Ten+ C-'0'; -     returnret; - } -  - voidPrintintx) { in     if(X >9) -Print (X/Ten); toPutchar (x%Ten+'0'); + } - intMain () { the     intu_i, V_i, length_i; *memset (Map,Ten,sizeof(map)); $n =Scan ();Panax Notoginsengm =Scan (); -      while(m--){ theU_i =Scan (); +V_i =Scan (); ALength_i =Scan (); the         if(Map[u_i][v_i] >length_i) +Map[u_i][v_i] = map[v_i][u_i] =length_i; -     } $ flody (); $      for(inti =1; I <= N; ++i) { -          for(intj =1; J <= N; ++j) { -             if(i = = j) {Putchar ('0'); Putchar (' ');} the             Else{print (map[i][j]); Putchar (' '); }  -         }WuyiPuts""); the     } -     return 0; Wu}

Floyd algorithm solves the shortest path problem

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.