Invitation cards
Description
In the age of television, not every people attend theater has CES. antique comedians of malidinesia are aware of this fact. they want to propagate theater and, most of all, antique comedies. they have printed invitation cards with all the necessary information and with the Programme. A lot of students were hired to distribute these invitations among the people. each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people traveling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.
The transport system is very special: All lines are unidirectional and connect exactly two stops. buses leave the originating stop with passangers each half an hour. after reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. x: 00 or X: 30, where 'x' denotes the hour. the parameter for transport between two stops is given by special tables and is payable on the spot. the lines are planned in such a way, that each round trip (I. e. A journey starting and finishing at the same stop) passes through a central checkpoint stop (CCS) where each passenger has to pass a thorough check including body scan.
All the ACM student members leave the CCS each morning. each volunteer is to move to one predetermined stop to invite passengers. there are as follows volunteers as stops. at the end of the day, all students travel back to CCS. you are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
Input
The input consists of n cases. the first line of the input contains only positive integer n. then follow the cases. each case begins with a line containing exactly two integers p and q, 1 <= p, q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. then there are Q lines, each describing one bus line. each of the lines contains exactly three numbers-the originating stop, the destination stop and the price. the CCS is designated by number 1. prices are positive integers the sum of which is smaller than 1000000000. you can also assume it is always possible to get from any stop to any other stop.
Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
Sample Input
2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
Sample output
46
210
Question:
Numbered 1 ~ P station, there are Q bus routes, bus routes only from a start station directly to the terminal, is one-way, each route has its own fare.
P people depart from 1 in the morning. They will arrive at every bus stop and return to in the evening. Calculate the sum of the minimum back-to-forth costs of all.
Ideas:
Find the Shortest Path on both sides, and reverse the side the second time. Just calculate the sum.
PS: This question has a large amount of data. The dijskstra algorithm and the spfa algorithm will time out and need to be optimized. For details, see the remarks.
Code:
1 # include <stdio. h> 2 # include <limits. h> 3 # include <iostream> 4 # include <string> 5 # include <queue> 6 # define maxn 1000000 7 using namespace STD; 8 struct E 9 {10 int begin; 11 int end; 12 int dis; 13} edge1 [maxn + 10], edge2 [maxn + 10]; 14 int dis [maxn + 10], first [maxn + 10]; 15 bool vis [maxn + 10]; 16 int T, S, d, n, k, M; 17 void spfa (INT begin, struct e edge []) 18 {19 for (INT I = 1; I <= N; I ++) 20 {21 dis [I] = int_max; 22 vi S [I] = 0; 23} 24 queue <int> q; 25 Q. Push (BEGIN); 26 dis [begin] = 0; 27 while (! Q. empty () 28 {29 begin = Q. front (); 30 Q. pop (); 31 vis [begin] = 0; 32 for (INT I = first [begin]; edge [I]. begin = begin; I ++) // You can traverse only begin = edge [I]. edge33 if (DIS [edge [I]. end]> dis [begin] + edge [I]. dis) 34 {35 DIS [edge [I]. end] = dis [begin] + edge [I]. DIS; 36 IF (! Vis [edge [I]. end]) 37 {38 Q. push (edge [I]. end); 39 vis [edge [I]. end] = 1; 40} 41} 42} 43} 44 void Init (struct e edge []) // first store each vertex as the first subscript of the knot 45 {46 memset (first, 0, sizeof (first); 47 first [edge [1]. begin] = 1; 48 for (INT I = 2; I <= m; I ++) 49 If (edge [I-1]. begin! = Edge [I]. begin) first [edge [I]. begin] = I; 50} 51 bool CMP (struct e a, struct e B) 52 {53 return. begin <B. begin; 54} 55 int main () 56 {57 int t; 58 CIN> T; 59 While (t --) 60 {61 scanf ("% d ", & N, & M); 62 int x1, x2, X3; 63 for (INT I = 1; I <= m; I ++) 64 {65 scanf ("% d", & X1, & X2, & X3 ); // CIN runs 2600 Ms scanf as long as 1300ms66 // CIN> x1> X2> X3; 67 edge1 [I]. begin = x1, edge1 [I]. end = x2, edge1 [I]. dis = X3; 68 edge2 [I]. begin = x2, edge2 [I]. end = x1, edge2 [I]. dis = X3; 69} 70 sort (edge1 + 1, edge1 + m + 1, CMP); // sort by begin vertex 71 sort (edge2 + 1, edge2 + m + 1, CMP); 72 Init (edge1); 73 spfa (1, edge1); 74 int CNT = 0; 75 for (INT I = 1; I <= N; I ++) 76 CNT + = dis [I]; 77 Init (edge2); 78 spfa (1, edge2); 79 for (INT I = 1; I <= N; I ++) 80 CNT + = dis [I]; 81 printf ("% d \ n", CNT); 82} 83 return 0; 84}