[Noip2001] car travel route

Source: Internet
Author: User
Tags getv
Description Description

After summer vacation, the car in City A wants to travel to City B with friends. She knows that each city has four airports located on four vertices in a rectangle, and there is a straight high-speed railway between two airports in the same city, the Unit mileage of the high-speed railway in the city I is Ti. There are routes between any two airports in different cities. The unit mileage of all routes is T.

So how should the car arrange the route to City B to save as much cost as possible? She found this was not a simple problem, so she came to ask you.
Task
Find a travel route from City A to City B. You can select any airport to start and arrive at the city, which requires the minimum total cost.

Input description Input description

The first behavior is a positive integer N (0 <= n <= 10), indicating that there are N groups of test data.
The first row of each group has four positive integers S, T, A, and B.
S (0 <S <= 100) indicates the number of cities, t indicates the price per unit of aircraft mileage, and A and B are the numbers of cities A and B respectively, (1 <=, B <= s ).
Next, there are s rows, where row I has seven positive integers xi1, yi1, xi2, yi2, xi3, yi3, Ti, in which (xi1, yi1), (xi2, yi2), (xi3, yi3) are the coordinates of any three airports in the city I, and t I is the price of the unit mileage of the expressway in the city I.

Output description Output description

There are n rows in total, and one row corresponds to the test data.

Sample Input Sample Input

1
3 10 1 3
1 1 1 3 3 1 30
2 5 7 4 5 2 1
8 6 8 8 11 6 3

Sample output Sample output

47.5

Analysis

The only problem with the Shortest Path = is how to change the Shortest Path of the "Four sources and four endpoints" to a problem of low complexity. Using the idea of network stream Modeling in Liu rujia's training guide, we may wish to build a virtual airport 0, which is connected to a side with a weight of 0 at each of the four airports of City, you can use the single-source shortest path algorithm. In addition .. Because the number of edges e in the question is very close to n2, so using Dijkstra will be much faster than spfa (2 S and 4 s ).... Don't look at me. I just remembered it after I wrote spfa. = _ = |

 

1 // awsm. definer → _ →
2 // Email: [email protected]
3 # include <cstdio>
4 # include <deque>
5 # include <cmath>
6 # include <memory. h>
7 # include <iostream>
8 # define Max 0x3f3f3f
9 # define Mod (x) (SQRT (x) * (X )))
10 using namespace STD;
11 struct v {// vector (location)
12 int x, Y;
13 V () {}; V (int A, int B): X (A), y (B ){};
14 V operator + (const V & B) {return V (x + B. X, Y + B. Y );}
15 V operator-(const V & B) {return V (x-b.x, y-b.y );}
16 int operator * (const V & B) {return x * B. x + y * B. Y ;}
17 };
18 inline v get4 (V & P1, V & p2, V & P3 ){
19 if (P2-P1) * (P3-P1) = 0) return P2-P1 + P3;
20 if (P1-P2) * (P3-P2) = 0) return P1-P2 + P3;
21 return P1-P3 + P2;
22}
23 inline void getv (V & K ){
24 scanf ("% d", & K. X, & K. Y );
25}
26 int N, S, T, a, B; // The meaning of s will be changed in function "work "...
27 double dis [404] [404] = {0 };
28 inline void makegragh () {// build dis [] []
29 v P [2, 404];
30 int Ti;
31 For (INT I = 1; I <= s; I + = 4 ){
32 getv (P [I]), getv (P [I + 1]), getv (P [I + 2]); scanf ("% d", & Ti );
33 P [I + 3] = get4 (P [I], p [I + 1], p [I + 2]);
34 For (Int J = 0; j <3; ++ J) for (int K = J; k <4; ++ K)
35 DIS [I + J] [I + k] = dis [I + k] [I + J] = Ti * Mod (P [I + k]-P [I + J]);
36 For (Int J = 1; j <I; ++ J) for (int K = 0; k <4; ++ K)
37 dis [J] [I + k] = dis [I + k] [J] = T * Mod (P [I + k]-P [J]);
38}
39}
40 inline double spfa (){
41 deque <int> q;
42 int K = 4 * A-3, K = 4 * B-3;
43 Q. push_front (K), Q. push_front (k + 1), Q. push_front (K + 2), Q. push_front (K + 3 );
44 bool in_q [404] = {0}; in_q [k] = in_q [k + 1] = in_q [K + 2] = in_q [K + 3] = 1;
45 double cost [404];
46 int I, T;
47 for (I = 1; I <= s; ++ I) Cost [I] = max;
48 cost [k] = cost [k + 1] = cost [K + 2] = cost [K + 3] = 0;
49 while (! Q. Empty ()){
50 t = Q. Front (); q. pop_front (); in_q [T] = 0;
51 for (I = 1; I <= s; ++ I ){
52 if (I = T) continue;
53 If (cost [I]> cost [T] + dis [I] [T]) {// relax
54 cost [I] = cost [T] + dis [I] [T];
55 if (in_q [I]) continue;
56 If (Q. Empty () | cost [q. Front ()] <cost [I]) Q. push_back (I );
57 else Q. push_front (I );
58 in_q [I] = 1;
59}
60}
61}
62 Return min (cost [K], cost [k + 1]), min (cost [K + 2], cost [K + 3]);
63}
64 void work (){
65 scanf ("% d", & S, & T, & A, & B); S * = 4; // s-> sum of airports
66 makegragh ();
67 printf ("%. 1f \ n", spfa (); // (0)-> (4 * n + 1)
68}
69 int main (){
70 # ifdef debug
71 freopen ("test. In", "r", stdin );
72 # endif
73 scanf ("% d", & N );
74 while (n --)
75 work ();
76 return 0;
77}

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.