|
| 1 | +// |
| 2 | +// Created by Alon on 13/06/2020. |
| 3 | +// |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <cstdlib> |
| 7 | +#include <cstring> |
| 8 | +#include <fstream> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | +#include <cassert> |
| 12 | +#include <climits> |
| 13 | +#include <cmath> |
| 14 | +#include <iostream> |
| 15 | +#include <algorithm> |
| 16 | +#include <vector> |
| 17 | +#include <queue> |
| 18 | +#include <set> |
| 19 | +#include <map> |
| 20 | + |
| 21 | +using namespace std; |
| 22 | + |
| 23 | +typedef pair<int,int> ii; |
| 24 | +typedef pair<int,ii> iii; |
| 25 | +typedef vector<ii> vii; |
| 26 | +typedef vector<vii> vvii; |
| 27 | +typedef vector<int> vi; |
| 28 | +typedef vector<vi> vvi; |
| 29 | +typedef set<int> si; |
| 30 | +typedef vector<si> vsi; |
| 31 | +typedef vector<map<int, int>> vmii; |
| 32 | + |
| 33 | +const int INF = 1e9; |
| 34 | +using namespace std; |
| 35 | +typedef long long ll; |
| 36 | + |
| 37 | +/********** Max Flow **********/ |
| 38 | +typedef pair<int, int> pii; |
| 39 | + |
| 40 | +int augment(vector<map<int, int>>& res, int s, int t, const vi& p, int minEdge) { |
| 41 | + // traverse the path from s to t according to p. |
| 42 | + // change the residuals on this path according to the min edge weight along this path. |
| 43 | + // return the amount of flow that was added. |
| 44 | + if (t == s) { |
| 45 | + return minEdge; |
| 46 | + } else if (p[t] != -1) { |
| 47 | + int f = augment(res, s, p[t], p, min(minEdge, res[p[t]][t])); |
| 48 | + res[p[t]][t] -= f; |
| 49 | + res[t][p[t]] += f; |
| 50 | + return f; |
| 51 | + } |
| 52 | + return 0; |
| 53 | +} |
| 54 | + |
| 55 | +// input: number of nodes (n), all nodes are between 0 and n-1, |
| 56 | +// edges v1->v2 of the form (weight,(v1,v2)), source (s) and target (t). |
| 57 | +// output: max flow from s to t over the edges. |
| 58 | +// time: O(VE^2) and O(EF). |
| 59 | +int EdmondsKarp (int n, vector<iii>& edges, int s, int t, vmii& res) { |
| 60 | + // initialise adjacenty list and residuals adjacency matrix |
| 61 | + |
| 62 | + // vvi res(n,vi(n,0)); |
| 63 | + vvi adj(n); |
| 64 | + for (iii e : edges) { |
| 65 | + res[e.second.first][e.second.second] += e.first; |
| 66 | + adj[e.second.first].push_back(e.second.second); |
| 67 | + adj[e.second.second].push_back(e.second.first); |
| 68 | + } |
| 69 | + // while we can add flow |
| 70 | + int addedFlow, maxFlow = 0; |
| 71 | + do { |
| 72 | + // save to p the BFS tree from s to t using only edges with residuals |
| 73 | + //vi dist(res.size(), INF); |
| 74 | + vi dist(n, INF); |
| 75 | + dist[s] = 0; |
| 76 | + queue<int> q; |
| 77 | + q.push(s); |
| 78 | + // vi p(res.size(), -1); |
| 79 | + vi p(n, -1); |
| 80 | + while (!q.empty()) { |
| 81 | + int u = q.front(); q.pop(); |
| 82 | + if (u == t) break; |
| 83 | + for (int v : adj[u]) if (res[u][v] > 0 && dist[v] == INF) { |
| 84 | + dist[v] = dist[u] + 1; |
| 85 | + q.push(v); |
| 86 | + p[v] = u; |
| 87 | + } |
| 88 | + } |
| 89 | + // add flow on the path between s to t according to p |
| 90 | + addedFlow = augment(res, s, t, p, INF); |
| 91 | + maxFlow += addedFlow; |
| 92 | + } while (addedFlow > 0); |
| 93 | + return maxFlow; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +int locations, start_location, number_of_people, number_of_steps, number_of_medics, number_of_roads; |
| 99 | +#define NODE(specific_location, time) (locations * (time) + specific_location) |
| 100 | +#define START_NODE 0 |
| 101 | +#define END_NODE (locations * number_of_steps + 1) |
| 102 | +#define NUMBER_OF_NODES (locations * number_of_steps + 2) |
| 103 | +#define INF_PEOPLE (1000 * 1000) |
| 104 | + |
| 105 | +int main() { |
| 106 | + int cases; |
| 107 | + cin >> cases; |
| 108 | + while (cases--) { |
| 109 | + vector<iii> edges; |
| 110 | + cin >> locations >> start_location >> number_of_people >> number_of_steps >> number_of_medics; |
| 111 | + number_of_steps++; |
| 112 | + // limit the flow |
| 113 | + edges.push_back({number_of_people, {START_NODE, NODE(start_location, 0)}}); |
| 114 | + |
| 115 | + for (int i = 0; i < number_of_medics; ++i) { // all medics go to the sink |
| 116 | + int m; |
| 117 | + cin >> m; |
| 118 | + edges.push_back({INF_PEOPLE, {NODE(m, number_of_steps - 1), END_NODE}}); |
| 119 | + } |
| 120 | + |
| 121 | + cin >> number_of_roads; |
| 122 | + for (int i = 0; i < number_of_roads; ++i) { //possible edges from input |
| 123 | + int from, to, max_people, stall_time; |
| 124 | + cin >> from >> to >> max_people >> stall_time; |
| 125 | + for (int time = 0; time + stall_time < number_of_steps; ++time) { |
| 126 | + edges.push_back({max_people, {NODE(from, time), NODE(to, time + stall_time)}}); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + for (int location = 1; location <= locations; ++location) { // waiting edges |
| 131 | + for (int time = 1; time < number_of_steps; ++time) { |
| 132 | + edges.push_back({INF_PEOPLE, {NODE(location, time - 1), NODE(location, time)}}); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + vector<map<int, int>> adj(NUMBER_OF_NODES); |
| 137 | + cout << EdmondsKarp(NUMBER_OF_NODES, edges, START_NODE, END_NODE, adj) << endl; |
| 138 | + } |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | + |
0 commit comments