I got WA in this problem this is what I do:-
1- calculate the shortest path between the brothers start and all exits using dijkstra.
1- calculate the shortest path between the police start and all exits using dijkstra.
3- for(i = 0 to e)
if (brotherDistance > policeDistance)
continue;
else
calc time the police will take then calc the speed with that time and take the min speed of all speeds.
4- if there is no speed print IMPOSSIBLE.
else
print the speed.
this is my code:
Code: Select all
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <queue>
using namespace std;
class Graph
{
public:
vector< vector< pair<int, int> > > AdjList;
int NumOfVertices;
Graph(int NOV)
{
NumOfVertices = NOV;
AdjList.resize(NumOfVertices);
}
bool InsertEdge(int x, int y, int weight, bool Directed)
{
if ( x > NumOfVertices || y > NumOfVertices )
return false;
AdjList[x].push_back(make_pair(weight, y));
if(!Directed)
InsertEdge(y, x, weight, true);
return true;
}
};
class Dijkstra
{
public:
vector<int> Distance;
Dijkstra(int NOV)
{
Distance.resize(NOV, 987654321);
}
void Go(Graph &G, int Source)
{
priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > Q;
Distance[Source] = 0;
Q.push(make_pair(0, Source));
while(!Q.empty())
{
pair<int, int> Top = Q.top();
Q.pop();
int V = Top.second;
int D = Top.first;
if(D <= Distance[V])
{
int sz = G.AdjList[V].size();
for(int i = 0 ; i < sz ; i++)
{
int V2 = G.AdjList[V][i].second;
int D2 = G.AdjList[V][i].first;
if(Distance[V2] > Distance[V] + D2)
{
Distance[V2] = Distance[V] + D2;
Q.push(make_pair(Distance[V2], V2));
}
}
}
}
}
};
int main()
{
int Cases;
int n, m, e, a, b, w, bs, ps;
scanf("%d", &Cases);
while(Cases--)
{
scanf("%d %d %d", &n, &m, &e);
Graph G(n);
for(int i = 0 ; i < m ; i++)
{
scanf("%d %d %d", &a, &b, &w);
G.InsertEdge(a-1, b-1, w, false);
}
vector<int> exits(e);
for(int i = 0 ; i < e ; i++)
scanf("%d", &exits[i]);
scanf("%d %d", &bs, &ps);
int bdist;
int pdist;
Dijkstra D1(n), D2(n);
D1.Go(G, bs - 1);
D2.Go(G, ps - 1);
bool flag = false;
double Speed = INT_MAX;
for(int i = 0 ; i < e ; i++)
{
bdist = D1.Distance[exits[i]-1];
pdist = D2.Distance[exits[i]-1];
if(bdist > pdist)
{
continue;
}
else
{
double T = double(pdist) / 160;
double S = double(bdist) / T;
Speed = min(S, Speed);
flag = true;
}
}
if(flag)
cout << fixed << setprecision(7) << Speed << endl;
else
printf("IMPOSSIBLE\n");
}
return 0;
}
thanks in advance.