Invitation CardsTime limit:8000ms Memory Limit:262144ktotal submissions:20229accepted:6612
Description
In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia is aware of this fact. They want to propagate theater and, the most of all, antique comedies. They has 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 have assigned exactly one bus stop and he or she stays there the whole day and gives invitation to P Eople travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing a nd robbery.
The transport system is very special:all lines be unidirectional and connect exactly. 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 fee for transport between and stops are given by special tables and are payable on the spot. The lines is planned in such a to, that's round trip (i.e. a journey starting and finishing at the same stop) passes Through a central Checkpoint Stop (CCS) where each passenger have to pass a thorough check including body scan.
All of the ACM student members leave the CCS morning. Each volunteer are to move to a predetermined stop to invite passengers. There is as many volunteers as stops. At the end of the day, all students travel back to CCS. You is to write a computer program this helps ACM to minimize the amount of money to pay every day for the transport of T Heir 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 the 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 is 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 is positive integers the sum of which is smaller than 1000000000. You can also assume it's 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 it S 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
Source
Central Europe 1998
The main topic: give you n nodes and M-bar side. Q: Departure from node 1 to 2, 3, 4 、...、 N-node distances and
From these nodes return the total distance of the node 1 and.
Idea: To find the source point to each node distance problem, with Bellman-ford time complexity O (n*m), the problem of data regulation
Modulus is 1000000, so decisive not. I used a chain forward star (similar to the adjacency table) to store the graph, because the separate
The shortest total distance and the shortest total distance back, so used a two-dimensional chain forward star, a positive edge, a storage counter-edge, but
After the node 1 as the starting point, the positive edge of the SPFA algorithm, and then the node 1 as the starting point for the inverse of the SPFA algorithm.
Look at the discussion area looks like someone with GetChar (), Putchar () can reach 625ms, seemingly is the input and output plug, have not tried ...
Try it sometime.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXM = 1000100;const int MAXN = 1000100;struct edgenode{int to; int W; int next;} Edges[2][maxm];int head[2][maxn],vis[maxn],queue[maxn],outque[maxn];__int64 dist[maxn];bool SPFA (int S,int N,int Flag) {for (int i = 2; I <= N; ++i) dist[i] = 0xFFFFFFFF; memset (vis,0,sizeof (VIS)); memset (outque,0,sizeof (Outque)); int iq = 0; queue[iq++] = S; Vis[s] = 1; Dist[s] = 0; int i = 0,top,k; while (i! = IQ) {top = Queue[i]; Vis[top] = 0; outque[top]++; if (Outque[top] > N) return false; k = Head[flag][top]; while (k >= 0) {if (dist[edges[flag][k].to]-EDGES[FLAG][K].W > Dist[top]) { Dist[edges[flag][k].to] = Dist[top] + edges[flag][k].w; if (!vis[edges[flag][k].to]) {Vis[edgeS[flag][k].to] = 1; queue[iq++] = edges[flag][k].to; }} k = Edges[flag][k].next; } i++; } return true; int main () {int t,n,m,u,v,w; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); memset (head,-1,sizeof (Head)); for (int i = 0; i < M; ++i) {scanf ("%d%d%d", &u,&v,&w); Edges[0][i].to = v; EDGES[0][I].W = W; Edges[0][i].next = Head[0][u]; Head[0][u] = i; edges[1][i].to = u; EDGES[1][I].W = W; Edges[1][i].next = Head[1][v]; HEAD[1][V] = i; } __int64 ans = 0; SPFA (1,n,0); for (int i = 1; I <= N; ++i) if (dist[i]! = 0xFFFFFFFF) ans + = dist[i]; SPFA (1,n,1); for (int i = 1; I <= N; ++i) if (dist[i]! = 0xFFFFFFFF) ans + = dist[i]; printf ("%i64d\n", ans); } return 0;}
POJ1511 invitation Cards "SPFA"