Artifact Content
Not logged in

Artifact d7e39504fab9386900d126939d76f8ff7c652e5a


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

class SnowPlow { public:
	int solve(vector <string> roads) 
	{
		int N = roads.size();

		set< pair<int,int> > edges;
		int sum = 0;
		for(int i=0; i<N; ++i)
			for(int j=i+1; j<N; ++j)
				if( roads[i][j] > '0' ) {
					edges.insert( make_pair(i,j) );
					sum += 2*(roads[i][j]-'0');
				}

		set<int> vis;
		queue<int> q; q.push(0);
		while( !q.empty() ) {
			int v = q.front();
			q.pop();
			for(int j=0; j<N; ++j) if( j!=v )
				if( roads[v][j]>'0' ) {
					edges.erase( make_pair(min(j,v), max(j,v)) );
					if( !vis.count(j) ) {
						q.push(j);
						vis.insert(j);
					}
				}
		}
		return edges.empty() ? sum : -1;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, SnowPlow().solve(roads));}
int main(){

CASE(0)
	string roads_[] = {"010000",
 "101000",
 "010100",
 "001010",
 "000101",
 "000010"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = 10; 
END
CASE(1)
	string roads_[] = {"010000",
 "101000",
 "010100",
 "001020",
 "000201",
 "000010"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = 12; 
END
CASE(2)
	string roads_[] = {"031415",
 "300000",
 "100000",
 "400000",
 "100000",
 "500000"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = 28; 
END
CASE(3)
	string roads_[] = {"0100",
 "1000",
 "0001",
 "0010"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = -1; 
END
CASE(4)
	string roads_[] = {"0101",
 "1001",
 "0000",
 "1100"};
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = 6; 
END
/*
CASE(5)
	string roads_[] = ;
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = ; 
END
CASE(6)
	string roads_[] = ;
	  vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); 
	int _ = ; 
END
*/
}
// END CUT HERE