POJ 1511 Invitation Cards (Shortest Path)

Source: Internet
Author: User

 

Invitation Cards
Time Limit:8000 MS   Memory Limit:262144 K
Total Submissions:14556   Accepted:4710

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

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

Sample Output

46210

Source

Central Europe 1998 seems simple. The time is 8 s, but there are a lot of points. Both Dijkstra and floyed will time out... SPFA is more efficient for a few multilateral sites. Use the SPFA twice to find the shortest path for the source image and the inverse image ,,
/* POJ 1511: A directed graph with m edges of n vertices. Calculate the sum of the shortest paths from 1st to other vertices, calculate the sum of the shortest paths from the other vertices to the first vertex, and obtain the sum of the two sums. first, store the input m edges in an adjacent table, and then create an adjacent table after reversing the m edges (that is, changing the end point to the start point and the start point to the end point, use the SPFA algorithm to calculate the shortest path from vertex 1 to each other and then add the two adjacent tables. point. Using Dijkstra and floyed will time out G ++ 40840 K 1750 ms */# include <stdio. h> # include <algorithm> # include <iostream> # include <string. h> using namespace std; const int MAXN = 1000010; const int MAXE = 1000010; const int INF = 1000000009; int head [MAXN]; bool vis [MAXN]; int que [MAXN]; int dist [MAXN]; struct Edge {int to; int next; int v;} edge [MAXE]; // inverse graph int head2 [MAXN]; edge edge2 [MAXE]; int tol; void add (int a, int B, int c) {edge [tol]. to = B; ed Ge [tol]. v = c; edge [tol]. next = head [a]; head [a] = tol ++;} int tol2; void add2 (int a, int B, int c) {edge2 [tol2]. to = B; edge2 [tol2]. v = c; edge2 [tol2]. next = head2 [a]; head2 [a] = tol2 ++;} void SPFA (int start, int n) // forward graph {int front = 0; int rear = 0; for (int v = 1; v <= n; v ++) {if (v = start) {que [rear ++] = v; vis [v] = true; dist [v] = 0;} else {vis [v] = false; dist [v] = INF ;}} while (front! = Rear) {int u = que [front ++]; vis [u] = false; if (front> = MAXN) front = 0; for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. to; if (dist [v]> dist [u] + edge [I]. v) {dist [v] = dist [u] + edge [I]. v; if (! Vis [v]) {que [rear ++] = v; vis [v] = true; if (rear> = MAXN) rear = 0 ;}}}}} void SPFA2 (int start, int n) // inverse graph {int front = 0; int rear = 0; for (int v = 1; v <= n; v ++) {if (v = start) {que [rear ++] = v; vis [v] = true; dist [v] = 0 ;} else {vis [v] = false; dist [v] = INF ;}} while (front! = Rear) {int u = que [front ++]; vis [u] = false; if (front> = MAXN) front = 0; for (int I = head2 [u]; I! =-1; I = edge2 [I]. next) {int v = edge2 [I]. to; if (dist [v]> dist [u] + edge2 [I]. v) {dist [v] = dist [u] + edge2 [I]. v; if (! Vis [v]) {que [rear ++] = v; vis [v] = true; if (rear> = MAXN) rear = 0 ;}}}}} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout); int T; int n, m; int a, B, c; scanf ("% d", & T); while (T --) {scanf ("% d ", & n, & m); tol = tol2 = 0; memset (head,-1, sizeof (head); memset (head2,-1, sizeof (head2 )); while (m --) {scanf ("% d", & a, & B, & c); add (a, B, c); add2 (B, a, c) ;}long long ans = 0; SPFA (1, n); for (int I = 2; I <= n; I ++) ans + = dist [I]; SPFA2 (1, n); for (int I = 2; I <= n; I ++) ans ++ = dist [I]; printf ("% I64d \ n", ans);} 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.