Codevs original plagiarism question 5960 messenger, codevs5960

Source: Internet
Author: User

Codevs original plagiarism question 5960 messenger, codevs5960
DescriptionDescription

• During the war period, there were n front lines, and each post may communicate with several other posts. The messenger is responsible for transmitting information between posts. Of course, this takes some time (in days ). The headquarters is located at the first post. When the command is issued by the command department, the command department sends several couriers to the post connected to the command center. When a post received a letter, the couriers in The Post also sent messages to other posts in the same way. The mail is successful only when all n posts receive commands. A sufficient number of couriers are arranged in each post (if one post communicates with the other k posts, at least k couriers will be provided in the post ). • For the General Command, compile a program to calculate the minimum time required to complete the entire mail delivery process.

Input description Input Description

• There are two integers n and m in row 1st, separated by one space in the middle, indicating that there are n posts and m communication lines respectively. 1 <= n <= 100. • Lines 2nd to m + 1: Each line contains three integers I, j, and k, separated by a space in the middle, indicating that there is a communication line between the I and j post, this line takes k days.

Output description Output Description

The output file msner. out is only an integer, indicating the shortest time to complete the entire mail sending process. If not all posts receive a letter, output-1.

Sample Input Sample Input

• 4 4 •

1 2 4 •

2 3 7 •

2 4 1 •

3 4 6

Sample output Sample Output

11

Data range and prompt Data Size & Hint

1 <= n <= 100

CATEGORY tag Tags click here to expand

 

Idea: Use Floyed to find the shortest path

Then enumerate the nodes from 1 to n and take the maximum value

If there are still issues

Then output-1

Principle: If this graph meets the conditions, you can traverse the entire graph from 1. The farthest distance between the vertices that can be reached by 1 must be the last vertex, this is the shortest path (because each node passes through at least once)

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 int map [101] [101]; 6 int maxn = 0xf; 7 int main () 8 {9 memset (map, maxn, sizeof (map); 10 int n, m; 11 cin> n> m; 12 for (int I = 1; I <= m; I ++) 13 {14 int x, y, z; 15 scanf ("% d ", & x, & y, & z); 16 map [x] [y] = map [y] [x] = z; 17} 18 map [1] [1] = 0; 19 for (int k = 1; k <= n; k ++) 20 {21 for (int I = 1; I <= n; I ++) 22 {23 for (int j = 1; j <= n; j ++) 24 {25 if (map [I] [j]> map [I] [k] + map [k] [j]) 26 {27 map [I] [j] = map [I] [k] + map [k] [j]; 28} 29} 30} 31} 32 33 int ans =-1; 34 for (int I = 2; I <= n; I ++) 35 {36 if (map [1] [I]> ans) 37 {38 ans = map [1] [I]; 39} 40 else41 {42 if (map [1] [I] = maxn) 43 {44 cout <-1; 45 return 0; 46} 47} 48} 49 cout <ans; 50 return 0; 51}View Code

 

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.