Because Vladimir is a vampire Vladimir has never had any pro
Solution
#include <iostream>
#include <queue>
#include <map>
using namespace std;
#define MAXN 100
#define EARLIER 18
#define LATER 6
#define HOURS 24
#define NO_ROUTE (-1)
class state
{
public:
int city, time, litres;
bool operator<(const state ¤t) const
{
return litres > current.litres;
}
};
class route
{
public:
int departure;
int arrival;
int to;
};
vector < route > edges[MAXN + 1];
int travel(int from, int to)
{
priority_queue < state > states;
states.push((state){from, EARLIER, 0});
while (!states.empty())
{
state current = states.top();
cout << current.city << \" \" << current.time << \" \" << current.litres << endl;
states.pop();
if (current.city == to)
return current.litres;
for (int r = 0; r < edges[current.city].size(); r++)
{
int used = current.litres;
if (current.time > edges[current.city][r].departure)
used++;
states.push((state){edges[current.city][r].to,
edges[current.city][r].arrival, used});
}
}
return NO_ROUTE;
}
int main(int ac, char *av[])
{
int test, routes, cases = 1;
int from, to;
string start, destination;
int departure, traveling;
cin >> test;
while (test--)
{
map < string, int > cities;
for (int i = 0; i < MAXN + 1; i++)
edges[i].clear();
cin >> routes;
for (int i = 1; i <= routes; i++)
{
cin >> start >> destination >> departure >> traveling;
if (cities.find(start) == cities.end())
{
from = cities.size();
cities[start] = from;
}
else
from = cities[start];
if (cities.find(destination) == cities.end())
{
to = cities.size();
cities[destination] = to;
}
else
to = cities[destination];
departure += (departure <= LATER ? HOURS : 0);
if (departure >= EARLIER
&& (departure + traveling) <= (HOURS + LATER))
edges[from].push_back((route){departure,
(departure + traveling), to});
}
cin >> start >> destination;
cout << \"Test Case \" << cases++ << \".\" << endl;
if (start == destination)
{
cout << \"Vladimir needs 0 litre(s) of blood.\" << endl;
continue;
}
if (cities.find(start) == cities.end()
|| cities.find(destination) == cities.end())
{
cout << \"There is no route Vladimir can take.\" << endl;
continue;
}
from = cities[start];
to = cities[destination];
int litres = travel(from, to);
if (litres == NO_ROUTE)
cout << \"There is no route Vladimir can take.\" << endl;
else
cout << \"Vladimir needs \" << litres << \" litre(s) of blood.\" << endl;
}
return 0;
}
in.txt
3
3
Ulm Muenchen 17 2
Ulm Muenchen 19 12
Ulm Muenchen 5 2
Ulm Muenchen
10
Lugoj Sibiu 12 6
Lugoj Sibiu 18 6
Lugoj Sibiu 24 5
Lugoj Medias 22 8
Lugoj Medias 18 8
Lugoj Reghin 17 4
Sibiu Reghin 19 9
Sibiu Medias 20 3
Reghin Medias 20 4
Reghin Bacau 24 6
Lugoj Bacau
6
a b 18 8
b c 20 6
c a 19 8
e f 19 20
f g 22 6
g e 23 4
a h


