Hi all,
I have been gettin TLE in this problem for quite a long time now.
My solution is as follows:
1. I have created a graph with Cities as Vertices and Train Routes as edges.
2. I maintain an array bestDepTime[][]
bestDepTime
[j] contains "If city(i) can be reached in time(j) then the best i.e. the latest Departure Time possible"
To me the complexity can be at best the size of the array bestDepTime .. I absolutely have no idea why I should get TLE I am ready to get WA though.
Hope someone can provide with an IO or reason for my TLE... thnx in advance
Code: Select all
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
const int cmax = 105;
const int tmax = 24 * 60 + 5;
using namespace std;
struct Route
{
int arrTime; //Arrival Time in dest city
int depTime; //Departure from the current city
int dest; //Destination
bool operator<(const Route &ob) const
{
if(depTime != ob.depTime)
return depTime > ob.depTime;
return arrTime < ob.arrTime;
}
};
vector<Route> tList[cmax];
int nRoute[cmax];
int cVisit[cmax];
int bestDepTime[cmax][tmax];
int nCity, nTrain;
int src, dest, startTime;
void generateRoute(int currCity, int arrTime, int depTime)
{
int i;
//If the current city was reached with an EARLIER i.e. BETTER arrival time
//Then the departure time for that arrival also is >= current depTime
//Hence we can ignore this case
if(cVisit[currCity] <= arrTime)
return;
cVisit[currCity] = arrTime;
//If the current city is reached with this arrival time
//then it is guaranteed that it was reached with a LATER i.e. BETTER depTime
//As we are calling the functions with a decreasing order of departure time
if(bestDepTime[currCity][arrTime] != -1)
return;
bestDepTime[currCity][arrTime] = depTime;
for(i=0; i<nRoute[currCity]; i++)
{
if(tList[currCity][i].depTime >= arrTime)
generateRoute(tList[currCity][i].dest, tList[currCity][i].arrTime, depTime);
else
break;
}
}
int main(void)
{
int i, j;
int nTest, tst;
map<string, int> cityNum;
string currCity, prevCity;
char cityName[100];
Route rt;
int frm, to, arrTime, depTime;
#ifndef ONLINE_JUDGE
freopen("d:/programs/input/10039.txt", "r", stdin);
#endif
scanf("%d", &nTest);
for(tst=1; tst<=nTest; tst++)
{
scanf("%d", &nCity);
for(i=0; i<nCity; i++)
{
scanf("%s", cityName);
cityNum[cityName] = i;
}
scanf("%d", &nTrain);
memset(nRoute, 0, sizeof(nRoute));
for(i=0; i<nTrain; i++)
{
scanf("%d", &j);
scanf("%d %s", &depTime, cityName);
prevCity = cityName;
frm = cityNum.find(prevCity)->second;
j--;
while(j--)
{
scanf("%d %s", &arrTime, cityName);
currCity = cityName;
to = cityNum.find(currCity)->second;
rt.arrTime = arrTime / 100 * 60 + arrTime % 100;
rt.depTime = depTime / 100 * 60 + depTime % 100;
rt.dest = to;
tList[frm].push_back(rt);
nRoute[frm]++;
prevCity = currCity;
depTime = arrTime;
frm = to;
}
}
scanf("%d", &startTime);
startTime = startTime / 100 * 60 + startTime % 100;
src = dest = -1;
scanf("%s", cityName);
prevCity = cityName;
src = cityNum.find(prevCity)->second;
scanf("%s", cityName);
currCity = cityName;
dest = cityNum.find(currCity)->second;
if(src < 0 || dest < 0)
{
printf("Scenario %d\n", tst);
printf("No connection\n\n");
continue;
}
for(i=0; i<nCity; i++)
sort(tList[i].begin(), tList[i].end());
//Initialization
memset(bestDepTime, -1, sizeof(bestDepTime));
memset(cVisit, 127, sizeof(cVisit));
cVisit[src] = -1;
for(i=0; i<nRoute[src]; i++)
{
if(tList[src][i].depTime >= startTime)
generateRoute(tList[src][i].dest, tList[src][i].arrTime, tList[src][i].depTime);
else
break;
}
for(i=0; i<tmax; i++)
{
if(bestDepTime[dest][i] != -1)
{
arrTime = i;
depTime = bestDepTime[dest][i];
break;
}
}
//Output
printf("Scenario %d\n", tst);
if(i >= tmax)
printf("No connection\n\n");
else
{
depTime = depTime / 60 * 100 + depTime % 60;
arrTime = arrTime / 60 * 100 + arrTime % 60;
printf("Departure %04d %s\n", depTime, prevCity.c_str());
printf("Arrival %04d %s\n\n", arrTime, currCity.c_str());
}
cityNum.clear();
for(i=0; i<nCity; i++)
tList[i].clear();
}
return 0;
}