Graph theory (network flow): [SDOI2010] Interstellar racing

Source: Internet
Author: User

DescriptionThe 10-year-old Galaxy Racing competition is starting again. As one of the biggest events in the Galaxy, the winner of this project is undoubtedly the dream of many people, and the one from Jason's Alpha star. The race is made up of N planets and M two-way interstellar routes, each of which has a different gravitational value. The competition asked the riders to start with a celestial body that had no route between the N planets, and to visit the N planets every time, and the first person to accomplish that goal was victorious. Because the format is very open, many people are driving strange, homemade cars to compete. The long-driving car called the Super Power Donkey, this is a gathering of the galaxy's most cutting-edge technology crystallization of the dream car. As the most high-tech products, the Super Electric Donkey has two modes of movement: high-speed sailing mode and ability burst mode. In high-speed mode, an ultra-electric donkey launches an antimatter engine that travels at high speed along the interstellar route at a speed of light. In the ability to burst mode, the Super Electric donkey out of the bondage of time and space, the use of super-ability to jump--after a period of positioning, it can instantly move to any planet. Days days wondering people wish, the day before the game, Super Electric Donkey in an ion storm unfortunate damage, function has some obstacles: in the use of high-speed navigation mode, only from each planet to the gravity of the larger planet, or the car will explode. Despite the problems of the beloved car, he still believes that he can win. He has found the smartest sage in the Galaxy-you, please arrange a match for him so that he can finish the game with the least amount of time. InputThe first line is a two positive integer N, M. The second row N number A1~an, where AI represents the location time required to reach planet I using the ability burst mode. Next m line, 3 positive integers per line UI, VI, WI, indicates that there is an interstellar route between the planets numbered UI and VI that requires sailing WI time. The input data has been sorted by gravitational value, i.e. the small number of planets has a small gravitational value, and no two planets have the same gravitational value. Outputcontains only a positive integer that represents the minimum time required to complete the match. Sample Input

Starrace.in

3 3
1 100 100
2 1 10
1 3 1
2 3 1Sample Output

Starrace.out

12

HINT

Description: Use the ability to burst mode to Planet 1, which takes time 1.
Then switch to high-speed sailing mode and sail to Planet 2, which takes time 10.
Then continue sailing to Planet 3 to complete the race, which takes time 1.
Although it looks better from Planet 1 to Planet 3 to Planet 2, we can't do that because it can cause a super electric donkey to explode.

For 30% of data n≤20,m≤50;
For 70% of data n≤200,m≤4000;
For 100% of data n≤800, m≤15000. No number in the input data will exceed 10^6.
The input data guarantees that there is at most one fairway between any two planets, and there is no planet to its own fairway.

Solve the problem by using the model of binary graph.

The left half represents the edge of the out point, the right half of the edge of the point, the instantaneous movement of the direct s into the point, there is a normal edge directly in the binary diagram.

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 using namespacestd;6 Const intmaxn= .;7 Const intmaxm=500010;8 Const intinf=1061109567;9 intN,M,W[MAXN],CNT,FIR[MAXN],TO[MAXM],NXT[MAXM];Ten intCAP[MAXM],VAL[MAXM],PATH[MAXN],VIS[MAXN],DIS[MAXN]; Onequeue<int>Q; A structnet_flow{ -Net_flow () {cnt=1;} -      the     voidAddintAintBintCintv) { -nxt[++cnt]=fir[a];cap[cnt]=C; -to[cnt]=b;val[cnt]=v;fir[a]=CNT; -     } +      -     voidAddedge (intAintBintCintv) { +Add (a,b,c,v); Add (B,a,0,-v); A     } at      -     intSPFA (intSintT) { -memset (DIS, the,sizeof(DIS)); -Q.push (S); vis[s]=1;d is[s]=0; -          while(!Q.empty ()) { -             intX=q.front (); Q.pop (); vis[x]=0; in              for(intI=fir[x];i;i=Nxt[i]) -                 if(cap[i]&&dis[to[i]]>dis[x]+Val[i]) { todis[to[i]]=dis[x]+Val[i]; +                     if(!Vis[to[i]] Q.push (To[i]); -vis[to[i]]=1;p ath[to[i]]=i; the                 } *         } $         returnDis[t];Panax Notoginseng     } -      the     int(intSintT) { +         intp=t,f=INF; A          while(p!=S) { thef=min (f,cap[path[p]]); +p=to[path[p]^1]; -}p=T; $          while(p!=S) { $cap[path[p]]-=F; -cap[path[p]^1]+=F; -p=to[path[p]^1]; the         } -         returnF;Wuyi     } the      -     intMCMF (intSintT) { Wu         intv=0, D; -          while((D=SPFA (s,t))! =INF) Aboutv+=d*(s,t); $         returnv; -     } - }MCMF; -  A ints,t; + intMain () { theFreopen ("starrace.in","R", stdin); -Freopen ("Starrace.out","W", stdout); $scanf"%d%d", &n,&m); s=0; t=2*n+1; the      for(intI=1; i<=n;i++){ thescanf"%d",&w[i]); theMcmf.addedge (S,i,1,0); theMcmf.addedge (S,i+n,1, W[i]); -Mcmf.addedge (I+n,t,1,0);  in     } the      for(intI=1, a,b,v;i<=m;i++){ thescanf"%d%d%d",&a,&b,&v); About         if(a>b) Swap ( A, a); theMcmf.addedge (A,b+n,1, v); the     } theprintf"%d\n", MCMF. MCMF (s,t)); +     return 0; -}

Graph theory (network flow): [SDOI2010] Interstellar racing

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.