POJ-1511 Invitation Cards (Shortest way, SPFA)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1511

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

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

Sample Output

46210

Question: Given a graph, ask how much time it takes to go back and forth from the starting point to each point
Problem Solving Ideas: will have to the beginning of the line segment and the end of the interchange can be any point to the starting point of the shortest, due to the large amount of data, need to use SPFA, while using CIN will also time out. would also like to write a Dfs, found that will explode stack, only affixed to the BFS code

1#include <iostream>2#include <cstring>3#include <cmath>4#include <algorithm>5#include <map>6#include <cstdio>7#include <queue>8 9 using namespacestd;Ten  One Const intLEN =1000005; A Const intINF =0x3f3f3f3f; -  - structedge{ the     intto, next; -     Long LongVal; -}edge[2][len]; -  + inth[2][len], cnt1, Cnt2; - intp, q; + Long LongDis[len]; A BOOLVis[len]; at  - voidSPFA_BFS (intCNT) { -queue<int>Q; -memset (Dis, INF, LEN *sizeof(Long Long ) ); -memset (Vis,false, LEN *sizeof(BOOL ) ); -      indis[1] =0; -Q.push (1 ); tovis[1] =true; +  -      while( !Q.empty ()) { the         intx; *x = Q.front (); Q.pop (); VIS[X] =false; $ Panax Notoginseng          for(intk = H[cnt][x]; k!=0; K =Edge[cnt][k].next) { -             inty =edge[cnt][k].to; the             if(Dis[x] + Edge[cnt][k].val <Dis[y]) { +Dis[y] = Dis[x] +Edge[cnt][k].val; A                 if( !Vis[y]) { theVis[y] =true; + Q.push (y); -                 } $             } $         } -     } - } the  - intMain () {Wuyi     intN; thescanf"%d", &n); -      while(n-- ){ WuCnt1 = Cnt2 =1; -scanf"%d%d", &p, &q); About         intBeg, end; $         Long LongVal; -memset (H,0,sizeof(h)); -          for(inti =0; i < Q; i++ ){ -scanf"%d%d%lld", &beg, &end, &val); Aedge[0][cnt1].to =end; +edge[0][cnt1].val =Val; theedge[0][cnt1].next = h[0][beg]; -h[0][beg] = cnt1++; $  theedge[1][cnt2].to =Beg; theedge[1][cnt2].val =Val; theedge[1][cnt2].next = h[1][end]; theh[1][end] = cnt2++; -         } in  the         Long LongAns =0; theSPFA_BFS (0 ); About          for(inti =1; I <= p; i++) ans + =Dis[i]; theSPFA_BFS (1 ); the          for(inti =1; I <= p; i++) ans + =Dis[i]; the  +cout << ans <<Endl; -     } the Bayi     return 0; the}

POJ-1511 Invitation Cards (Shortest way, SPFA)

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.