Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define INF INT_MAX;
  5.  
  6. const int MAX1 = 5010;
  7. const int MAX2 = 110;
  8. int belongs[MAX1];
  9. string cities;
  10. vector<vector<pair<int,int>>> graphDijkstra;
  11. vector<vector<int>> graph;
  12. vector<int> dist, path;
  13. int residualGraph[MAX1][MAX1];
  14. int totalPlaces, nOfCities, nOfRoads, source, to;
  15.  
  16. bool bfs(int parent[]) {
  17.     bool visited[MAX1] = {false};
  18.     queue<int> q;
  19.     q.push(source);
  20.     visited[source] = true;
  21.     parent[source] = -1;
  22.     while (!q.empty()) {
  23.         int u = q.front(); q.pop();
  24.         for (int v = 0; v < MAX1; v++) {
  25.             if (!visited[v] and residualGraph[u][v] > 0) {
  26.                 q.push(v);
  27.                 parent[v] = u;
  28.                 visited[v] = true;
  29.             }
  30.         }
  31.     }
  32.     return visited[to];
  33. }
  34.  
  35. int fordFulkerson() {
  36.     int u, v;
  37.     int parent[MAX1];
  38.     int maxFlow = 0;
  39.     while (bfs(parent)) {
  40.         int pathFlow = INT_MAX;
  41.         for (v = to; v != source; v = parent[v]) {
  42.             u = parent[v];
  43.             pathFlow = min(pathFlow, residualGraph[u][v]);
  44.         }
  45.         for (v = to; v != source; v = parent[v]) {
  46.             u = parent[v];
  47.             residualGraph[u][v] -= pathFlow;
  48.             residualGraph[v][u] += pathFlow;
  49.         }
  50.         maxFlow += pathFlow;
  51.     }
  52.     return maxFlow;
  53. }
  54.  
  55. int dijkstra() {
  56.     int s = belongs[source];
  57.     int t = belongs[to];
  58.     path.resize(MAX2);
  59.     dist.resize(MAX2);
  60.     for (int i = 0; i < dist.size(); i++) {
  61.         dist[i] = INF;
  62.     }
  63.     dist[source] = 0;
  64.     priority_queue<pair<int,int>> pq;
  65.     dist[source] = 0;
  66.     cout << "coiroi\n";
  67.     path[source] = -1;
  68.     pq.push({0, source});
  69.     while (!pq.empty()) {
  70.         int u = pq.top().second; pq.pop();
  71.         for (pair<int,int> p : graphDijkstra[u]) {
  72.             if (dist[u] + p.second < dist[p.first]) {
  73.                 dist[p.first] = dist[u] + p.second;
  74.                 path[p.first] = u;
  75.                 pq.push({-dist[p.first], p.first});
  76.             }
  77.         }
  78.     }
  79.     return dist[t];
  80. }
  81.  
  82. int main() {
  83.     cin >> totalPlaces;
  84.     cin >> nOfCities;
  85.     cout << "TOTALPLACES = " << totalPlaces << " and NOFCITIES = " << nOfCities << endl;
  86.     char lixo;
  87.     scanf("%c",&lixo);
  88.     for (int i = 0; i < nOfCities; i++) {
  89.         getline(cin, cities);
  90.         cout << cities << endl;
  91.         int n = 0;
  92.         for (int j = 0; cities[j]; j++) {
  93.             if (cities[j] == ' ') {
  94.                 belongs[n] = i;
  95.                 n = 0;
  96.             } else {
  97.                 n = n * 10 + (cities[j] - '0');
  98.             }
  99.         }
  100.         belongs[n] = i;
  101.     }
  102.     cin >> nOfRoads;
  103.     cout << "NOFROADS = " << nOfRoads << endl;
  104.     graph.resize(MAX1);
  105.     graphDijkstra.resize(MAX2);
  106.     for (int i = 0; i < nOfRoads; i++) {
  107.         int u, v, c, d;
  108.         cin >> u >> v >> c >> d;
  109.         cout << u << " " << v << " " << c << " " << d << endl;
  110.         graph[u].push_back(v);
  111.         graph[v].push_back(u);
  112.         residualGraph[u][v] += c;
  113.         if (belongs[u] != belongs[v]) {
  114.             graphDijkstra[belongs[u]].push_back({belongs[v], d});
  115.         }
  116.     }
  117.     cin >> source >> to;
  118.     cout << "SOURCE = " << source << " and TO = " << to << endl;
  119.     cout << dijkstra() << endl;
  120.     cout << fordFulkerson() << endl;
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement