Artifact Content
Not logged in

Artifact fb6029affb2ad4b991c4cf2f760b3f604c0dd46b


#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>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const int INF = 0x3fffffff;

typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;

bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] )
{
	for(int i=0; i<G[v].size(); ++i) {
		vert u = G[v][i];
		if( visited[u] ) continue;
		visited[u] = true;

		if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) )
			{ matchTo[v]=u, matchTo[u]=v; return true; }
	}
	return false;
}

template<int NV>
int biMatch( graph& G, int L ) // [0,L):left, [L,?):right
    // only left->right edges are used during computation
{
	vector<vert> matchTo(G.size(), -1);
	int ans = 0;
	for(vert v=0; v<L; ++v) {
		bool visited[NV] = {};
		if( augment(G, v, matchTo, visited) )
			++ans;
	}
	return ans;
}

class SafeReturn { public:
	int minRisk(int N, vector <string> streets)
	{
		int T = streets.size();
		vector< vector<int> > g(T, vector<int>(T, INF));
		for(int i=0; i<T; ++i)
			for(int j=0; j<T; ++j)
				if( i == j )
					g[i][j] = 0;
				else if( streets[i][j] != '-' )
					g[i][j] = streets[i][j] - '0';
		vector< vector<int> > d = g;
		for(int k=0; k<T; ++k)
			for(int i=0; i<T; ++i)
				for(int j=0; j<T; ++j)
					d[i][j] = min(d[i][j], d[i][k]+d[k][j]);

		graph G(2*(N+1));
		for(int x=0; x<=N; ++x)
		for(int y=0; y<=N; ++y) if( x != y )
		{
			if( d[0][x] + d[x][y] == d[0][y] )
			{
				G[x].push_back(N+1+y);
			}
		}
		return N+1 - biMatch<256>(G, N+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(_, SafeReturn().minRisk(N, streets));}
int main(){

CASE(0)
	int N = 3; 
	string streets_[] = {"-234",
 "2---",
 "3---",
 "4---"};
	  vector <string> streets(streets_, streets_+sizeof(streets_)/sizeof(*streets_)); 
	int _ = 3; 
END
CASE(1)
	int N = 2; 
	string streets_[] = {"-12",
 "1-1",
 "21-"};
	  vector <string> streets(streets_, streets_+sizeof(streets_)/sizeof(*streets_)); 
	int _ = 1; 
END
CASE(2)
	int N = 3; 
	string streets_[] = {"-----7",
 "--1---",
 "-1-5--",
 "--5-1-",
 "---1-3",
 "7---3-"};
	  vector <string> streets(streets_, streets_+sizeof(streets_)/sizeof(*streets_)); 
	int _ = 1; 
END
CASE(3)
	int N = 2; 
	string streets_[] = {"-11",
 "1-1",
 "11-"};
	  vector <string> streets(streets_, streets_+sizeof(streets_)/sizeof(*streets_)); 
	int _ = 2; 
END
/*
CASE(4)
	int N = ; 
	string streets_[] = ;
	  vector <string> streets(streets_, streets_+sizeof(streets_)/sizeof(*streets_)); 
	int _ = ; 
END
CASE(5)
	int N = ; 
	string streets_[] = ;
	  vector <string> streets(streets_, streets_+sizeof(streets_)/sizeof(*streets_)); 
	int _ = ; 
END
*/
}
// END CUT HERE