POJ1511 (shortest circuit large data processing)

Source: Internet
Author: User

Invitation Cards
Time Limit: 8000MS Memory Limit: 262144K
Total Submissions: 23357 Accepted: 7675

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
Test instructions: The sum of the sum of the 1th nodes to the rest of the nodes and the sum of the remaining nodes to the number 1th nodes.
Idea: The path of the remaining nodes to the number 1th junction can be reversed to the sum of the paths of the 1th nodes to the remaining nodes. Note: The data volume of this topic is large, and the dynamic adjacency table is used to store the re.
The Dijkstra and SPFA algorithms are compared.
/*Dijkstra 1511 Accepted 39744K 1907MS g++*/#include"Cstdio"#include"Queue"#include"Vector"using namespacestd;Const intmaxn=1000005;Const intinf=0x3fffffff; typedefLong LongLl;typedef pair<int,int>P;structedge{intTo,cost,next;} es[2][MAXN];intv,e;inthead[2][MAXN]; LL D[MAXN];voidAdd_edge (intUintVintCostinttype) {es[type][e].to=v; Es[type][e].cost=Cost ; Es[type][e].next=Head[type][u]; Head[type][u]=E;} LL Dijkstra (intSinttype) {     for(intI=1; i<=v;i++) d[i]=INF; Priority_queue<P,vector<P>,greater<P> >que; D[s]=0, Que.push (P (0, s));  while(!Que.empty ()) {p P=que.top (); Que.pop (); intv=P.second; if(D[v]<p.first)Continue;  for(inti=head[type][v];i!=-1; i=Es[type][i].next) {Edge e=Es[type][i]; if(d[e.to]>d[v]+e.cost) {d[e.to]=d[v]+E.cost;            Que.push (P (d[e.to],e.to)); }}} LL ans=0;  for(intI=1; i<=v;i++) ans+=D[i]; returnans;}intMain () {intT; scanf ("%d",&T);  for(intcas=1; cas<=t;cas++)    {                intp,q; scanf ("%d%d",&p,&p); V=p,e=0;  for(intI=1; i<=v;i++) head[0][i]=head[1][i]=-1;  for(intI=0; i<q;i++)        {            intU,v,co; scanf ("%d%d%d",&u,&v,&co); Add_edge (U,v,co,0); Add_edge (V,u,co,1); E++; } LL Res=0; Res+=dijkstra (1,0); Res+=dijkstra (1,1); printf ("%i64d\n", RES); }    return 0;}

/*SPFA 1511 Accepted 43676K 1875MS g++*/#include"Cstdio"#include"Queue"using namespacestd;Const intmaxn=1000005;Const intinf=0x3fffffff; typedefLong LongLL;structedge{intTo,cost,next;} es[2][MAXN];inthead[2][MAXN];intv,e; LL D[MAXN];intVIS[MAXN]; LL SPFA (intSinttype) {     for(intI=1; i<=v;i++) {D[i]=INF; Vis[i]=0; } Queue<int>que; Vis[s]=1, d[s]=0, Que.push (s);  while(!Que.empty ()) {        intv=Que.front (); Que.pop (); VIS[V]=0;  for(inti=head[type][v];i!=-1; i=Es[type][i].next) {Edge e=Es[type][i]; if(d[e.to]>d[v]+e.cost) {d[e.to]=d[v]+E.cost; if(!Vis[e.to])                    {Que.push (e.to); Vis[e.to]=1; } }}} LL ans=0;  for(intI=1; i<=v;i++) ans+=D[i]; returnans;}intMain () {intT; scanf ("%d",&T);  for(intcas=1; cas<=t;cas++)    {                intp,q; scanf ("%d%d",&p,&Q); V=p,e=0;  for(intI=1; i<=v;i++) head[0][i]=head[1][i]=-1;  for(intI=0; i<q;i++)        {            intU,v,co; scanf ("%d%d%d",&u,&v,&co); es[0][e].to=v,es[0][e].cost=co,es[0][e].next=head[0][u],head[0][u]=E; es[1][e].to=u,es[1][e].cost=co,es[1][e].next=head[1][v],head[1][v]=D; E++; } LL Res=0; Res+=SPFA (1,0); Res+=SPFA (1,1); printf ("%i64d\n", RES); }    return 0;}

The complexity of the heap optimization Dijkstra algorithm is | E|*log (| v|) , the advantage lies in processing sparse graphs.

POJ1511 (shortest circuit large data processing)

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.