Invitation Cardstime limit:16000/8000ms (java/other) Memory limit:524288/262144k (java/other) Total Submission (s): 7 Accepted Submission (s): 1Problem DescriptionIn 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. 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 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-get from any stop-to-any OTher stop. outputfor each case, print one line containing the minimum amount of money to being paid each day by ACM for the travel Costs of its volunteers. sample Input22 2 1 334 2 102 1 601 3 203 4 102 4 si 1 50 sample output46210
The main idea: to give n points and n a forward edge, all points to the source point 1 back and forth the shortest possible sum (to ensure that each point can return to the source point 1)
Problem-Solving ideas: This data range is too large, obviously can not be used floyd,dijstra,bellman-ford these algorithms, with SPFA words can not be used adjacent matrix to save,
Because there are so many points, the adjacency table is stored with SPFA.
A little bit of notice is that the back and forth and only need to reverse all the edges from 1 to all points to find the shortest circuit is their minimum circuit
AC Code:
1#include <stdio.h>2#include <string.h>3 #defineINF 99999999994#include <iostream>5#include <queue>6#include <algorithm>7 using namespacestd;8 structnode9 {Ten intto ; One intW; A intNext; - }; -Queue <int>Q; the intn,m; -Node list[1000010]; -Node list1[1000010]; - intvis[1000010]; + intdis[1000010]; - inth1[1000010]; + inth2[1000010]; A voidSPFA () at { - intI,j,u; - for(i =1; I <= N; i + +) - { -Dis[i] =inf; -Vis[i] =0; in } -Q.push (1); todis[1] =0; +vis[1] =1; - the while(!q.empty ()) * { $U =Q.front ();Panax Notoginseng Q.pop (); -Vis[u] =0; the for(j = h1[u]; j; j =list[j].next) + { A if(Dis[list[j].to] > dis[u]+LIST[J].W) the { +Dis[list[j].to] = dis[u]+LIST[J].W; - if(!vis[list[j].to]) $ { $ Q.push (list[j].to); -Vis[list[j].to] =1; - } the } - }Wuyi } the } - voidspfa1 () Wu { - intI,j,u; About for(i =1; I <= N; i + +) $ { -Dis[i] =inf; -Vis[i] =0; - } AQ.push (1); +dis[1] =0; thevis[1] =1; - $ while(!q.empty ()) the { theU =Q.front (); the Q.pop (); theVis[u] =0; - for(j = h2[u]; j; j =list1[j].next) in { the if(Dis[list1[j].to] > dis[u]+LIST1[J].W) the { AboutDis[list1[j].to] = dis[u]+LIST1[J].W; the if(!vis[list1[j].to]) the { the Q.push (list1[j].to); +Vis[list1[j].to] =1; - } the }Bayi } the } the } - intMain () - { the intI,j,t,u,v,w,ans; thescanf"%d",&t); the while(T--) the { -scanf"%d%d",&n,&m); thememset (H1,0,sizeof(H1)); thememset (H2,0,sizeof(H2)); the for(ans =1, i =0; I < m; i + +)94 { thescanf"%d%d%d",&u,&v,&W); theNode temp = {V,w,0}; theList[ans] =temp;98List[ans].next =H1[u]; AboutH1[u] =ans; -Temp.to =u;101List1[ans] =temp;102List1[ans].next =H2[v];103H2[V] =ans;104Ans + +; the }106 Long Longsum =0;107 SPFA ();108 for(i =1; I <= N; i + +)109Sum + =Dis[i]; the spfa1 ();111 for(i =1; I <= N; i + +) theSum + =Dis[i];113printf"%lld\n", sum); the } the return 0; the}
View Code
POJ 1511 Invitation Cards