[Daily learning] [Dijkstra] codevs1069 usaco go home to answer questions, issue stracodevs1069

Source: Internet
Author: User

[Daily learning] [Dijkstra] codevs1069 usaco go home to answer questions, issue stracodevs1069

Source: usaco codevs1069

Description Description

It's dinner time, and cows are scattered outside the farm. John the farmer rang the bell, so they started to go to the barn. Your job is to identify which cow will arrive in the Barn first (there will always be and only one of the fastest cows in the given test data ). During milking (before dinner), every cow is on her farm, and some pastures may have no cows. Each farm is connected to one or more farms by one road (may include itself ). Sometimes there is more than one road between two pastures (which may be the same letter. There must be at least one farm and a barn with a road connection. Therefore, all cows can reach the barn at last, and the cows always take the shortest path. Of course, cows can move in any direction at the same speed. The farm is marked as 'A'... 'Z' and 'A'... 'y'. There is a cow in the farm indicated by uppercase letters, but not in lowercase letters. The tag of the barn is 'Z'. Note that there are no cows in the barn.

Note that 'M' and 'M' are not the same farm. Otherwise, the error indicates that the input data may contain both m and M (depressing ing). For example

M a m z

Input description Input Description

Row 1st: integer P (1 <= P <= 10000), indicating the number of roads connecting the farm (Barn.

2nd. P + 1 line: two letters separated by space and an integer:

The length of the flag and road connecting to the farm by road (1 <= length <= 1000 ).

Output description Output Description

A separate row contains two items: the marker of the farm where the cows first arrive at the Barn are located, and the length of the path that the cows walk through.

Sample Input Sample Input

5

A d 6

B d 3

C e 9

D Z 8

E Z 3

Sample output Sample Output

B 11


This is the bare Shortest Path. As long as you start to find the single source shortest path from Z, dij and floyed can both be handled.

I am dealing with lowercase letters, the first 26 in the grid, and the last 26 in the case of 26 in particular, the pre-processing when reading characters because there are spaces to use scanf to write this way

scanf("%d\n",&p);scanf("%c %c %d\n",&c1,&c2,&k);

Other Notes: 1. undirected graphs 2. Duplicate edges 3. Self-loops can be eliminated during init.

Basic Idea: Pre-processing is converted into a number. Find the single-source shortest path from the last vertex, and find the smallest one among all uppercase letters.


Put the Code:



-- If you want to see it, you will be afraid of it.

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.