[Dijkstra] algorithm lab question 12.1 automatic Responder

Source: Internet
Author: User

After a long time, I finally completed the problem using the Dijkstra algorithm!

The hard work of one day is not in vain.

In fact, this question can also be implemented using the polye algorithm...

This is the task for tomorrow !!!!

 

Question:

Algorithm lab question 12.1 automatic responder <br/>★Problem description <br/> Ares's rot is a very punctual but forgetful person, each time he wants to go to a place, he must calculate the minimum time required from <br/> Start Point to end point. At first, Ares actively answered each rot's question and finally one day, tired of <br/>, he decided not to answer the rot. Looking at rot's innocent and disappointing eyes, Ares decided to write an automatic responder for rot <br/> to solve the rot problem. <Br/>★Programming task <br/> calculate the minimum time required from the start point to the end point for the given MAP and several rot queries. <Br/>★Data input <br/> the input data is provided by the input.txt file. The first input contains three integers: n, m, and T, indicating that there is a <br/> location on the map, M unidirectional routes, and t-inquiry. The number of N locations is 1 .. n. <Br/> next, there are m rows. Each row has three integers x, y, and W, which indicate that it takes w minutes from X to Y. In the next <br/> T line, each row has two integers, A and B, which indicate how many minutes the rot queries from A to B? <Br/> data range: <br/> 1 ≤ n ≤ 300, 1 ≤ m ≤ 25,000, 1 ≤ T ≤ 40,000 <br/> 1 ≤ x, y ≤ n, x! = Y, 1 ≤ W ≤ 1,000,000 <br/> 1 ≤ a, B ≤ n,! = B <br/>★Data Output <br/> outputs the program running result to the output.txt file. For each query, an integer is output to indicate the minimum time required from <br/> Start A to end B. If a cannot reach B, a line "-1" is output ".

 

Test Data

5 6 3
1 2 12
3 2 8
1 3 5
2 5 3
3 4 4
2 4 8
5 3
4 2
1 4

 

Result

-1

-1

9

# Include <iostream> <br/> # include <list> <br/> # include <string. h> <br/> # define infinite 999999999 <br/> # define numofpoint 301 <br/> using namespace STD; <br/> // way subscript starts from 1 <br/> void Dijkstra (INT way [] [numofpoint], int S, int Dist [], int Prev [], int N) <br/>{< br/> int I; <br/> int K; <br/> List <int> L; <br/> List <int >:: iterator next, IT, TMP; <br/> // initial <br/> for (I = 1; I <= N; I ++) <br/>{< br/> Dist [I] = way [S] [I]; <br/> If (Dist [I] = infinite) <br/> Prev [I] = 0; <br/> else <br/> {<br/> Prev [I] = s; <br/> L. push_front (I); <br/>}< br/> Prev [s] = s; <br/> Dist [s] = 0; <br/> while (! L. empty () <br/>{< br/> it = next = L. begin (); <br/> next ++; <br/> while (next! = L. End () <br/>{< br/> // The error code is returned... <Br/> If (Dist [* It]> Dist [* Next]) <br/> it = next; <br/> next ++; <br/>}< br/> K = * it; <br/> L. erase (it); <br/> for (I = 1; I <= N; I ++) <br/> {<br/> If (way [k] [I]! = Infinite & <br/> (prev [I] = 0 | Dist [I]> way [k] [I] + dist [k]) <br/> {<br/> Dist [I] = way [k] [I] + dist [k]; <br/> If (prev [I] = 0) <br/> L. push_front (I); <br/> Prev [I] = K; <br/>}</P> <p> int main () <br/>{< br/> int n, m, T; <br/> int X, Y, W; <br/> int A, B; <br/> int I, j; <br/> int way [numofpoint] [numofpoint]; <br/> int Dist [numofpoint] [numofpoint], Prev [numofpoint]; <br/> while (scanf ("% D", & N, & M, & T )! = EOF) <br/>{< br/> memset (way, 0, sizeof (way); <br/> for (I = 1; I <= N; I ++) <br/> for (j = 1; j <= N; j ++) <br/> if (I = J) <br/> way [I] [J] = 0; <br/> else <br/> way [I] [J] = infinite; <br/> for (I = 0; I <m; I ++) <br/> {<br/> scanf ("% d ", & X, & Y, & W); <br/> If (way [x] [Y]> W) <br/> way [x] [Y] = W; <br/>}</P> <p> for (I = 1; I <= N; I ++) <br/> Dijkstra (way, I, dist [I], Prev, n); <br/> for (I = 0; I <t; I ++) <br/>{< br/> scanf ("% d", & A, & B ); <br/> If (Dist [a] [B] = infinite) <br/> printf ("-1/N "); <br/> else <br/> printf ("% d/N", DIST [a] [B]); <br/>}< br/> return 0; <br/>}

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.