DIV1 250pt
题意:在平面直角坐标系中,只能走到整点,每次有两种移动方法,可以沿平行于坐标轴方向走,也可以沿45度方向走,前者走一步耗时wt,后者走一步耗时st。比如从(x, y)可以走到(x+1, y),(x-1, y),(x, y+1),(x, y-1)四个点,耗时均为wt,也可以走到(x-1, y+1),(x-1, y-1),(x+1, y+1),(x+1, y-1),耗时均为st。给定x, y, wt, st,求从(0, 0)到(x, y)最少耗时多少。
解法:水题,见代码。
tag: greedy
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "StreetWalking.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 13 #include <vector> 14 #include <iostream> 15 #include <sstream> 16 #include <set> 17 #include <queue> 18 #include <fstream> 19 #include <numeric> 20 #include <iomanip> 21 #include <bitset> 22 #include <list> 23 #include <stdexcept> 24 #include <functional> 25 #include <string> 26 #include <utility> 27 #include <map> 28 #include <ctime> 29 #include <stack> 30 31 using namespace std; 32 33 #define clr0(x) memset(x, 0, sizeof(x)) 34 #define clr1(x) memset(x, -1, sizeof(x)) 35 #define pb push_back 36 #define mp make_pair 37 #define sz(v) ((int)(v).size()) 38 #define out(x) cout<<#x<<":"<<(x)<<endl 39 #define tst(a) cout<<#a<<endl 40 #define CINBEQUICKER std::ios::sync_with_stdio(false) 41 42 typedef vector<int> VI; 43 typedef vector<string> VS; 44 typedef vector<double> VD; 45 typedef long long int64; 46 47 const double eps = 1e-8; 48 const double PI = atan(1.0)*4; 49 const int inf = 2139062143 / 2; 50 51 inline int MyMod( int a , int b ) { return (a%b+b)%b;} 52 53 class StreetWalking 54 { 55 public: 56 long long minTime(int x, int y, int wt, int st){ 57 if (st >= wt*2) return ((int64)x + y) * wt; 58 59 if (x < y) swap(x, y); 60 int64 t = (int64)y * st; 61 int64 dif = x - y; 62 if (st < wt){ 63 if (dif & 1) return t + (dif-1) * st + wt; 64 return t + dif * st; 65 } 66 return t + dif * wt; 67 } 68 69 // BEGIN CUT HERE 70 public: 71 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); } 72 private: 73 template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 74 void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 75 void test_case_0() { int Arg0 = 4; int Arg1 = 2; int Arg2 = 3; int Arg3 = 10; long long Arg4 = 18LL; verify_case(0, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 76 void test_case_1() { int Arg0 = 4; int Arg1 = 2; int Arg2 = 3; int Arg3 = 5; long long Arg4 = 16LL; verify_case(1, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 77 void test_case_2() { int Arg0 = 2; int Arg1 = 0; int Arg2 = 12; int Arg3 = 10; long long Arg4 = 20LL; verify_case(2, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 78 void test_case_3() { int Arg0 = 25; int Arg1 = 18; int Arg2 = 7; int Arg3 = 11; long long Arg4 = 247LL; verify_case(3, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 79 void test_case_4() { int Arg0 = 24; int Arg1 = 16; int Arg2 = 12; int Arg3 = 10; long long Arg4 = 240LL; verify_case(4, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 80 void test_case_5() { int Arg0 = 10000000; int Arg1 = 50000000; int Arg2 = 800; int Arg3 = 901; long long Arg4 = 41010000000LL; verify_case(5, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 81 void test_case_6() { int Arg0 = 135; int Arg1 = 122; int Arg2 = 43; int Arg3 = 29; long long Arg4 = 3929LL; verify_case(6, Arg4, minTime(Arg0, Arg1, Arg2, Arg3)); } 82 83 // END CUT HERE 84 85 }; 86 //by plum rain 87 // BEGIN CUT HERE 88 int main() 89 { 90 //freopen( "a.out" , "w" , stdout ); 91 StreetWalking ___test; 92 ___test.run_test(-1); 93 return 0; 94 } 95 // END CUT HERE