Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 10806 - Dijkstra, Dijkstra.
Source : UVA Online Judge
Category : Graph Theory
Algorithm : Min Cost Max Flow
Verdict : Accepted
- #include "bits/stdc++.h"
-
- using namespace std;
-
- #define FI freopen("in.txt", "r", stdin)
- #define FO freopen("out.txt", "w", stdout)
- #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
-
- #define FOR(i, n) for (int i = 1; i <= n; i++)
- #define For(i, n) for (int i = 0; i < n; i++)
- #define ROF(i, n) for (int i = n; i >= 1; i--)
- #define Rof(i, n) for (int i = n-1; i >= 0; i--)
- #define FORI(i, n) for (auto i : n)
- #define REP(i, a, b) for (int i = a; i <= b; i++)
-
- #define ll long long
- #define ull unsigned long long
- #define vi vector <int>
- #define vl vector <ll>
- #define pii pair <int, int>
- #define pll pair <ll, ll>
- #define mk make_pair
- #define ff first
- #define ss second
- #define eb emplace_back
- #define em emplace
- #define pb push_back
- #define ppb pop_back
- #define All(a) a.begin(), a.end()
- #define memo(a, b) memset(a, b, sizeof a)
- #define Sort(a) sort(All(a))
- #define ED(a) Sort(a), a.erase(unique(All(a)), a.end())
-
- #define sz(a) (int)a.size()
- #define max3(a, b, c) max(a, max(b, c))
- #define min3(a, b, c) min(a, min(b, c))
- #define maxAll(a) *max_element(All(a))
- #define minAll(a) *min_element(All(a))
- #define allUpper(a) transform(All(a), a.begin(), :: toupper)
- #define allLower(a) transform(All(a), a.begin(), :: tolower)
- #define endl '\n'
- #define nl puts("")
- #define ub upper_bound
- #define lb lower_bound
- #define Exp exp(1.0)
- #define PIE 2*acos(0.0)
- #define Sin(a) sin(((a)*PIE)/180.0)
- #define EPS 1e-9
-
- #include "ext/pb_ds/assoc_container.hpp"
- #include "ext/pb_ds/tree_policy.hpp"
- using namespace __gnu_pbds;
-
- template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
-
- #include "ext/rope"
- using namespace __gnu_cxx;
-
-
-
-
-
-
-
-
-
-
- #define trace1(x) cerr << #x << ": " << x << endl;
- #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
- #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
- #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
- #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
- #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
-
- inline int setbit(int mask, int pos) { return mask |= (1 << pos); }
- inline int resetbit(int mask, int pos) { return mask &= ~(1 << pos); }
- inline int togglebit(int mask, int pos) { return mask ^= (1 << pos); }
- inline bool checkbit(int mask, int pos) { return (bool)(mask & (1 << pos)); }
-
- #define popcount(mask) __builtin_popcount(mask) // count set bit
- #define popcountLL(mask) __builtin_popcountll(mask) // for long long
-
- inline int read() { int a; scanf("%d", &a); return a; }
- inline ll readLL() { ll a; scanf("%lld", &a); return a; }
- inline double readDD() { double a; scanf("%lf", &a); return a; }
-
- template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
- int toInt(string s) { int num; istringstream iss(s); iss >> num; return num; }
- ll toLLong(string s) { ll num; istringstream iss(s); iss >> num; return num; }
-
- #define inf 1e17
- #define mod 1000000007
-
- static const int maxn = 2e4 + 5;
- static const int logn = 18;
-
-
-
-
-
- struct edge
- {
- int to, flow, cap, cost, rev;
- edge(int to = 0, int flow = 0, int cap = 0, int cost = 0, int rev = 0) :
- to(to), flow(flow), cap(cap), cost(cost), rev(rev) {}
- };
-
- struct MinCostMaxFlow
- {
- int nodes;
- vector <int> prio, curflow, prevedge, prevnode, q, pot;
- vector <bool> inqueue;
- vector< vector<edge> > graph;
-
- MinCostMaxFlow() {}
-
- MinCostMaxFlow(int n) :
- nodes(n), prio(n, 0), curflow(n, 0), prevedge(n, 0),
- prevnode(n, 0), q(n, 0), pot(n, 0), inqueue(n, 0), graph(n) {}
-
- void addEdge(int source, int to, int capacity, int cost)
- {
- edge a = {to, 0, capacity, cost, (int)graph[to].size()};
- edge b = {source, 0, 0, -cost, (int)graph[source].size()};
- graph[source].push_back(a);
- graph[to].push_back(b);
- }
-
- void bellman_ford(int source, vector<int> &dist)
- {
- fill(dist.begin(), dist.end(), INT_MAX);
- dist[source] = 0;
- int qt=0;
- q[qt++] = source;
- for(int qh = 0;(qh-qt) % nodes != 0; qh++)
- {
- int u = q[qh % nodes];
- inqueue[u] = false;
- for(auto &e : graph[u])
- {
- if(e.flow >= e.cap) continue;
- int v = e.to;
- int newDist = dist[u] + e.cost;
- if(dist[v] > newDist)
- {
- dist[v] = newDist;
- if(!inqueue[v])
- {
- inqueue[v] = true;
- q[qt++ % nodes] = v;
- }
- }
- }
- }
- }
-
- pair<int, int> minCostFlow(int source, int dest, int maxflow)
- {
- bellman_ford(source, pot);
- int flow = 0;
- int flow_cost = 0;
- while(flow < maxflow)
- {
- priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > q;
- q.push({0, source});
- fill(prio.begin(), prio.end(), INT_MAX);
- prio[source] = 0;
- curflow[source] = INT_MAX;
- while(!q.empty())
- {
- int d = q.top().first;
- int u = q.top().second;
- q.pop();
- if(d != prio[u]) continue;
- for(int i = 0; i < graph[u].size(); i++)
- {
- edge &e=graph[u][i];
- int v = e.to;
- if(e.flow >= e.cap) continue;
- int newPrio = prio[u] + e.cost + pot[u] - pot[v];
- if(prio[v] > newPrio)
- {
- prio[v] = newPrio;
- q.push({newPrio, v});
- prevnode[v] = u;
- prevedge[v] = i;
- curflow[v] = min(curflow[u], e.cap - e.flow);
- }
- }
- }
- if(prio[dest] == INT_MAX) break;
- for(int i = 0;i < nodes; i++) pot[i] += prio[i];
- int df = min(curflow[dest], maxflow - flow);
- flow += df;
- for(int v = dest; v!= source; v = prevnode[v])
- {
- edge &e = graph[ prevnode[v] ][ prevedge[v] ];
- e.flow += df;
- graph[v][e.rev].flow -= df;
- flow_cost += df * e.cost;
- }
- }
- return {flow, flow_cost};
- }
- };
-
- int main()
- {
- int tnode, tedge;
- while (scanf("%d", &tnode) == 1)
- {
- if (tnode == 0) break;
- MinCostMaxFlow mcmf(tnode+2);
- tedge = read();
- FOR(e, tedge)
- {
- int u = read();
- int v = read();
- int c = read();
- mcmf.addEdge(u, v, 1, c);
- mcmf.addEdge(v, u, 1, c);
- }
- int S = 0;
- int T = tnode + 1;
- mcmf.addEdge(S, 1, 2, 0);
- mcmf.addEdge(1, S, 2, 0);
- mcmf.addEdge(tnode, T, 2, 0);
- mcmf.addEdge(T, tnode, 2, 0);
- pii res = mcmf.minCostFlow(S, T, 2);
- if (res.first != 2) puts("Back to jail");
- else printf("%d\n", res.second);
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.