Bzoj 3627 jloi2014 route planning hierarchy chart + heap optimization spfa jloi2014 all achieved!

Source: Internet
Author: User

Given an undirected graph, each edge has edge weight, and some points have the right. Some points are gas stations, and the shortest path from the start point to the end point is obtained, so that the number of authorized points does not exceed K times, a tube of oil can only take the limit time, the time is up to the gas station to spend cost time to fuel

The formula for calculating the traffic light is red * red/2/(Red + green). Many people in the test room did not launch the formula. I will not write the formula when I launch the formula, but I will not write it, 26 points

There are many restrictions... Considering k <= 10 and gas station <= 50, we can perform layered processing on K to reduce the graph point and convert it into a graph that walks between gas stations, in this way, the restriction conditions of K and limit are removed.

First, we enumerate each gas station (the starting point and starting point are regarded as a gas station), run spfa once at the gas station as the starting point, and then add a direct edge with a length not greater than limit to other gas stations, in this way, the point other than the gas station can die.

Then run spfa again from the source point.

There are few good people, few good people, and few people... I have ranked second in all the bad code .. Because there are two people in total...

Spfa run 50 times, I am afraid that the heap optimization will be used for timeout. No heap optimization may lead to endless crashes.

#include<map>#include<string>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 10100using namespace std;map<string,int>a;string st;struct abcd{int to,lights,next;bool usable;double f;}table[100100];int head[M],tot,top;int n,m,k,cnt,s,t;double limit,cost,light[M];bool is_gasonline_stand[M];int gasonline_stands[100];double f[M][11],ans=2147483647;pair<int,int>heap[M*10];int pos[M][11];void pop();void push_up(int t);void SPFA(int from);void insert(pair<int,int>x);void add(int x,int y,double z);void Final_SPFA(){int i;memset(f,0x42,sizeof f);insert( make_pair(s,0) );f[s][0]=0;while(top){pair<int,int>x=heap[1];pop();for(i=head[x.first];i;i=table[i].next)if( table[i].usable )if( x.second+table[i].lights<=k && f[x.first][x.second]+table[i].f<f[table[i].to][x.second+table[i].lights] ){f[table[i].to][x.second+table[i].lights]=f[x.first][x.second]+table[i].f;if( !pos[table[i].to][x.second+table[i].lights] )insert( make_pair( table[i].to , x.second+table[i].lights ) );elsepush_up( pos[table[i].to][x.second+table[i].lights] );}}for(i=0;i<=k;i++)ans=min(ans,f[t][i]);}int main(){int i,x,y;double z,green,red;cin>>n>>m>>k>>limit>>cost;for(i=1;i<=n;i++){cin>>st;scanf("%lf%lf",&red,&green);a[st]=++cnt;if(st=="start")s=i,gasonline_stands[++gasonline_stands[0]]=i;else if(st=="end")t=i,gasonline_stands[++gasonline_stands[0]]=i;else if( ~st.find("gas") )is_gasonline_stand[i]=1,gasonline_stands[++gasonline_stands[0]]=i;if(red==0)light[i]=0;elselight[i]=red*red/2.0/(red+green);}for(i=1;i<=m;i++){cin>>st;x=a[st];cin>>st;y=a[st];cin>>st;scanf("%lf",&z);add(x,y,z);add(y,x,z);}for(i=1;i<=gasonline_stands[0];i++)SPFA(gasonline_stands[i]);Final_SPFA();printf("%.3lf\n",ans-cost);}void add(int x,int y,double z){table[++tot].to=y;table[tot].lights=light[x]?1:0;table[tot].f=z+light[x];table[tot].next=head[x];head[x]=tot;}void push_up(int t){while( t>1 && f[heap[t].first][heap[t].second]<f[heap[t>>1].first][heap[t>>1].second] )swap(heap[t],heap[t>>1]),swap(pos[heap[t].first][heap[t].second],pos[heap[t>>1].first][heap[t>>1].second]),t>>=1;}void insert(pair<int,int>x){heap[++top]=x;pos[x.first][x.second]=top;push_up(top);}void pop(){pos[heap[1].first][heap[1].second]=0;heap[1]=heap[top];heap[top--]=heap[0];pos[heap[1].first][heap[1].second]=1;int t=2;while(t<=top){if( t<top && f[heap[t].first][heap[t].second]>f[heap[t+1].first][heap[t+1].second] )t++;if( f[heap[t].first][heap[t].second]<f[heap[t>>1].first][heap[t>>1].second] )swap(heap[t],heap[t>>1]),swap(pos[heap[t].first][heap[t].second],pos[heap[t>>1].first][heap[t>>1].second]),t<<=1;elsebreak;}}void SPFA(int from){int i,j;memset(f,0x42,sizeof f);insert( make_pair(from,0) );f[from][0]=0;while(top){pair<int,int>x=heap[1];pop();for(i=head[x.first];i;i=table[i].next)if(!table[i].usable)if( x.second+table[i].lights<=k && f[x.first][x.second]+table[i].f<f[table[i].to][x.second+table[i].lights] && f[x.first][x.second]+table[i].f<=limit ){f[table[i].to][x.second+table[i].lights]=f[x.first][x.second]+table[i].f;if( !pos[table[i].to][x.second+table[i].lights] )insert( make_pair( table[i].to , x.second+table[i].lights ) );elsepush_up( pos[table[i].to][x.second+table[i].lights] );}}for(i=1;i<=gasonline_stands[0];i++){if(gasonline_stands[i]==from)continue;for(j=0;j<=k;j++)if(f[ gasonline_stands[i] ][j]<=limit)add( from , gasonline_stands[i] , f[ gasonline_stands[i] ][j]+cost ),table[tot].lights=j,table[tot].usable=1;}}


Bzoj 3627 jloi2014 route planning hierarchy chart + heap optimization spfa jloi2014 all achieved!

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.