4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector<double>& V ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<double> A(V); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // pivot 4fd800b3a8 2011-02-23 kinaba: if( M[i][i] == 0 ) 4fd800b3a8 2011-02-23 kinaba: for(int j=i+1; j<n; ++j) 4fd800b3a8 2011-02-23 kinaba: if( M[j][i] != 0 ) 4fd800b3a8 2011-02-23 kinaba: {swap(M[i], M[j]); swap(A[i], A[j]); break;} 4fd800b3a8 2011-02-23 kinaba: if( M[i][i] == 0 ) 4fd800b3a8 2011-02-23 kinaba: throw "no anser"; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // M[i][i] <-- 1 4fd800b3a8 2011-02-23 kinaba: double p = M[i][i]; 4fd800b3a8 2011-02-23 kinaba: for(int j=i; j<n; ++j) 4fd800b3a8 2011-02-23 kinaba: M[i][j] /= p; 4fd800b3a8 2011-02-23 kinaba: A[i] /= p; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // M[*][i] <-- 0 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<n; ++j) if(j!=i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double r = M[j][i]; 4fd800b3a8 2011-02-23 kinaba: for(int k=i; k<n; ++k) 4fd800b3a8 2011-02-23 kinaba: M[j][k] -= M[i][k] * r; 4fd800b3a8 2011-02-23 kinaba: A[j] -= A[i] * r; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // consistency check 4fd800b3a8 2011-02-23 kinaba: for(int i=n; i<M.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: double s = 0.0; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<n; ++j) 4fd800b3a8 2011-02-23 kinaba: s += M[i][j] * A[j]; 4fd800b3a8 2011-02-23 kinaba: if( abs(s-A[i]) > 1e-4 ) 4fd800b3a8 2011-02-23 kinaba: throw "no answer"; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return A; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class LongStraightRoad 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int distanceToDestination(vector <string> signs, string destination) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: typedef map< string, vector< pair<double,int> > > MSV; 4fd800b3a8 2011-02-23 kinaba: MSV data; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<signs.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(signs[i]); 4fd800b3a8 2011-02-23 kinaba: while(sin) { 4fd800b3a8 2011-02-23 kinaba: string n; int d; sin>>n>>d; 4fd800b3a8 2011-02-23 kinaba: char c; sin>>c; // ';' 4fd800b3a8 2011-02-23 kinaba: data[n].push_back( make_pair(double(d), i) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( data[destination].empty() ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<double> > M; 4fd800b3a8 2011-02-23 kinaba: vector<double> V; 4fd800b3a8 2011-02-23 kinaba: for(MSV::iterator it=data.begin(); it!=data.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< pair<double,int> >& d = it->second; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i+1<d.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( d[i].first <= d[i+1].first ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // S[d[i].second] - S[d[i+1].second] == d[i+1].first - d[i].first 4fd800b3a8 2011-02-23 kinaba: vector<double> Mk(signs.size()); 4fd800b3a8 2011-02-23 kinaba: Mk[d[i].second] = 1; 4fd800b3a8 2011-02-23 kinaba: Mk[d[i+1].second] = -1; 4fd800b3a8 2011-02-23 kinaba: M.push_back(Mk); 4fd800b3a8 2011-02-23 kinaba: V.push_back( d[i+1].first - d[i].first ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // S[last] = 0.0 4fd800b3a8 2011-02-23 kinaba: vector<double> Mk(signs.size()); 4fd800b3a8 2011-02-23 kinaba: Mk[signs.size()-1] = 1; 4fd800b3a8 2011-02-23 kinaba: M.push_back(Mk); 4fd800b3a8 2011-02-23 kinaba: V.push_back(0); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( signs.size() > M.size() ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: try { 4fd800b3a8 2011-02-23 kinaba: vector<double> S = solve_linear_eq( signs.size(), M, V ); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i+1<S.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( S[i] > S[i+1] ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: double result = 4fd800b3a8 2011-02-23 kinaba: S[ data[destination][0].second ] + data[destination][0].first; 4fd800b3a8 2011-02-23 kinaba: int rI = (int)floor(result+0.5); 4fd800b3a8 2011-02-23 kinaba: if( abs(result-rI) > 1e-4 ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: return ( rI < 0 ? -1 : rI ); 4fd800b3a8 2011-02-23 kinaba: } catch(const char*) { 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: private: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13" 4fd800b3a8 2011-02-23 kinaba: ,"MARLBOROUGH 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "GLASTONBURY"; int Arg2 = 14; verify_case(0, Arg2, distanceToDestination(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13" 4fd800b3a8 2011-02-23 kinaba: ,"GLASTONBURY 13;MARLBOROUGH 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "GLASTONBURY"; int Arg2 = -1; verify_case(1, Arg2, distanceToDestination(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { string Arr0[] = {"A 25;B 15" 4fd800b3a8 2011-02-23 kinaba: ,"A 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "B"; int Arg2 = -1; verify_case(2, Arg2, distanceToDestination(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { string Arr0[] = {"YVO 60;J 62" 4fd800b3a8 2011-02-23 kinaba: ,"K 45" 4fd800b3a8 2011-02-23 kinaba: ,"K 40;MV 17" 4fd800b3a8 2011-02-23 kinaba: ,"K 37;YVO 44;HY 48;CC 69;D 77;YXF 80" 4fd800b3a8 2011-02-23 kinaba: ,"YVO 30;B 37;RB 59"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "MV"; int Arg2 = 0; verify_case(3, Arg2, distanceToDestination(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_4() { string Arr0[] = {"A 200;B 150" 4fd800b3a8 2011-02-23 kinaba: ,"C 45;D 100;E 150" 4fd800b3a8 2011-02-23 kinaba: ,"C 25;E 130" 4fd800b3a8 2011-02-23 kinaba: ,"F 80;G 65" 4fd800b3a8 2011-02-23 kinaba: ,"G 35;H 160" 4fd800b3a8 2011-02-23 kinaba: ,"A 160" 4fd800b3a8 2011-02-23 kinaba: ,"H 130"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "F"; int Arg2 = -1; verify_case(4, Arg2, distanceToDestination(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { 4fd800b3a8 2011-02-23 kinaba: LongStraightRoad().run_test(-1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE