Advertisement
TLE

UVA - 10436 - Cheapest way

TLE
Nov 12th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 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 20000000.0
  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. float g[23][23];
  46. int next[23][23];
  47. map<int,string>idint;
  48. map<string,int>idst;
  49. float paid[23];
  50.  
  51. void floyd_warshall(int n){
  52.     for(int k=1;k<=n; k++)
  53.         for(int i=1;i<=n; i++)
  54.             for(int j=1;j<=n; j++)
  55.                 if( g[i][j]>(g[i][k]+g[k][j]) )
  56.                     g[i][j]=g[i][k]+g[k][j],next[i][j]=next[i][k];
  57.  
  58. }
  59.  
  60. void Set(int n){
  61.     for(int i=1;i<=n; i++)
  62.         for(int j=1;j<=n; j++)
  63.             g[i][j]=inf,next[i][j]=j;
  64. }
  65.  
  66. void Setpay(int n){
  67.     for(int i=1;i<=n; i++)
  68.         for(int j=1;j<=n; j++)
  69.             g[i][j] += paid[j];
  70. }
  71.  
  72. void printPath(int u,int v){
  73.     printf("%s",idint[u].c_str());
  74.     while(u!=v){
  75.         u = next[u][v];
  76.         printf(" %s",idint[u].c_str());
  77.     }
  78.     nl;
  79. }
  80.  
  81. int main()
  82. {
  83.     //filein;
  84.  
  85.     int T=0,t;
  86.     sc(t);
  87.     while( t-- ){
  88.         int nStation,nPath,nQuery;
  89.         sc(nStation);
  90.         Set(nStation);
  91.         for(int i=1;i<=nStation;i++){
  92.             cin >> idint[i];
  93.             scanf("%f",&paid[i]);
  94.             idst[idint[i]]=i;
  95.         }
  96.         sc(nPath);
  97.         while(nPath--){
  98.             string u,v;
  99.             float w;
  100.             cin >> u >> v;
  101.             scanf("%f",&w);
  102.             g[idst[u]][idst[v]] = min( g[idst[u]][idst[v]], w*2 );
  103.             g[idst[v]][idst[u]] = min( g[idst[u]][idst[v]], w*2 );
  104.         }
  105.         Setpay(nStation);
  106.         floyd_warshall(nStation);
  107.         sc(nQuery);
  108.         if(T) nl;
  109.         printf("Map #%d\n",++T);
  110.         int Query=0;
  111.         while(nQuery--){
  112.             string U,V;
  113.             float w;
  114.             cin >> U >> V;
  115.             scanf("%f",&w);
  116.             float r=g[idst[U]][idst[V]]+paid[idst[U]];
  117.             r += (r*10)/100;
  118.             r /= w;
  119.             printf("Query #%d\n",++Query);
  120.             printPath(idst[U],idst[V]);
  121.             printf("Each passenger has to pay : %.2f taka\n",r);
  122.         }
  123.         idint.clear();
  124.         idst.clear();
  125.     }
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement