Advertisement
Guest User

loj 1281

a guest
Sep 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define ll long long
  5. #define infl 100000000000000018
  6. ll n,m,p,a,b,c,e,f,g,h,x,mn;
  7. struct edge
  8. {
  9.     ll node,cost;
  10.     ll pro;
  11.     bool operator<(const edge &p)const
  12.     {
  13.         if(cost==p.cost)return pro>p.pro;
  14.         return cost>p.cost;
  15.     }
  16. };
  17. vector<edge>v[10005];
  18. ll d[10005][12];
  19. //ll pr[10005];
  20. void dijkstra()
  21. {
  22.     for(ll i=0;i<n;++i)
  23.     {
  24.         for(ll j=0;j<=10;j++)
  25.         {
  26.             d[i][j]=infl;
  27.         }
  28.     }
  29.     d[0][0]=0;
  30.     priority_queue<edge>q;
  31.     edge u,w;
  32.     u={0,0,0};
  33.     q.push(u);
  34.     while(!q.empty())
  35.     {
  36.         u=q.top();
  37.         q.pop();
  38.         a=u.node;
  39.         b=u.cost;
  40.         c=u.pro;
  41.         for(ll i=0;i<v[a].size();++i)
  42.         {
  43.             f=v[a][i].node;
  44.             g=v[a][i].cost;
  45.             h=v[a][i].pro;
  46.             x=c+h;
  47.             if((d[a][c] + g <d[f][x]) && x<=e)
  48.             {
  49.                 d[f][x]=d[a][c]+g;
  50.  
  51.                 w={f,d[f][x],x};
  52.                 q.push(w);
  53.             }
  54.         }
  55.     }
  56.     mn=infl;
  57.     for(ll i=0;i<=e;++i){
  58.         //cout<<d[n-1][i]<<" ";
  59.         mn=min(d[n-1][i],mn);
  60.  
  61.     }
  62. }
  63. int main()
  64. {
  65.     ll t,tc;
  66.     scanf("%lld",&t);
  67.     for(tc=1;tc<=t;++tc)
  68.     {
  69.         scanf("%lld %lld %lld %lld",&n,&m,&p,&e);
  70.         for(ll i=0;i<m;++i){
  71.             scanf("%lld %lld %lld",&a,&b,&c);
  72.             edge temp;
  73.             temp={b,c,0};
  74.             v[a].pb(temp);
  75.             //temp={b,c,0};
  76.             //v[a].pb(temp);
  77.  
  78.         }
  79.         for(ll i=0;i<p;++i){
  80.             scanf("%lld %lld %lld",&a,&b,&c);
  81.             edge temp;
  82.             //temp={a,c,1};
  83.             //v[b].pb(temp);
  84.             temp={b,c,1};
  85.             v[a].pb(temp);
  86.  
  87.         }
  88.         dijkstra();
  89.         printf("Case %d: ",tc);
  90.         if(mn==infl)printf("Impossible\n");
  91.         else printf("%lld\n",mn);
  92.         for(ll i=0;i<n;++i)v[i].clear();
  93.     }
  94.     return  0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement