Advertisement
VladNitu

makeSpanDianaWrongAns

Apr 25th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <climits>
  5. #include <algorithm>
  6. #include <fstream>
  7.  
  8. #define MAXN 100000// maximum number of nodes in graph
  9. #define NMAX 100 + 5
  10. #define TMAX 1000 + 5
  11. using namespace std;
  12.  
  13. void __print(int x) { cerr << x; }
  14.  
  15. void __print(long x) { cerr << x; }
  16.  
  17. void __print(long long x) { cerr << x; }
  18.  
  19. void __print(unsigned x) { cerr << x; }
  20.  
  21. void __print(unsigned long x) { cerr << x; }
  22.  
  23. void __print(unsigned long long x) { cerr << x; }
  24.  
  25. void __print(float x) { cerr << x; }
  26.  
  27. void __print(double x) { cerr << x; }
  28.  
  29. void __print(long double x) { cerr << x; }
  30.  
  31. void __print(char x) { cerr << '\'' << x << '\''; }
  32.  
  33. void __print(const char *x) { cerr << '\"' << x << '\"'; }
  34.  
  35. void __print(const string &x) { cerr << '\"' << x << '\"'; }
  36.  
  37. void __print(bool x) { cerr << (x ? "true" : "false"); }
  38.  
  39. template<typename T, typename V, typename W>
  40. void __print(const std::tuple<T, V, W> &x) {
  41. cerr << '{';
  42. __print(std::get<0>(x));
  43. cerr << ',';
  44. __print(std::get<1>(x));
  45. cerr << ',';
  46. __print(std::get<2>(x));
  47. cerr << '}';
  48. }
  49.  
  50. template<typename T, typename V>
  51. void __print(const pair<T, V> &x) {
  52. cerr << '{';
  53. __print(x.first);
  54. cerr << ',';
  55. __print(x.second);
  56. cerr << '}';
  57. }
  58.  
  59. template<typename T>
  60. void __print(const T &x) {
  61. int f = 0;
  62. cerr << '{';
  63. for (auto &i: x) cerr << (f++ ? "," : ""), __print(i);
  64. cerr << "}";
  65. }
  66.  
  67. void _print() { cerr << "]\n"; }
  68.  
  69. template<typename T, typename... V>
  70. void _print(T t, V... v) {
  71. __print(t);
  72. if (sizeof...(v)) cerr << ", ";
  73. _print(v...);
  74. }
  75.  
  76. #ifndef ONLINE_JUDGE
  77. #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
  78. #else
  79. #define dbg(x...)
  80. #endif
  81.  
  82.  
  83.  
  84. int n, m, k;
  85. int start, destination, weight;
  86. int startOfNewAgent;
  87. int vertex, steps, timeStamp;
  88.  
  89.  
  90. std::vector<int> agents;
  91. std::vector<std::vector<std::pair<int, int>>> targetPath;
  92.  
  93. // edges in the original graph
  94. int edges[NMAX][NMAX];
  95.  
  96. int viz[NMAX][TMAX];
  97.  
  98.  
  99. struct Edge
  100. {
  101. Edge(int _a, int _b, int _c, int _f, int _w) {
  102. a = _a; b = _b; c = _c; f = _f; w = _w;
  103. }
  104. ~Edge() { };
  105. int a; //from
  106. int b; //to
  107. int c; //capacity
  108. int f; //flow
  109. int w; //weight
  110. Edge* r;
  111. };
  112.  
  113.  
  114. const int MAX_DIST = 2000000; //do not choose INT_MAX because you are adding weights to it
  115. std::vector<Edge*> adj[MAXN];
  116. int distances[MAXN];
  117. Edge* parents[MAXN];
  118.  
  119. int nodecount = 1000;
  120. int source;
  121. int sink;
  122.  
  123. std::vector<std::vector<int>> layer_on_timestamp;
  124. std::vector<std::vector<int>> layer_on_double_timestamp;
  125. std::vector<int> sinks;
  126.  
  127. std::vector<int> possibleMakeSpan;
  128.  
  129. // bellman - ford
  130. bool find_path(int from, int to, std::vector<Edge*>& output)
  131. {
  132. std::fill(distances, distances+nodecount, MAX_DIST);
  133. std::fill(parents, parents+nodecount, (Edge*)0);
  134. distances[from] = 0;
  135.  
  136. bool updated = true;
  137. while(updated)
  138. {
  139. updated = false;
  140. for(int j = 0; j < nodecount; ++j)
  141. for(int k = 0; k < (int)adj[j].size(); ++k){
  142. Edge* e = adj[j][k];
  143. if( e->f >= e->c ) continue;
  144. if( distances[e->b] > distances[e->a] + e->w )
  145. {
  146. //std::cerr << distances[e->b] << " " << distances[e->a] << " " << e->w << std::endl;
  147. distances[e->b] = distances[e->a] + e->w;
  148. parents[e->b] = e;
  149. updated = true;
  150. }
  151. }
  152. }
  153. output.clear();
  154. if(distances[to] == MAX_DIST) return false;
  155. int cur = to;
  156. while(parents[cur])
  157. {
  158. output.push_back(parents[cur]);
  159. cur = parents[cur]->a;
  160. }
  161. return true;
  162. }
  163.  
  164.  
  165. // MAX FLOW MIN COST IMPLEMENTATION
  166. int min_cost_max_flow(int source, int sink)
  167. {
  168. int total_cost = 0;
  169.  
  170. int totalFlow = 0;
  171. std::vector<Edge*> p;
  172. while(find_path(source, sink, p))
  173. {
  174. int flow = INT_MAX;
  175. for(int i = 0; i < p.size(); ++i)
  176. if(p[i]->c - p[i]->f < flow) flow = p[i]->c - p[i]->f;
  177.  
  178. int cost = 0;
  179. for(int i = 0; i < p.size(); ++i) {
  180. cost += p[i]->w;
  181. p[i]->f += flow;
  182. p[i]->r->f -= flow;
  183. }
  184. cost *= flow; //cost per flow
  185. total_cost += cost;
  186. totalFlow += flow;
  187. }
  188.  
  189. return totalFlow;
  190. }
  191.  
  192. int run(int source, int sink) {
  193. const auto max_flow = min_cost_max_flow(source, sink);
  194. for (int i = 0; i < nodecount; ++i) {
  195.  
  196. for (unsigned int j = 0; j < adj[i].size(); ++j)
  197. delete adj[i][j];
  198. adj[i].clear();
  199. }
  200.  
  201. return max_flow;
  202. }
  203.  
  204.  
  205. void add_edge(int a, int b, int c, int w)
  206. {
  207. Edge* e = new Edge(a, b, c, 0, w);
  208. Edge* re = new Edge(b, a, 0, 0, -w);
  209. e->r = re;
  210. re->r = e;
  211. adj[a].push_back(e);
  212. adj[b].push_back(re);
  213. }
  214.  
  215.  
  216. void reset() {
  217. sinks.clear();
  218. for (int i = 0; i < NMAX; ++i)
  219. for (int j = 0; j < NMAX; ++j)
  220. viz[i][j] = false;
  221.  
  222. // std::vector<Edge*> adj[MAXN];
  223. // int distances[MAXN];
  224. // Edge* parents[MAXN];
  225. layer_on_timestamp.clear();
  226. layer_on_double_timestamp.clear();
  227.  
  228. }
  229. void constructGraph(int max_timestamp) {
  230.  
  231.  
  232. // construct graph
  233. source = 0;
  234. sink = 1;
  235.  
  236. int id = 2;
  237.  
  238. reset();
  239.  
  240. layer_on_timestamp.resize(TMAX + 1, vector<int>(NMAX + 1, -1));
  241. layer_on_double_timestamp.resize(TMAX + 1, vector<int>(NMAX + 1, -1));
  242. sinks.resize(k + 1);
  243.  
  244. // create the sink and the corresponding edges
  245. for(int target = 0; target < k; target ++) {
  246. sinks[target] = id ++;
  247. // add edges to the sink from the target
  248. add_edge(sinks[target], sink, 1, 0);
  249. }
  250.  
  251. // construct rest of the matrix
  252. std::queue<std::pair<int, int>> queue; // node, timestamp pair
  253.  
  254. // go through all the agents and create edges from the source
  255. for(int i = 0; i < k ; i++) {
  256. queue.push(std::make_pair(agents[i], 0));
  257. layer_on_timestamp[0][agents[i]] = id ++;
  258. add_edge(source, layer_on_timestamp[0][agents[i]], 1, 0);
  259. }
  260.  
  261. while(!queue.empty()) {
  262. std::pair<int, int> current = queue.front();
  263. queue.pop();
  264.  
  265. int node = current.first;
  266. int timestamp = current.second;
  267.  
  268. if (timestamp > max_timestamp || viz[node][timestamp])
  269. continue;
  270.  
  271. viz[node][timestamp] = 1;
  272.  
  273. if (layer_on_double_timestamp[timestamp][node] == -1)
  274. layer_on_double_timestamp[timestamp][node] = id ++;
  275.  
  276.  
  277. // stay in place
  278. if(timestamp + 1 < max_timestamp) {
  279. if(layer_on_timestamp[timestamp + 1][node] == -1)
  280. layer_on_timestamp[timestamp + 1][node] = id ++;
  281.  
  282. add_edge(layer_on_double_timestamp[timestamp][node], layer_on_timestamp[timestamp + 1][node], 1,
  283. 1);
  284. queue.push(std::make_pair(node, timestamp + 1));
  285. }
  286.  
  287. for (int i = 0; i < n; i++) {
  288. int forward = edges[node][i];
  289.  
  290. if (timestamp + forward < max_timestamp && forward != 0) {
  291. if (layer_on_timestamp[timestamp + forward][i] == -1)
  292. layer_on_timestamp[timestamp + forward][i] = id ++;
  293.  
  294. add_edge(layer_on_double_timestamp[timestamp][node], layer_on_timestamp[timestamp + forward][i], 1,
  295. forward);
  296. queue.push(std::make_pair(i, timestamp + forward));
  297. }
  298. }
  299.  
  300. }
  301.  
  302. // create edges from targets to the sink target
  303. for(int target = 0; target < k; target ++) {
  304. int sink_of_target = sinks[target];
  305.  
  306. std::vector<std::pair<int, int>> path = targetPath[target];
  307.  
  308. // position -> vertex, timestamp
  309. for (auto position: path) {
  310. if (layer_on_double_timestamp[position.second][position.first] == -1)
  311. layer_on_double_timestamp[position.second][position.first] = id ++;
  312.  
  313. add_edge(layer_on_double_timestamp[position.second][position.first], sink_of_target, 1, 0);
  314. }
  315.  
  316. std::pair<int, int> last_node = path.back();
  317. for(int i = last_node.second + 1; i < max_timestamp; i++) {
  318. if (layer_on_double_timestamp[i][last_node.first] == -1)
  319. layer_on_double_timestamp[i][last_node.first] = id ++;
  320.  
  321. add_edge(layer_on_double_timestamp[i][last_node.first], sink_of_target, 1, 0);
  322. }
  323. }
  324.  
  325. for (int t = 0; t < max_timestamp; ++t)
  326. for (int i = 0; i < n; ++i)
  327. if (layer_on_timestamp[t][i] != -1 && layer_on_double_timestamp[t][i] != -1) {
  328. add_edge(layer_on_timestamp[t][i], layer_on_double_timestamp[t][i], 1, 0);
  329. }
  330.  
  331.  
  332. nodecount = id + 5;
  333. }
  334.  
  335. int main() {
  336. //std::ifstream cin("date.in");
  337. // freopen("date.in" , "r", stdin);
  338.  
  339. // read vertex num, edges num and k (agent/target count)
  340. std::cin >> n >> m >> k;
  341.  
  342. // read graph
  343. for(int i = 0; i < m; i++) {
  344. std::cin >> start >> destination >> weight;
  345.  
  346. // directed graph add just one time
  347. edges[start][destination] = weight;
  348. }
  349.  
  350. // see where the agents are starting -> value of index for each
  351. for(int i = 0; i < k; i++) {
  352. std::cin >> startOfNewAgent;
  353. agents.push_back(startOfNewAgent);
  354. }
  355.  
  356. // see the steps that each target makes -> (vertex, time)
  357. for(int i = 0; i < k; i++) {
  358. std::cin >> steps;
  359.  
  360. std::vector<std::pair<int, int>> path;
  361. for (int j = 0; j < steps; j++) {
  362. std::cin >> vertex >> timeStamp;
  363. path.push_back(std::make_pair(vertex, timeStamp));
  364.  
  365. }
  366.  
  367. targetPath.push_back(path);
  368. }
  369.  
  370. int maxTime = -1;
  371.  
  372. for (int t = 1; t <= 10; ++t) {
  373. constructGraph(t);
  374. int ans = run(0, 1);
  375. dbg(t, ans);
  376. if (ans == k) // cuplaj
  377. {
  378. maxTime = t;
  379. break;
  380. }
  381. }
  382.  
  383. cout << maxTime << '\n';
  384. return 0;
  385. }
  386.  
  387.  
  388.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement