"BZOJ-3627" Path planning layer map + Dijkstra + SPFA

Source: Internet
Author: User

3627: [JLOI2014] path planning time limit:30 Sec Memory limit:128 MB
submit:186 solved:70
[Submit] [Status] [Discuss] Description

I believe everyone has used the map of the path planning function, as long as the input point of origin to find an optimal route. Now tell you a map of the information, please find the optimal path (that is, the shortest path). Given the actual situation, a car full of oil can be opened for a limited time, so on the map to add a few gas stations.

The map consists of points and two-way edges, each of which represents a junction, or it may be a petrol station or a starting point. Some intersections also contain traffic lights. Because after too many traffic lights can be uncomfortable, so request in the case of not exceeding the K traffic lights, at least the average amount of time to spend from the starting point to the end point. Ensure that the starting point and gas station do not have traffic lights.

(the title does not consider the worst case can be added to the oil, only consider the average time spent on the premise, the car can reach the gas station refueling).

Input

The first line input 5 integer n,m,k,limit,cost, indicating that there are n points M edge, the car can open limit long time, and refueling time cost.

The next n lines enter each point information, including the name of the point (with "gas" for the petrol station, "start" as the starting point, "end" as the end), and whether the point has a traffic light, (A, B) (if a=0 is not, a indicates a red light, and B indicates a long green).

The next m line enters each edge information, including the start, end point, and edge name of the edge, and the length of time it takes to pass the edge.

Ensure that the length of the point and edge name is not greater than 20, only uppercase and lowercase letters, numbers and ' _ ' composition.

Output

The average length of a line of output is minimal.

Sample INPUT5 8 1 100 10
Start 0 0
Azhan 10 10
XXGASXX 0 5
Bpoint 20 5
End 0 100
Start Azhan SDF 30
Azhan xxgasxx EWFG 20
Start End R3TG 200
End Azhan 1XQ2 70
Azhan bpoint GG 10
Xxgasxx Bpoint KK 30
Bpoint End DSG 40
Xxgasxx End t_s 100
Sample Output162.500
HINT

A total of 14 sets of data:

3 sets of data to meet n<10,m<20,k<5

There are 3 other groups without traffic lights.

All data meet n<=10000,m<=20000,k<=10, gas station <=50

Answer reserved 3 decimal places

SourceSolution

This problem is very hard. The algorithm is not very difficult, but it is very easy to write.

From the data range and test instructions see is a hierarchical chart, but there are two constraints, respectively, traffic lights and gas stations, but one layer can not limit two conditions, so it is only possible to deal with one of them before considering another.

So first deal with the traffic lights, first to the traffic lights a hierarchical map, each layer represents the number of traffic lights through this layer of the shortest, so do not consider the oil limit can be directly run the shortest possible to get.

But then also have to deal with the oil, but the number of gas stations is relatively small, you can run many times, to find out the shortest distance from each gas station, so that you can get two gas stations can be a box of oil to arrive;

Can arrive in a box of oil there is nothing to be afraid of, and then enumerate to another gas station after the number of traffic lights, build another diagram;

The graph is still a hierarchical chart, each layer represents the shortest possible distance between the gas stations that can be reached directly through the traffic lights on this layer, and then the shortest circuit that runs over the diagram.

Red light situation is need to seek, the method is relatively simple, the answer is $\frac {red^{2}}{2* (red+green)}$, this value can be paid directly to the side, gas station in the shortest way when the cost can be added.

At first, I wrote two Dij and then blew it up, and I changed it into a SPFA, and when I ran to the gas station, it was a special egg. Read what processing is also more brain-free, you can directly think of the starting point is two gas stations, the final answer to lose an additional cost.

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<algorithm>#include<queue>#include<map>#include<string.h>using namespaceStd;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 10010intn,m,k,l,c,gas[1010],gas,id[ the][1100],ID,S,T,PRE[MAXN];structpoint{Charname[ -],id;DoubleAve;BOOLre;} P[MAXN];Doubledist[ -][MAXN];structgraph{structedgenode{intNext,to;DoubleDis;} edge[maxn* -]; inthead[maxn],cnt; InlinevoidAddedge (intUintVDoubleW) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].dis=W;} InlinevoidInsertedge (intUintVDoubleW) {Addedge (u,v,w); Addedge (v,u,w);}#definePa pair<int,int>#defineMake Make_pair#defineDi Top (). First#definePo Top (). Second#defineINF (1LL&LT;&LT;60)DoubleDIS[MAXN]; InlinevoidPredijsktra (intSt) {Priority_queue<pa, vector<pa>, greater<pa> >Q; BOOLvisit[ +][maxn]={0};  for(intI=0; i<=k; i++) for(intj=1; j<=n; J + +) dist[i][j]=INF; Q.push (Make (0, ST)); dist[0][st]=0; visit[0][st]=1;  while(!Q.empty ()) {                intNow=q.po,d=q.di; Q.pop (); visit[d][now]=0;  for(intI=head[now]; I I=Edge[i].next) {                        intTmp=d+ (p[edge[i].to].re?1:0); if(Tmp<=k && dist[d][now]+edge[i].dis+p[edge[i].to]. Ave<=l && dist[tmp][edge[i].to]>edge[i].dis+p[edge[i].to]. Ave+Dist[d][now]) {Dist[tmp][edge[i].to]=dist[d][now]+p[edge[i].to]. Ave+Edge[i].dis; if(!visit[tmp][edge[i].to]) Q.push (Make (tmp,edge[i].to)), visit[tmp][edge[i].to]=1; }}}} inlinevoidSPFA (intSt) {Queue<int>Q;BOOLVISIT[MAXN];  for(intI=1; i<=id; i++) dis[i]=INF; Q.push (ST); DIS[ST]=0.0; visit[st]=1;  while(!Q.empty ()) {                intNow=q.front (); Q.pop (); visit[now]=0; intLay= (now-1)/gas,pos=now%gas;if(!pos) pos=Gas ;  for(intj=0; j+lay<=k; J + +)                     for(intI=HEAD[J*GAS+POS],PPP; I I=Edge[i].next) {PPP=edge[i].to%gas,ppp=!ppp?GAS:PPP; if(dis[(J+lay) *gas+ppp]>dis[now]+edge[i].dis+C) {dis[(J+lay) *gas+ppp]=dis[now]+edge[i].dis+C; if(!visit[(J+lay) *GAS+PPP]) Q.push ((J+lay) *gas+ppp), visit[(J+lay) *gas+ppp]=1; } }}} G1,g2;map<string,int>Hash;intMain () {Freopen ("pathplan.in","R", stdin); Freopen ("Pathplan.out","W", stdout); N=read (), M=read (), K=read (), L=read (), c=read ();  for(intI=1; i<=n; i++)        {            Doublere,gr; scanf ("%S%LF%LF", p[i].name+1, &re,&gr), p[i].id=i,hash[p[i].name+1]=i; if(re>0) p[i].re=1, P[i]. Ave= (Re*re)/2.0/(re+GR); }     for(intI=1; i<=n; i++)        {            strings=p[i].name+1; if(s=="Start"|| s=="End"|| S.find (" Gas")!=string:: NPOs) gas[++gas]=hash[s],pre[hash[s]]=Gas ; if(s=="Start") s=Hash[s]; if(s=="End") t=Hash[s]; }     for(intI=1; i<=m; i++)        {            Charu[ to],v[ to],na[ to];Doubledi; scanf ("%S%S%S%LF", u+1, v+1, na+1,&di); G1. Insertedge (Hash[u+1],hash[v+1],di); }     for(intI=0; i<=k; i++)         for(intj=1; j<=gas; J + +) Id[i][j]=++ID;  for(intI=1; i<=gas; i++) {G1.predijsktra (gas[i]);  for(intj=1; j<=gas; J + +)                if(i!=j) for(intk=0; k<=k; k++)                        if(dist[k][gas[j]]<INF) G2.                Addedge (Id[k][i],id[k][j],dist[k][gas[j]); } G2.SPFA (id[0][pre[s]]); Doubleans=INF;  for(intI=0; i<=k; i++) ans=min (ans,g2.dis[id[i][pre[t]]); printf ("%.3lf\n", ans-C); return 0;}

"BZOJ-3627" Path planning layer map + Dijkstra + 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.