chillurbrain

6. Метро

May 22nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <map>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. struct result{
  11.     int a,b;
  12.    
  13.     result(){}
  14.    
  15.     result(int _a, int _b){
  16.         a = _a; b = _b;
  17.     }
  18.    
  19.     result operator + (result X)const{
  20.         return result(a+X.a,b+X.b);
  21.     }
  22.    
  23.     double toDouble(){
  24.         return a+b*sqrt(2.0);
  25.     }
  26.    
  27.     result min(result X){
  28.         if((*this).toDouble()<X.toDouble()) return (*this);
  29.         return X;
  30.     }
  31. };
  32.  
  33. result dp[2][1001];
  34. set< pair<int, int> > S;
  35.  
  36. int main(){
  37.     int N,M,K;
  38.     scanf("%d %d %d",&N,&M,&K);
  39.    
  40.     for(int i = 0,r,c;i<K;++i){
  41.         scanf("%d %d",&c,&r);
  42.         S.insert(make_pair(r,c));
  43.     }
  44.    
  45.     for(int c = 0;c<=N;++c) dp[0][c] = result(c,0);
  46.    
  47.     for(int r = 1;r<=M;++r){
  48.         dp[r & 1][0] = result(r,0);
  49.        
  50.         for(int c = 1;c<=N;++c){
  51.             dp[r & 1][c] = result(1,0)+dp[(r & 1) ^ 1][c].min(dp[r & 1][c-1]);
  52.            
  53.             if(S.find(make_pair(r,c))!=S.end())
  54.                 dp[r & 1][c] = dp[r & 1][c].min(dp[(r & 1) ^ 1][c-1]+result(0,1));
  55.         }
  56.     }
  57.    
  58.     printf("%.0lf\n",100.0*dp[M & 1][N].toDouble());
  59.    
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment