SuitNdtie

osu! mapping

Apr 30th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<vector>
  3. #include<queue>
  4. #define inf 1e18+1
  5. using namespace std;
  6. typedef long long int ll;
  7. struct edge{
  8.     int v;
  9.     ll w;
  10. };
  11. struct elem{
  12.     int u;
  13.     ll dis;
  14.     ll cnt;
  15.     bool operator<(const elem& rhs)const
  16.     {
  17.     /*  if(dis == rhs.dis){
  18.             return cnt < rhs.cnt;
  19.         }*/
  20.         return dis > rhs.dis;
  21.     }
  22. };
  23. int main()
  24. {
  25.     //var
  26.     int n,m,T;
  27.     int s,d;
  28.     vector<edge> adj[10010];
  29.    
  30.     //input
  31.     scanf("%d %d %d",&n,&m,&T);
  32.     scanf("%d %d",&s,&d);
  33.     for(int i = 0 ; i < m ; i ++){
  34.         int u,v;
  35.         ll w;
  36.         scanf("%d %d %lld",&u,&v,&w);
  37.         adj[u].push_back({v,w});
  38.     }
  39.    
  40.     //dijktra
  41.     priority_queue<elem> pq;
  42.     ll dist[n+1][T];
  43.     bool visited[n+1][T];
  44.     for(int i = 0 ; i <= n ; i ++)
  45.     {
  46.         for(int j = 0 ; j < T ; j ++){
  47.             dist[i][j] = inf;
  48.             visited[i][j] = false;
  49.         }
  50.     }
  51.     dist[s][1%T] = 0;
  52.     pq.push({s,dist[s][1%T],1});
  53.    
  54.     while(!pq.empty()){
  55.         int u = pq.top().u;
  56.         int cnt = pq.top().cnt;
  57.     //  printf("Test elem (%d,%d) dist : %d\n",u,cnt%T,dist[u][cnt%T]);
  58.         pq.pop();
  59.         if(visited[u][cnt%T])continue;
  60.         visited[u][cnt%T] = true;
  61.         if(u == d && cnt % T == 0){
  62.             break;
  63.         }
  64.         for(int i = 0 ; i < adj[u].size() ; i ++){
  65.             int v = adj[u][i].v;
  66.             int w = adj[u][i].w;
  67.         //  printf("Test if %d < %d ?\n",dist[u][cnt%T] + w,dist[v][(cnt+1)%T]);
  68.             if(!visited[v][(cnt+1)%T] && dist[u][cnt%T] + w <= dist[v][(cnt+1)%T]){
  69.                 dist[v][(cnt+1)%T] = dist[u][cnt%T] + w;
  70.                 pq.push({v,dist[v][(cnt+1)%T],cnt+1});
  71.             }
  72.         }
  73.     }
  74.     printf("%lld",(dist[d][0] == inf ? -1 : dist[d][0]));
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment