Problem Link
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
const long INF = 9999999;
void dijkstra(int source, long dist[], vector< pair<int, long> > maze[])
{
priority_queue< pair<long, int> > pq;
dist[source] = 0;
pq.push(make_pair(0, source));
int neighbor;
long t;
while(!pq.empty())
{
int s = pq.top().second;
pq.pop();
for(int i = 0; i < maze[s].size(); i++)
{
neighbor = maze[s][i].first;
t = maze[s][i].second;
if(dist[neighbor] > dist[s] + t)
{
dist[neighbor] = dist[s] + t;
pq.push(make_pair(dist[neighbor], neighbor));
}
}
}
}
int main()
{
int n, exitCell, timer, edges, u, v, t;
int result = 0;
cin >> n >> exitCell >> timer >> edges;
vector< pair<int, long> > maze[n + 1];
long dist[n + 1];
for(int i = 0; i < edges; i++)
{
cin >> u >> v >> t;
maze[v].push_back(make_pair(u, t));
}
for(int i = 0; i < n + 1; i++)
{
dist[i] = INF;
}
dijkstra(exitCell, dist, maze);
for(int i = 1; i <= n; i++)
{
if(dist[i] <= timer)
{
result++;
}
}
cout << result << endl;
}
Comments
Post a Comment