Artifact Content
Not logged in

Artifact 3cd3d111843830ab7d7b3e37908beaa1f8773ac0


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;


class LongStraightRoad
{
public:
	typedef pair<int,int> edge; // (index,dist)
	int distanceToDestination(vector<string> signs, string destination) 
	{
		int n = signs.size();

		// parse
		vector< map<string, int> > data(n);
		map<string, vector<edge> > n2d; // name -> (index, dist)
		for(int i=0; i<n; ++i)
		{
			stringstream sin(signs[i]);
			while(sin) {
				string n; int d; sin>>n>>d;
				char c; sin>>c; // ';'
				data[i][n] = d;
				n2d[n].push_back( edge(i,d) );
			}
		}

		// construct the graph
		vector< vector<edge> > G(n);
		for(int i=0; i<n; ++i)
		{
			for(map<string,int>::iterator it=data[i].begin(); it!=data[i].end(); ++it)
			{
				vector<edge>& es = n2d[it->first];
				for(int j=0; j<es.size(); ++j)
					if( es[j].first != i )
						G[i].push_back( edge(es[j].first, it->second - es[j].second) );
			}
		}

		try {
			vector<bool> done(n, false);
			vector<int>     p(n);
			{
				vector<bool> visited(n, false);
				dfs(n-1, 0, G, visited, p);

				vector<int> kn;
				for(int i=0; i<n; ++i)
					if( visited[i] ) {
						kn.push_back( p[i] );
						done[i] = true;
					}
				if( !sorted(kn) ) {
cerr << "ns1" << endl;
					return -1;
}
			}
			vector<edge>& es = n2d[destination];
			int result = p[es[0].first] + es[0].second;
			if( result < 0 )
				return -1;

			for(int v=0; v<n; ++v)
				if( !done[v] )
				{
					vector<bool> visited(n, false);
					dfs(v, 0, G, visited, p);

					vector<int> kn;
					for(int i=0; i<n; ++i)
						if( visited[i] ) {
							kn.push_back( p[i] );
							done[i] = true;
						}
					if( !sorted(kn) ) {
cerr << "ns@" << v << endl;
						return -1;
}
				}
			return result;
		} catch (const char*) {
			return -1;
		}
	}

	bool sorted( vector<int>& kn )
	{
		for(int i=0; i+1<kn.size(); ++i)
			if( kn[i] >= kn[i+1] )
				return false;
		return true;
	}

	void dfs(int v, int d, vector< vector<edge> >& G, vector<bool>& visited, vector<int>& p)
	{
		if( visited[v] ) {
			if( d != p[v] ) {
cerr << d << " " << v << " " << p[v] << endl;
				throw "bad";
}
			return;
		}
		visited[v] = true;
		p[v]       = d;
		for(int i=0; i<G[v].size(); ++i) {
cerr << v << "->" << G[v][i].first << " : " << G[v][i].second << endl;
			dfs( G[v][i].first, d+G[v][i].second, G, visited, p );
}
	}

// BEGIN CUT HERE
	public:
	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(); }
	private:
	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(); }
	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; } }
	void test_case_0() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13"
,"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)); }
	void test_case_1() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLBOROUGH 13"
,"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)); }
	void test_case_2() { string Arr0[] = {"A 25;B 15"
,"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)); }
	void test_case_3() { string Arr0[] = {"YVO 60;J 62"
,"K 45"
,"K 40;MV 17"
,"K 37;YVO 44;HY 48;CC 69;D 77;YXF 80"
,"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)); }
	void test_case_4() { string Arr0[] = {"A 200;B 150"
,"C 45;D 100;E 150"
,"C 25;E 130"
,"F 80;G 65"
,"G 35;H 160"
,"A 160"
,"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)); }

// END CUT HERE
};
// BEGIN CUT HERE 
int main() {
	LongStraightRoad().run_test(-1);
}
// END CUT HERE