d171a2e25c 2015-03-17 kinaba: #include <iostream> d171a2e25c 2015-03-17 kinaba: #include <sstream> d171a2e25c 2015-03-17 kinaba: #include <iomanip> d171a2e25c 2015-03-17 kinaba: #include <vector> d171a2e25c 2015-03-17 kinaba: #include <string> d171a2e25c 2015-03-17 kinaba: #include <map> d171a2e25c 2015-03-17 kinaba: #include <set> d171a2e25c 2015-03-17 kinaba: #include <algorithm> d171a2e25c 2015-03-17 kinaba: #include <numeric> d171a2e25c 2015-03-17 kinaba: #include <iterator> d171a2e25c 2015-03-17 kinaba: #include <functional> d171a2e25c 2015-03-17 kinaba: #include <complex> d171a2e25c 2015-03-17 kinaba: #include <queue> d171a2e25c 2015-03-17 kinaba: #include <stack> d171a2e25c 2015-03-17 kinaba: #include <cmath> d171a2e25c 2015-03-17 kinaba: #include <cassert> d171a2e25c 2015-03-17 kinaba: #include <tuple> d171a2e25c 2015-03-17 kinaba: using namespace std; d171a2e25c 2015-03-17 kinaba: typedef long long LL; d171a2e25c 2015-03-17 kinaba: typedef complex<double> CMP; d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: typedef int Vert; d171a2e25c 2015-03-17 kinaba: typedef LL Cost; d171a2e25c 2015-03-17 kinaba: typedef pair<Cost,Vert> edge; d171a2e25c 2015-03-17 kinaba: typedef vector<edge> edges; d171a2e25c 2015-03-17 kinaba: typedef vector<edges> graph; d171a2e25c 2015-03-17 kinaba: const LL INF = 0x3ffffffffffff; d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: class MaliciousPath { public: d171a2e25c 2015-03-17 kinaba: long long minPath(int N, int K, vector <int> from, vector <int> to, vector <int> cost) d171a2e25c 2015-03-17 kinaba: { d171a2e25c 2015-03-17 kinaba: graph G(N), Gr(N); d171a2e25c 2015-03-17 kinaba: for(int i=0; i<from.size(); ++i) { d171a2e25c 2015-03-17 kinaba: G[from[i]].emplace_back(cost[i], to[i]); d171a2e25c 2015-03-17 kinaba: Gr[to[i]].emplace_back(cost[i], from[i]); d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: vector<Cost> d(N, INF); { d171a2e25c 2015-03-17 kinaba: priority_queue<edge, edges, greater<edge>> Q; d171a2e25c 2015-03-17 kinaba: Q.emplace(0, N-1); d171a2e25c 2015-03-17 kinaba: while(!Q.empty()) { d171a2e25c 2015-03-17 kinaba: Cost c = Q.top().first; d171a2e25c 2015-03-17 kinaba: Vert v = Q.top().second; d171a2e25c 2015-03-17 kinaba: Q.pop(); d171a2e25c 2015-03-17 kinaba: if(d[v] <= c) d171a2e25c 2015-03-17 kinaba: continue; d171a2e25c 2015-03-17 kinaba: d[v] = c; d171a2e25c 2015-03-17 kinaba: for(auto e: Gr[v]) { d171a2e25c 2015-03-17 kinaba: Cost cc = c + e.first; d171a2e25c 2015-03-17 kinaba: Vert vv = e.second; d171a2e25c 2015-03-17 kinaba: if(d[vv] == INF) d171a2e25c 2015-03-17 kinaba: Q.emplace(cc, vv); d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: if(d[0] == INF) d171a2e25c 2015-03-17 kinaba: return -1; d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: while(K --> 0) { d171a2e25c 2015-03-17 kinaba: vector<Cost> d2(N, 0); d171a2e25c 2015-03-17 kinaba: for(Vert v=0; v<N-1; ++v) { d171a2e25c 2015-03-17 kinaba: LL worst = 0; d171a2e25c 2015-03-17 kinaba: for(auto e: G[v]) d171a2e25c 2015-03-17 kinaba: worst = max(worst, e.first+d[e.second]); d171a2e25c 2015-03-17 kinaba: d2[v] = worst; d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: d.assign(N, INF); d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: priority_queue<edge, edges, greater<edge>> Q; d171a2e25c 2015-03-17 kinaba: Q.emplace(0, N-1); d171a2e25c 2015-03-17 kinaba: while(!Q.empty()) { d171a2e25c 2015-03-17 kinaba: Cost c = Q.top().first; d171a2e25c 2015-03-17 kinaba: Vert v = Q.top().second; d171a2e25c 2015-03-17 kinaba: Q.pop(); d171a2e25c 2015-03-17 kinaba: if(d[v] <= c) d171a2e25c 2015-03-17 kinaba: continue; d171a2e25c 2015-03-17 kinaba: if(c < d2[v]) d171a2e25c 2015-03-17 kinaba: continue; d171a2e25c 2015-03-17 kinaba: d[v] = c; d171a2e25c 2015-03-17 kinaba: for(auto e: Gr[v]) { d171a2e25c 2015-03-17 kinaba: Vert vv = e.second; d171a2e25c 2015-03-17 kinaba: Cost cc = max(d2[vv], c + e.first); d171a2e25c 2015-03-17 kinaba: if(d[vv] == INF) d171a2e25c 2015-03-17 kinaba: Q.emplace(cc, vv); d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: return d[0]; d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: }; d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: // BEGIN CUT HERE d171a2e25c 2015-03-17 kinaba: #include <ctime> d171a2e25c 2015-03-17 kinaba: double start_time; string timer() d171a2e25c 2015-03-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d171a2e25c 2015-03-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d171a2e25c 2015-03-17 kinaba: { os << "{ "; d171a2e25c 2015-03-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d171a2e25c 2015-03-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d171a2e25c 2015-03-17 kinaba: void verify_case(const long long& Expected, const long long& Received) { d171a2e25c 2015-03-17 kinaba: bool ok = (Expected == Received); d171a2e25c 2015-03-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d171a2e25c 2015-03-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d171a2e25c 2015-03-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d171a2e25c 2015-03-17 kinaba: #define END verify_case(_, MaliciousPath().minPath(N, K, from, to, cost));} d171a2e25c 2015-03-17 kinaba: int main(){ d171a2e25c 2015-03-17 kinaba: d171a2e25c 2015-03-17 kinaba: CASE(0) d171a2e25c 2015-03-17 kinaba: int N = 3; d171a2e25c 2015-03-17 kinaba: int K = 1000; d171a2e25c 2015-03-17 kinaba: int from_[] = {0,1,1,2}; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = {1,0,2,2}; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = {3,2,1,1}; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = 5004LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: CASE(1) d171a2e25c 2015-03-17 kinaba: int N = 4; d171a2e25c 2015-03-17 kinaba: int K = 1; d171a2e25c 2015-03-17 kinaba: int from_[] = {0,0,1,1,1,2,2,3}; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = {1,3,0,2,3,2,1,3}; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = {0,100,103,0,0,34,102,33}; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = 100LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: CASE(2) d171a2e25c 2015-03-17 kinaba: int N = 10; d171a2e25c 2015-03-17 kinaba: int K = 5; d171a2e25c 2015-03-17 kinaba: int from_[] = {0,0,1,1,2,2,3,3,4,4,4,5,5,6,6,7,7,8,8,9,9}; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = {1,1,2,2,3,3,4,4,5,5,4,6,6,7,7,8,8,9,9,9,9}; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = {2,10,10,1,2,10,10,1,2,10,100,10,2,1,10,10,2,1,10,10,1}; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = 514LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: CASE(3) d171a2e25c 2015-03-17 kinaba: int N = 50; d171a2e25c 2015-03-17 kinaba: int K = 200; d171a2e25c 2015-03-17 kinaba: int from_[] = {0,13,8,17,3,8,4,21,11,20,2,18,21,2,4,9,17,0,14,10,15,18,1,22,10,14, d171a2e25c 2015-03-17 kinaba: 19,24,5,5,12,7,7,16,19,13,20,15,23,6,23,9,3,6,16,11,22,24,12,1,25,25, d171a2e25c 2015-03-17 kinaba: 26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37, d171a2e25c 2015-03-17 kinaba: 37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48, d171a2e25c 2015-03-17 kinaba: 49,49,37,9,14,0,33,20,46,26,12,11,2,7,34,19,37,5,2,17,41,16,34,13,18, d171a2e25c 2015-03-17 kinaba: 35,6,14,16,25,9,10,5,10,7,36,45,3,6,22,32,28,45,40,16,36,28,16,34,1, d171a2e25c 2015-03-17 kinaba: 9,19,18,6,15,29,12,5,44,33,49,14,40,1,30,21,37,49,1,44,42,6,38,1,31, d171a2e25c 2015-03-17 kinaba: 40,37,34,35,6,43,29,41,48,17,4,38,26,4,46,43,6,27,30,0,16,40,33,0,42, d171a2e25c 2015-03-17 kinaba: 41,10,33,47,11,37,49,25,36,20,47,12,28,17,11,17,26,26,37,34,27,17,8, d171a2e25c 2015-03-17 kinaba: 2,13,43,36,28,1,23,29,40,18,22,0,7,30,23,3,39,5,23,28,38,44,19,43,15, d171a2e25c 2015-03-17 kinaba: 16,43,5,27,24,25,7,16,38,33,33,1,9,25,47,0,31,30,29,4,36,49,26,6,39, d171a2e25c 2015-03-17 kinaba: 40,28,39,48,26,2,15,41,42,32,0,35,34,28,30,40,3,33,16,15,41,45,12,33, d171a2e25c 2015-03-17 kinaba: 35,16,47,34,23}; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = {41,42,17,0,2,7,28,32,31,33,6,42,11,13,7,40,47,21,4,6,19,15,4,18,30,25, d171a2e25c 2015-03-17 kinaba: 2,10,30,34,1,47,35,23,3,0,9,25,42,21,4,6,47,32,5,40,5,0,8,49,16,29,8, d171a2e25c 2015-03-17 kinaba: 11,42,33,35,26,27,43,35,6,14,13,44,25,13,42,2,26,17,3,40,31,18,12,24, d171a2e25c 2015-03-17 kinaba: 37,0,37,15,44,35,40,10,1,35,47,36,33,2,39,23,28,32,0,6,21,33,41,0,19, d171a2e25c 2015-03-17 kinaba: 16,29,35,16,44,6,18,17,2,46,41,11,27,5,44,1,48,15,43,8,41,33,16,11,45, d171a2e25c 2015-03-17 kinaba: 47,19,41,14,41,8,24,13,3,44,41,42,30,31,44,21,14,43,48,0,6,25,38,36,14, d171a2e25c 2015-03-17 kinaba: 36,22,43,15,20,19,37,25,17,44,17,46,8,25,33,19,42,40,42,24,15,31,34,8, d171a2e25c 2015-03-17 kinaba: 41,25,20,29,2,5,43,28,33,40,31,27,6,21,9,35,8,8,26,13,11,31,4,4,30,34, d171a2e25c 2015-03-17 kinaba: 35,1,15,11,10,24,15,24,23,16,24,32,9,1,6,17,48,6,35,19,12,5,21,23,25,9, d171a2e25c 2015-03-17 kinaba: 17,47,19,23,22,35,19,5,1,10,9,41,11,45,0,23,0,11,39,0,17,2,18,4,17,24,0, d171a2e25c 2015-03-17 kinaba: 10,19,4,36,22,42,18,13,48,27,11,19,28,31,39,32,48,2,26,38,43,38,49,34, d171a2e25c 2015-03-17 kinaba: 37,11,9,11,14,12,9,37,0,22,14,15,0,8,23,7,43,5,8,16,47}; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = {494,848305,3326,6008,223,2,6,673,152335,25,713909,42842,122,9941,361853, d171a2e25c 2015-03-17 kinaba: 35,20157,1098,41,83693,365851,43843,622,591230,7722,2489,7,861622,21272, d171a2e25c 2015-03-17 kinaba: 169,1153,3,7,3,577031,24522,5,241,757900,11036,8892,199,7,2,1,453031,115, d171a2e25c 2015-03-17 kinaba: 13,125,67,35,91,33427,14,106,203749,5,1804,4543,23,1,281,441212,18,3, d171a2e25c 2015-03-17 kinaba: 11629,233,388188,10,701,76170,763,875,11606,44972,6449,37409,83516,5912, d171a2e25c 2015-03-17 kinaba: 6,705899,26759,253,580531,14215,21916,884775,30,678,5940,17,480830,8, d171a2e25c 2015-03-17 kinaba: 61,218613,683352,4,557589,2619,3658,221515,15825,163577,25,9,1,4,13,2,88, d171a2e25c 2015-03-17 kinaba: 147,110163,18118,2,15429,211872,24,1,188382,12500,2348,190,4279,40,8428, d171a2e25c 2015-03-17 kinaba: 56325,933152,231523,9454,21,4855,96168,1722,329515,77,1,3,3518,10926, d171a2e25c 2015-03-17 kinaba: 12172,4,71,181976,1318,9,5086,905,108490,80164,2,10236,197,1880,17420, d171a2e25c 2015-03-17 kinaba: 614650,372457,13918,36,17,167,6,254127,512,15,341436,1,186,96,7,3,42,4, d171a2e25c 2015-03-17 kinaba: 3,4,492598,18523,172302,1,421535,390382,2952,6228,871,505372,131266,5, d171a2e25c 2015-03-17 kinaba: 743902,11,34,657,4717,3196,259,192504,229,6786,28,44364,21123,8,166781, d171a2e25c 2015-03-17 kinaba: 885021,2,828,497,376,24707,52,1,1659,19402,27261,82,473,443,1089,586,20, d171a2e25c 2015-03-17 kinaba: 7,239277,27132,4681,761,10644,17798,1,820306,13330,19,293167,2,4,89279, d171a2e25c 2015-03-17 kinaba: 14,1,9,18023,1165,495221,32304,538,178613,1,4764,32767,114,103,1,302,428, d171a2e25c 2015-03-17 kinaba: 92,927352,22270,2646,3599,6,16362,3,4,280286,338,652,2,347022,23,323084, d171a2e25c 2015-03-17 kinaba: 3338,46,58,1263,93,46992,19112,1,19499,33,807600,20296,16803,911294,3151, d171a2e25c 2015-03-17 kinaba: 1,793,1,1,504,62209,1397,52726,3650,54,3630,358480,178,394,28,137436,52764, d171a2e25c 2015-03-17 kinaba: 1209,1599}; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = 121213509LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: CASE(4) d171a2e25c 2015-03-17 kinaba: int N = 20; d171a2e25c 2015-03-17 kinaba: int K = 1000; d171a2e25c 2015-03-17 kinaba: int from_[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,18}; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0,0}; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = {1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000, d171a2e25c 2015-03-17 kinaba: 1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000, d171a2e25c 2015-03-17 kinaba: 1000000}; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = 19019000000LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: CASE(5) d171a2e25c 2015-03-17 kinaba: int N = 2; d171a2e25c 2015-03-17 kinaba: int K = 0; d171a2e25c 2015-03-17 kinaba: int from_[] = {0,1}; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = {0,1}; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = {5,4}; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = -1LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: /* d171a2e25c 2015-03-17 kinaba: CASE(6) d171a2e25c 2015-03-17 kinaba: int N = ; d171a2e25c 2015-03-17 kinaba: int K = ; d171a2e25c 2015-03-17 kinaba: int from_[] = ; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = ; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = ; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: CASE(7) d171a2e25c 2015-03-17 kinaba: int N = ; d171a2e25c 2015-03-17 kinaba: int K = ; d171a2e25c 2015-03-17 kinaba: int from_[] = ; d171a2e25c 2015-03-17 kinaba: vector <int> from(from_, from_+sizeof(from_)/sizeof(*from_)); d171a2e25c 2015-03-17 kinaba: int to_[] = ; d171a2e25c 2015-03-17 kinaba: vector <int> to(to_, to_+sizeof(to_)/sizeof(*to_)); d171a2e25c 2015-03-17 kinaba: int cost_[] = ; d171a2e25c 2015-03-17 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); d171a2e25c 2015-03-17 kinaba: long long _ = LL; d171a2e25c 2015-03-17 kinaba: END d171a2e25c 2015-03-17 kinaba: */ d171a2e25c 2015-03-17 kinaba: } d171a2e25c 2015-03-17 kinaba: // END CUT HERE