Zoj 1064 (floyed STL at the shortest distance)

Source: Internet
Author: User
// I used the data structure of MAP and queue, floyed Algorithm . According to the requirement of the question, first sort the output content in the priority queue, and then wa several times. We also need to pay attention to the decimal point, based on my previous experience, I converted the data stored in double to an int Based on the inaccuracy. The other is the question of rounding the data to 0.5. Finally, I want to see the question clearly, at first, I thought that the output condition was to consider the relationship between the mileage recorded from the brand to the target and the size recorded in the previous city. Later, I found that there was no need to dig the previous distance, that is to say, distance is remembered from the previous City, and we can also think of it from the reality. A bunch of long-winded attachments Code # Include <stdio. h >#include <iostream >#include <queue >#include <map> using namespace STD; Map <int, string> m; struct node {int X; string name; friend bool operator <(node A, Node B) {if (. x! = B. x) return. x> B. x; else return. name> B. name ;}; priority_queue <node> q; Map <int, string >:: iterator it; const int INF = 1000000000; const int maxn = 31; void Floyd (int n, int map [] [maxn], int Dist [] [maxn], int pre [] [maxn]) {int I, j, k; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {Dist [I] [J] = map [I] [J]; pre [I] [J] = I ;}for (k = 0; k <n; k ++) for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) if (Dist [I] [k]! = Inf & Dist [k] [J]! = Inf & Dist [I] [J]> Dist [I] [k] + dist [k] [J]) {Dist [I] [J] = DIST [I] [k] + dist [k] [J]; pre [I] [J] = pre [k] [J]; Dist [J] [I] = DIST [J] [k] + dist [k] [I]; pre [J] [I] = pre [k] [I] ;}} int main () {int n, m, K, s; int X, Y; int T; node P; double TMP; char name [20]; int map [maxn] [maxn]; int Dist [maxn] [maxn]; int pre [maxn] [maxn]; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M, & K ); for (INT I = 0; I <n; I ++) for (Int J = 0; j <n; j ++) {if (I = J) map [I] [J] = 0; else map [I] [J] = inf;} while (M --) {scanf ("% d % lf", & X, & Y, & TMP ); map [x] [Y] = (INT) (TMP * 100 + 0.01); map [y] [x] = (INT) (TMP * 100 + 0.01 );} floyd (n, MAP, DIST, pre); While (k --) {scanf ("% d % s", & X, name); m [x] = Name ;} scanf ("% d", & S); While (s --) {scanf ("% d % lf", & X, & Y, & TMP ); for (INT I = 0; I <n; I ++) {If (Dist [y] [I]! = Inf & I! = X) {If (Dist [x] [I]! = Inf) {If (Dist [x] [I] <Dist [y] [I] + dist [x] [Y]) continue;} It = m. find (I); If (it! = M. end () {P. X = (Dist [y] [I] + 50 + dist [x] [Y]-(INT) (TMP * 100 + 0.01)/100; p. name = it-> second; q. push (p) ;}} while (! Q. empty () {node front = Q. top (); cout <front. name; For (Int J = 0; j <20-front.name.length (); j ++) cout <""; cout <front. x <Endl; q. pop ();} If (s) printf ("\ n");} M. clear (); If (t) printf ("\ n");} return 0 ;}

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.