【LOJ6225】【网络流24题】火星探险问题
生活随笔
收集整理的這篇文章主要介紹了
【LOJ6225】【网络流24题】火星探险问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
https://loj.ac/problem/6225
Solution
跟深海機器人類似地建邊。
注意輸出方案時有點坑,找到一條路徑后必須退回終點,再繼續找,不然會出bug。
Code
/************************************************* Au: Hany01* Date: Jul 13th, 2018* Prob: LOJ6225 火星探險* Email: hany01@foxmail.com* Inst: Yali High School ************************************************/#include<bits/stdc++.h>using namespace std;typedef long long LL; typedef pair<int, int> PII; #define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout) #define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i) #define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i) #define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i) #define Set(a, b) memset(a, b, sizeof(a)) #define Cpy(a, b) memcpy(a, b, sizeof(a)) #define x first #define y second #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define ALL(a) (a).begin(), (a).end() #define SZ(a) ((int)(a).size()) #define INF (0x3f3f3f3f) #define INF1 (2139062143) #define debug(...) fprintf(stderr, __VA_ARGS__) #define y1 wozenmezhemecaiatemplate <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }inline int read() {static int _, __; static char c_;for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);return _ * __; }const int maxn = 37 * 37 * 2, maxm = maxn << 4;int P, Q, beg[maxn], nex[maxm], v[maxm], w[maxm], f[maxm], S, T, vis[maxn], dis[maxn], e = 1, cost, tot, pt[maxn], cnt, tt;inline void add(int uu, int vv, int ff, int ww, int fl = 1) {v[++ e] = vv, f[e] = ff, w[e] = ww, nex[e] = beg[uu], beg[uu] = e;if (fl) add(vv, uu, 0, -ww, 0); }#define idx(i, j, k) (((i) - 1) * Q + (j) + (k) * P * Q)inline bool BFS() {static queue<int> q;For(i, 1, T) dis[i] = INF;Set(vis, 0), dis[S] = 0, q.push(S);while (!q.empty()) {int u = q.front(); q.pop(), vis[u] = 0;for (register int i = beg[u]; i; i = nex[i])if (f[i] && chkmin(dis[v[i]], dis[u] + w[i]))if (!vis[v[i]]) vis[v[i]] = 1, q.push(v[i]);}return dis[T] != INF; }int DFS(int u, int flow) {if (u == T) return flow;int t, res = flow;vis[u] = 1;for (register int i = beg[u]; i; i = nex[i])if (f[i] && dis[v[i]] == dis[u] + w[i] && !vis[v[i]]) {f[i] -= (t = DFS(v[i], min(res, f[i]))), f[i ^ 1] += t, cost += t * w[i];if (!(res -= t)) return flow;}return flow - res; }void getPath(int u, int now) {if (u == T) {++ cnt;For(i, 1, tot) printf("%d %d\n", cnt, pt[i]);return;}for (register int i = beg[u]; i; i = nex[i]) {if (!(i & 1) && f[i ^ 1] > 0) {-- f[i ^ 1];int A = u % (P * Q), B = v[i] % (P * Q);if (!B) B = P * Q;if (!A) A = P * Q;if (A != B) {if (u != S && v[i] != T) {if (B - A == 1) pt[++ tot] = 0;else assert(B == A + Q), pt[++ tot] = 1;getPath(v[i], now), -- tot;} else getPath(v[i], now);} else getPath(v[i], now);if (cnt == now) return;}} }int main() { #ifdef hany01File("loj6225"); #endiftot = read(), P = read(), Q = read(), T = (S = ((P * Q) << 1) + 1) + 1;add(S, idx(1, 1, 0), tot, 0), add(idx(P, Q, 1), T, tot, 0);For(j, 1, Q) For(i, 1, P) {int t = read();if (!(t & 1)) add(idx(i, j, 0), idx(i, j, 1), INF, 0);if (t == 2) add(idx(i, j, 0), idx(i, j, 1), 1, -1);if (j + 1 <= Q) add(idx(i, j, 1), idx(i, j + 1, 0), INF, 0);if (i + 1 <= P) add(idx(i, j, 1), idx(i + 1, j, 0), INF, 0);}while (BFS()) do Set(vis, 0), tt += DFS(S, INF); while (vis[T]);tot = 0;For(Cas, 1, tt) getPath(S, Cas);return 0; } //至今商女,時時猶唱,后庭遺曲。 // -- 王安石《桂枝香·登臨送目》總結
以上是生活随笔為你收集整理的【LOJ6225】【网络流24题】火星探险问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【操作系统】HRT任务和SRT任务(实时
- 下一篇: 我的第一桶金