Artifact 4413ce4dac004874adfd74c74493d759965bf2c4
#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;
vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector<double>& V )
{
vector<double> A(V);
for(int i=0; i<n; ++i)
{
// pivot
if( M[i][i] == 0 )
for(int j=i+1; j<n; ++j)
if( M[j][i] != 0 )
{swap(M[i], M[j]); swap(A[i], A[j]); break;}
if( M[i][i] == 0 )
throw "no anser";
// M[i][i] <-- 1
double p = M[i][i];
for(int j=i; j<n; ++j)
M[i][j] /= p;
A[i] /= p;
// M[*][i] <-- 0
for(int j=0; j<n; ++j) if(j!=i)
{
double r = M[j][i];
for(int k=i; k<n; ++k)
M[j][k] -= M[i][k] * r;
A[j] -= A[i] * r;
}
}
// consistency check
for(int i=n; i<M.size(); ++i) {
double s = 0.0;
for(int j=0; j<n; ++j)
s += M[i][j] * A[j];
if( abs(s-A[i]) > 1e-4 )
throw "no answer";
}
return A;
}
class LongStraightRoad
{
public:
int distanceToDestination(vector <string> signs, string destination)
{
typedef map< string, vector< pair<double,int> > > MSV;
MSV data;
for(int i=0; i<signs.size(); ++i)
{
stringstream sin(signs[i]);
while(sin) {
string n; int d; sin>>n>>d;
char c; sin>>c; // ';'
data[n].push_back( make_pair(double(d), i) );
}
}
if( data[destination].empty() )
return -1;
vector< vector<double> > M;
vector<double> V;
for(MSV::iterator it=data.begin(); it!=data.end(); ++it)
{
vector< pair<double,int> >& d = it->second;
for(int i=0; i+1<d.size(); ++i)
{
if( d[i].first <= d[i+1].first )
return -1;
// S[d[i].second] - S[d[i+1].second] == d[i+1].first - d[i].first
vector<double> Mk(signs.size());
Mk[d[i].second] = 1;
Mk[d[i+1].second] = -1;
M.push_back(Mk);
V.push_back( d[i+1].first - d[i].first );
}
}
// S[last] = 0.0
vector<double> Mk(signs.size());
Mk[signs.size()-1] = 1;
M.push_back(Mk);
V.push_back(0);
if( signs.size() > M.size() )
return -1;
try {
vector<double> S = solve_linear_eq( signs.size(), M, V );
for(int i=0; i+1<S.size(); ++i)
if( S[i] > S[i+1] )
return -1;
double result =
S[ data[destination][0].second ] + data[destination][0].first;
int rI = (int)floor(result+0.5);
if( abs(result-rI) > 1e-4 )
return -1;
return ( rI < 0 ? -1 : rI );
} catch(const char*) {
return -1;
}
}
// 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