Advertisement
TLE

UVA - 10525 - New to Bangladesh?

TLE
Nov 12th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define pf(x) printf("%d",x)
  6. #define pfd(x,y) printf("%d %d",x,y)
  7. #define pfl(x) printf("%ld",x)
  8. #define nl printf("\n")
  9.  
  10. #define cs(x) printf("Case %d: ",x)
  11. #define csn(x) printf("Case %d:\n",x)
  12.  
  13. #define all(x) x.begin(),x.end()
  14. #define Find(x,n) find(all(x),n)
  15. #define pi acos(-1.0)
  16. #define i64 long long
  17. #define pb(x) push_back(x)
  18. #define mset(x,v) memset(x,v,sizeof(x))
  19. #define sc(n) scanf("%d",&n)
  20. #define scl(n) scanf("%ld",&n)
  21. #define scll(n) scanf("%lld",&n)
  22. #define scd(n,m) scanf("%d %d",&n,&m)
  23. #define scdl(n,m) scanf("%ld %ld",&n,&m)
  24. #define scdll(n,m) scanf("%lld %lld",&n,&m)
  25. #define filein freopen("in.txt","r",stdin)
  26. #define fileout freopen("my.txt","w",stdout)
  27. #define FOR(i,n) for(int i=0;i<n; i++)
  28. #define inf 200000000
  29.  
  30. bool isUpper(char ch){ if( ch>='A' && ch<='Z' ) return true; return false; }
  31. bool isLower(char ch){ if( ch>='a' && ch<='z') return true; return false;}
  32. bool isLetter(char ch){ if( ch>='A' && ch<='Z' || ch>='a' && ch<='z') return true; return false; }
  33. bool isDigit(char ch){ if( ch>='0' && ch<='9') return true; return false; }
  34. char toLower(char ch){ if (isUpper(ch)) return (ch+32); return ch; }
  35. char toUpper(char ch){ if (isLower(ch)) return (ch-32); return ch; }
  36.  
  37. template<class T>bool isEven(T a){ return (a%2==0);}
  38. template<class T>T sq(T a){ return a*a; }
  39. template<class T>T gcd(T a,T b){ return b==0 ? a : gcd(b,a%b); }
  40. template<class T>T lcm(T a,T b){ return (a/gcd(a,b))*b; }
  41. template<class T>bool isPrime(T n){ for(T i=2; i*i<=n; i++){ if(n%i==0) return false; } return true; }
  42. template<class T>T Pow(T n,T p) { T res=n; for(T i=1;i<p; i++){ res *= n; } return res; }
  43. template<class T>T Max(T n,T p) { if(n>=p) return n; return p; }
  44.  
  45. i64 g[500][500];
  46. i64 times[500][500];
  47.  
  48. void floyd_warshall(int n){
  49.     for(int k=1;k<=n; k++)
  50.         for(int i=1;i<=n; i++)
  51.             for(int j=1;j<=n; j++)
  52.                 if( times[i][j]>times[i][k]+times[k][j] )
  53.                         g[i][j]=g[i][k]+g[k][j],times[i][j]=times[i][k]+times[k][j];
  54.                 else if( (times[i][j]==times[i][k]+times[k][j]) && (g[i][j]>g[i][k]+g[k][j]) )
  55.                         g[i][j]=(g[i][k]+g[k][j]),times[i][j]=times[i][k]+times[k][j];
  56.  
  57. }
  58.  
  59. void Set(int n){
  60.     for(int i=1;i<=n; i++)
  61.         for(int j=1;j<=n; j++)
  62.             g[i][j]=inf,times[i][j]=inf;
  63. }
  64.  
  65. int main()
  66. {
  67.     //filein;
  68.  
  69.     int t,T=0;
  70.     int n,m,query;
  71.     sc(t);
  72.     while( t-- ){
  73.         scanf("%d %d",&n,&m);
  74.         Set(n);
  75.         for(int i=1;i<=m;i++){
  76.             int u,v;
  77.             i64 w,tim;
  78.             scanf("%d %d %lld %lld",&u,&v,&tim,&w);
  79.             if( u== v) continue;
  80.             if( tim<times[u][v] )
  81.                 times[u][v]=tim,times[v][u]=tim,g[u][v]=w,g[v][u]=w;
  82.             else if(  tim==times[u][v] && g[u][v]>w  )
  83.                 times[u][v]=tim,times[v][u]=tim,g[u][v]=w,g[v][u]=w;
  84.         }
  85.         floyd_warshall(n);
  86.         sc(query);
  87.         if(T) nl;
  88.         while(query--){
  89.             int u,v;
  90.             scanf("%d %d",&u,&v);
  91.             if( u==v )
  92.                 printf("Distance and time to reach destination is %d & %d.\n",0,0);
  93.             else if( g[u][v]==inf )
  94.                 printf("No Path.\n");
  95.             else
  96.                 printf("Distance and time to reach destination is %lld & %lld.\n",g[u][v],times[u][v]);
  97.         }
  98.         ++T;
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement