Advertisement
kcku

01112 - Mice and Maze

Jul 29th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. struct Data{ int p, d; } arr[101][100];
  7.  
  8. bool operator<(const Data &a, const Data &b)
  9. {
  10.     return a.d>b.d;
  11. }
  12. int main()
  13. {
  14.     int cases;
  15.     scanf("%d", &cases);
  16.  
  17.     for(int n, e, t, m; cases-- && scanf("%d%d%d%d", &n, &e, &t, &m);)
  18.     {
  19.         int len[101]={}, dist[101], ans=0;
  20.         memset(dist, 127, sizeof(dist));
  21.         priority_queue<Data> q;
  22.         q.push({e, dist[e]=0});
  23.  
  24.         for(int i=0, a, b, c; i<m; i++)
  25.             scanf("%d%d%d", &a, &b, &c), arr[b][len[b]++]={a, c};
  26.  
  27.         while(!q.empty())
  28.         {
  29.             Data a=q.top();
  30.             q.pop();
  31.  
  32.             if(dist[a.p]==a.d)
  33.                 for(int i=0; i<len[a.p]; i++)
  34.                 {
  35.                     Data b=arr[a.p][i];
  36.  
  37.                     if(dist[b.p]>a.d+b.d)
  38.                         q.push({b.p, dist[b.p]=a.d+b.d});
  39.                 }
  40.         }
  41.         for(int i=1; i<=n; i++)
  42.             ans+=dist[i]<=t;
  43.  
  44.         printf("%d\n", ans);
  45.         if(cases) puts("");
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement