Nur_Mohammed_Mehedy

Not The Best Lightoj

May 19th, 2021 (edited)
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. #pragma GCC target("avx,avx2,fma")
  3. #pragma GCC optimization ("unroll-loops")
  4.  
  5. #include<bits/stdc++.h>
  6. #include<string.h>
  7. using namespace std;
  8. #define pb          push_back
  9. #define all(v)      v.begin(),v.end()
  10. #define ya          cout<<"YES"<<endl;
  11. #define no          cout<<"NO"<<endl;
  12. #define ff          first
  13. #define sc          second
  14. #define inf         999999999
  15. #define pi          3.14159265359
  16. #define printv(v)   for(auto x:v)cout<<x<<' ';cout<<endl;
  17. #define takev(v)    for(auto &x:v)cin>>x;
  18. inline  int         random(int a=1,int b=10)
  19. {
  20.     return a+rand()%(b-a+1);
  21. }
  22. typedef long long ll;
  23. inline ll           lcm(ll a,ll b)
  24. {
  25.     return (a*b)/__gcd(a,b);
  26. }
  27. //#define see(x)  cout<<endl<<#x<<" : "<<(x)<<endl;
  28. #define see(args...) \
  29. { \
  30.     string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  31.     stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);\
  32. }
  33. void err(istream_iterator<string> it) {}
  34. template<typename T, typename... Args>
  35. void err(istream_iterator<string> it, T a, Args... args)
  36. {
  37.     cout<< *it << " = " << a <<",\n"[++it==istream_iterator<string>()];
  38.     err(it, args...);
  39. }
  40. #define scc(n) scanf("%d",&n);
  41. #define sccc(n) scanf("%lld",&n);
  42.  
  43. typedef pair<ll,ll> pll;
  44. typedef pair<int,int> pii;
  45. const int N=5009,mod=998244353;
  46.  
  47. int d[N][N];
  48. int n,r;
  49. vector< pair<int,int> >G[N];
  50.  
  51.  
  52. void shortest_length(int source)
  53. {
  54.     d[source][source] = 0;
  55.     priority_queue< pii > q;
  56.     q.push({0,source});
  57.  
  58.     while(!q.empty())
  59.     {
  60.         int a = q.top().sc;
  61.  
  62.         q.pop();
  63.  
  64.         for(auto [b,length]:G[a])
  65.         {
  66.             if(d[source][a] + length < d[source][b])
  67.             {
  68.                 d[source][b] = d[source][a] + length;
  69.                 q.push({-d[source][b],b});
  70.             }
  71.         }
  72.     }
  73. }
  74. int main()
  75. {
  76. //    ios::sync_with_stdio(0);
  77. //    cin.tie(NULL);
  78.  
  79.  
  80.     int _,t=0;
  81.  
  82.     scc(_);
  83.  
  84.     while(++t <= _)
  85.     {
  86.         scc(n)
  87.         scc(r)
  88.  
  89.         for(int i=1;i<=n;i++)G[i].clear();
  90.  
  91.         int ans = INT_MAX;
  92.        
  93.         for(int i=0;i<r;i++)
  94.         {
  95.             int x,y,c;
  96.             scc(x)
  97.             scc(y)
  98.             scc(c)
  99.             G[x].pb({y,c});
  100.             G[y].pb({x,c});
  101.         }
  102.  
  103.         fill(d[1],d[1]+N-1,inf);
  104.         fill(d[n],d[n]+N-1,inf);
  105.  
  106.         shortest_length(1);
  107.         shortest_length(n);
  108.  
  109.         int best = d[1][n];
  110.  
  111.         for(int a=1;a<=n;a++)
  112.         {
  113.             for(auto [b,l]:G[a])
  114.             {
  115.                 if(d[1][a] + d[n][b] + l > best)
  116.                     ans = min(ans,d[1][a] + d[n][b] + l);
  117.                
  118.             }
  119.         }
  120.  
  121.         printf("Case %d: %d\n",t,ans);
  122.     }
  123.     return 0;
  124. }
  125.  
  126.  
  127.  
Add Comment
Please, Sign In to add comment