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: 4fd800b3a8 2011-02-23 kinaba: class LongStraightRoad 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: typedef pair<int,int> edge; // (index,dist) 4fd800b3a8 2011-02-23 kinaba: int distanceToDestination(vector<string> signs, string destination) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int n = signs.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // parse 4fd800b3a8 2011-02-23 kinaba: vector< map<string, int> > data(n); 4fd800b3a8 2011-02-23 kinaba: map<string, vector<edge> > n2d; // name -> (index, dist) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++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[i][n] = d; 4fd800b3a8 2011-02-23 kinaba: n2d[n].push_back( edge(i,d) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // construct the graph 4fd800b3a8 2011-02-23 kinaba: vector< vector<edge> > G(n); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(map<string,int>::iterator it=data[i].begin(); it!=data[i].end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<edge>& es = n2d[it->first]; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<es.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( es[j].first != i ) 4fd800b3a8 2011-02-23 kinaba: G[i].push_back( edge(es[j].first, it->second - es[j].second) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: try { 4fd800b3a8 2011-02-23 kinaba: vector<bool> done(n, false); 4fd800b3a8 2011-02-23 kinaba: vector<int> p(n); 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<bool> visited(n, false); 4fd800b3a8 2011-02-23 kinaba: dfs(n-1, 0, G, visited, p); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> kn; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: if( visited[i] ) { 4fd800b3a8 2011-02-23 kinaba: kn.push_back( p[i] ); 4fd800b3a8 2011-02-23 kinaba: done[i] = true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( !sorted(kn) ) { 4fd800b3a8 2011-02-23 kinaba: cerr << "ns1" << endl; 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: vector<edge>& es = n2d[destination]; 4fd800b3a8 2011-02-23 kinaba: int result = p[es[0].first] + es[0].second; 4fd800b3a8 2011-02-23 kinaba: if( result < 0 ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<n; ++v) 4fd800b3a8 2011-02-23 kinaba: if( !done[v] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<bool> visited(n, false); 4fd800b3a8 2011-02-23 kinaba: dfs(v, 0, G, visited, p); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> kn; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: if( visited[i] ) { 4fd800b3a8 2011-02-23 kinaba: kn.push_back( p[i] ); 4fd800b3a8 2011-02-23 kinaba: done[i] = true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( !sorted(kn) ) { 4fd800b3a8 2011-02-23 kinaba: cerr << "ns@" << v << endl; 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return result; 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: bool sorted( vector<int>& kn ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i+1<kn.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( kn[i] >= kn[i+1] ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void dfs(int v, int d, vector< vector<edge> >& G, vector<bool>& visited, vector<int>& p) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( visited[v] ) { 4fd800b3a8 2011-02-23 kinaba: if( d != p[v] ) { 4fd800b3a8 2011-02-23 kinaba: cerr << d << " " << v << " " << p[v] << endl; 4fd800b3a8 2011-02-23 kinaba: throw "bad"; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: visited[v] = true; 4fd800b3a8 2011-02-23 kinaba: p[v] = d; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<G[v].size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: cerr << v << "->" << G[v][i].first << " : " << G[v][i].second << endl; 4fd800b3a8 2011-02-23 kinaba: dfs( G[v][i].first, d+G[v][i].second, G, visited, p ); 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