Uvalive 6485 Electric Car Rally (BFS, priority queue)

Source: Internet
Author: User

Https://icpcarchive.ecs.baylor.edu/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4496

In an attempt to demonstrate the practicality of electric cars, eleccarco is already soring a cross-country
Road Rally. There are n charging stations for the rally where cars may check in and charge their batteries.
The rally may require multiple days of travel. Each car can travel four hours (240 minutes)
Charges. A car must be plugged into a charger for two minutes for each minute of Travel Time. Cars
Start the rally at noon on the first day, fully charged. Cars are permitted remain at a station even after
They are fully charged.
It is only possible to drive directly between select pairs of stations. Variations in trafc conditions,
Road conditions, availability of HOV lanes, Etc., result in different travel times along each route de-
Pending upon the time of day at which travel along that route begins. All roads are two-way, and
Prevailing conditions affect travel in both directions ctions.
The winner is the first car to reach checkpoint n? 1, starting form checkpoint 0. Other than
Starting and ending conditions, cars may pass through the stations in any order, and need not visit all
Stations to complete the course.
Write a program to determine the earliest time, expressed as the total number of minutes elapsed
Since the start of the rally, at which a car cocould reach the final checkpoint.
Input
There will be several test cases in the input. Each test case starts with a line containing N (1 ≤ n ≤500 ),
The number of stations, and M (1 ≤ m ≤ 1,000), the number of ing road segments.
This is followed by M blocks, each block describing one road segment. A road segment block has
The following structure:
Each block begins with a single line containing two integers, A and B (0 ≤ a, B ≤ n? 1,? = B ).
These numbers are the two checkpoints connected by that segment. The connections are undirected:
Segment permitting travel from Station A to Station B will also allow travel from Station B to Station.
This is followed by from one to twenty 'travel lines' describing travel times. Each of the travel lines
Contains 3 numbers: start, stop, (0 ≤ start <Stop ≤ 1,439), and t ime (0 <t ime <1,000). Start
And stop are the time of day (expressed in minutes since midnight) described by this line, and t ime
Is the travel time, in minutes, required to traverse this road segment if travel begins at any time in
Range [start... Stop], random Sive. The first travel line in a block will have a start time of 0 (midnight, or
). The final travel line in a block will have a stop time of 1439 (I. e., or 1 less than 24 hours
Times 60 minutes). Adjacent travel lines in the input will be arranged in order, and the start time
Any line after the first is one higher than the Stop Time of the preceding line. The travel lines will cover
All times from 00:00 to 23:59.
Input will end with a line with two 0 s. All test cases will describe a course that can be completed
By the cars.
Output
For each test case, output a single integer representing the smallest number of minutes needed
Complete the rally. Output no spaces, and do not separate answers with blank lines.
Sample Input
4
0 1
0 1439 100
0 2
0 1439 75
1 3
0 720 150
721 824 100
825 1000 75
1001 1439 150
2 3
0 1439 150
3 2
0 1
0 10 200
11 1439 300
1 2
0 10 200
11 1439 300
4 3
0 1
0 719 500
720 1439 240
1 2
0 964 500
965 1439 2
2 3
0 971 500
972 1439 3
0 0
Sample output
180
2360
255


Question:

Give an undirected graph, starting from noon (fully powered, supporting 240 minutes), charge at every point, charge for 2 minutes of electricity can run for 1 minute, each route is divided into several time periods by minute. The time for passing through this route in each time period is Ti, and the minimum time required to arrive at the n-1 point is asked.

Analysis:

BFS + priority queue. Take the lead by time, pay attention to the storage of Power * 2 (of course there are other methods), because if the charging time is odd, saving with int will lose 0.5. The second is to run two cycles (two days ). There are many methods to determine the weight, the more common is to open two dimensions, location and remaining power, my method is to open one-dimensional relaxation, the power of 0 (X. time-x.power ). Be careful that the time period is not divided into 1-20 according to the description of the question (this is followed by from one to twenty 'travel lines' describing travel times .), there are 24, damn illegal data. Wa has been used for two days and has become a wa excavator. It has handed in more than 70 records before a, and total submissions is only 210, I have lowered the IQ of the entire OJ.


/* * * Author : fcbruce * * Time : Sun 05 Oct 2014 06:44:35 PM CST * */#include <cstdio>#include <iostream>#include <sstream>#include <cstdlib>#include <algorithm>#include <ctime>#include <cctype>#include <cmath>#include <string>#include <cstring>#include <stack>#include <queue>#include <list>#include <vector>#include <map>#include <set>#define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _WIN32  #define lld "%I64d"#else  #define lld "%lld"#endif#define maxm 2333#define maxn 507using namespace std;struct _record{  int start[24],stop[24],time[24];  int cnt;}w[maxm];int fir[maxn];int u[maxm],v[maxm],nex[maxm];int e_max=0;int vis[maxn];bool go[24];inline int ReadInt(){  int flag=0;  int data=0;  char ch=getchar();  while (ch<'0' || ch>'9')  {    if (ch=='-') flag=1;    ch=getchar();  }  do  {    data=data*10+ch-'0';    ch=getchar();  }while (ch>='0' && ch<='9');  if (flag) data=-data;  return data;}inline void add_edge(int _u,int _v){  int &e=e_max;  e++;  u[e]=_u;v[e]=_v;  nex[e]=fir[u[e]];fir[u[e]]=e;  for (int i=0,start,stop=0,time,j=0;stop!=1439;i++)  {    start=ReadInt();    stop=ReadInt();    time=ReadInt();    w[e].start[j]=w[e+1].start[j]=start;    w[e].stop[j]=w[e+1].stop[j]=stop;    w[e].time[j]=w[e+1].time[j]=time*2;    w[e].cnt=w[e+1].cnt=++j;  }  e++;  u[e]=_v;v[e]=_u;  nex[e]=fir[u[e]];fir[u[e]]=e;}struct Heap_node{  int pos,time,power;  bool operator < (const Heap_node &_)const  {    return time>_.time;  }};priority_queue<Heap_node> q;int bfs(int s,int t,int start){  while (!q.empty()) q.pop();  memset(vis,0x3f,sizeof vis);  Heap_node iter=(Heap_node){s,start,480};  q.push(iter);    while (!q.empty())  {    Heap_node x=q.top();q.pop();    if (x.pos==t) return x.time-start;    vis[x.pos]=min(vis[x.pos],x.time-x.power);    for (int e=fir[x.pos];~e;e=nex[e])    {      if (vis[v[e]]<x.time-x.power) continue;      memset(go,0,sizeof go);      int begin;      for (int i=0;i<w[e].cnt;i++)        if (x.time% 1440>=w[e].start[i] && x.time% 1440<=w[e].stop[i])        {          begin=i;          break;        }      if (w[e].time[begin]>480) goto too_far;      if (x.power>=w[e].time[begin])      {        go[begin]=true;        iter=(Heap_node){v[e],x.time+w[e].time[begin]/2,x.power-w[e].time[begin]};        q.push(iter);      }      else      {        int charge=w[e].time[begin]-x.power;        if (x.time % 1440+charge<=w[e].stop[begin])        {          go[begin]=true;          iter=(Heap_node){v[e],x.time+charge+w[e].time[begin]/2,0};          q.push(iter);        }      }too_far:      int plus=0;      for (int j=begin+1;j<w[e].cnt*2+begin+1;j++)      {        int i=j%w[e].cnt;        if (w[e].start[i]==0) plus+=1440;        if (w[e].time[i]>480) continue;        if (go[i]) continue;        int power=x.power;        int time=w[e].start[i]+plus-x.time% 1440;        power+=time;        power=min(480,power);        if (power>=w[e].time[i])        {          go[i]=true;          iter=(Heap_node){v[e],x.time+time+w[e].time[i]/2,power-w[e].time[i]};          q.push(iter);        }        else        {          int charge=w[e].time[i]-power;          if (w[e].start[i]+charge<=w[e].stop[i])          {            go[i]=true;            iter=(Heap_node){v[e],x.time+time+charge+w[e].time[i]/2,0};            q.push(iter);          }        }      }    }  }}int main(){#ifdef FCBRUCE  freopen("/home/fcbruce/code/t","r",stdin);#endif // FCBRUCE  int n,m;  while (scanf("%d%d",&n,&m),n||m)  {    memset(fir,-1,sizeof fir);    e_max=0;    for (int i=0,u,v;i<m;i++)    {     // scanf("%d%d",&u,&v);      u=ReadInt();v=ReadInt();      add_edge(u,v);    }    printf("%d\n",bfs(0,n-1,720));  }  return 0;}


Uvalive 6485 Electric Car Rally (BFS, priority queue)

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.