HDU 1535 invitation cards (multi-source point to single point shortest)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1535

Question:

Invitation cards

Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 1044 accepted submission (s): 459

Problem descriptionin the age of television, not necessarily people attend theater 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 writing a computer program
That helps ACM to minimize the amount of money to pay every day for the transport of their employees.

 

Inputthe 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.

Outputfor 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

22 21 2 132 1 334 61 2 102 1 601 3 203 4 102 4 54 1 50
 

Sample output

46210
 

Sourcecentral Europe 1998

Recommendll

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.


Analysis and Summary:

Based on the meaning of the question, you can use the single-source shortest path algorithm to find the minimum total cost.

But it is not easy to do when you come back. It is the minimum cost to arrive at the specified point 1 from each point. If the minimum short circuit is obtained for each vertex, it will definitely time out.

At this time, based on the reverse thinking, re-establish the chart (that is, the original is u --> V to V --> U ), use the single-source shortest path algorithm to obtain the shortest path from 1 to all vertices.

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <utility> using namespace STD; typedef pair <int, int> PII; const int VN = 1000005; const int en = Vn; const int INF = 0x7fffff; struct edge {int V, next, W;} e [En]; priority_queue <PII, vector <PII>, greater <PII> q; int N, size; int head [VN]; int d [VN]; int U [VN], V [VN], W [VN]; void Init () {size = 0; memset (Head,-1, sizeof (head); While (! Q. empty () Q. pop ();} void addedge (int u, int V, int W) {e [size]. V = V, E [size]. W = W; E [size]. next = head [u]; head [u] = size ++;} void Dijkstra (int src) {for (INT I = 1; I <= N; ++ I) d [I] = inf; d [SRC] = 0; q. push (make_pair (d [SRC], Src); While (! Q. Empty () {PII x = Q. Top (); q. Pop (); int u = x. Second; If (d [u]! = X. First) continue; For (int e = head [u]; e! =-1; E = E [e]. next) {int TMP = d [u] + E [e]. w; If (d [E [e]. v]> TMP) {d [E [e]. v] = TMP; q. push (make_pair (TMP, E [e]. v) ;}}} int main () {int T, M; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M); Init (); For (INT I = 0; I <m; ++ I) {scanf ("% d", & U [I], & V [I], & W [I]); addedge (U [I], V [I], W [I]);} Dijkstra (1); int ans = 0; For (INT I = 1; I <= N; ++ I) ans + = d [I]; Init (); For (INT I = 0; I <m; ++ I) // reverse map addedge (V [I], U [I], W [I]); Dijkstra (1); For (INT I = 1; I <= N; ++ I) ans + = d [I]; printf ("% d \ n", ANS);} return 0 ;}

 
  

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

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.