Going Home POJ - 2195(费用流)
就是一个简单题
四个月前a的一道题,今天又看到了,再a一遍吧。
好吧 我想多了 用了bfs求最短路 其实不用的 因为没有障碍物
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 100100, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m, s, t, flow, value; char str[110][110]; int head[maxn], d[maxn], cnt, vis[maxn], p[maxn], f[maxn], di[maxn]; int dis[4][2] = {{0, 1},{0, -1},{1, 0},{-1, 0}}; vector<int> g; vector<int> ff; void bfs(int s) { mem(di, 0); queue<int> Q; Q.push(s); di[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i = 0; i < 4; i++) { int v = u + dis[i][0] * m + dis[i][1]; if(u % m == 0 && i == 0 || (u - 1) % m == 0 && i == 1) continue; if(v < 1 || v > n * m || di[v]) continue; di[v] = di[u] + 1; Q.push(v); } } } struct node { int u, v, c, w, next; }Node[maxn]; void add_(int u, int v, int w, int c) { Node[cnt].u = u; Node[cnt].v = v; Node[cnt].w = w; Node[cnt].c = c; Node[cnt].next = head[u]; head[u] = cnt++; } void add(int u, int v, int w, int c) { add_(u, v, w, c); add_(v, u, -w, 0); } int spfa() { for(int i = 0; i < maxn; i++) d[i] = INF; queue<int> Q; mem(vis, 0); mem(p, -1); Q.push(s); vis[s] = 1; d[s] = 0; p[s] = 0; f[s] = INF; while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; // cout << u << endl; for(int i = head[u]; i != -1; i = Node[i].next) { node e = Node[i]; //cout << e.v << endl; if(d[e.v] > d[u] + e.w && e.c > 0) { d[e.v] = d[u] + e.w; p[e.v] = i; f[e.v] = min(f[u], e.c); if(!vis[e.v]) { vis[e.v] = 1; Q.push(e.v); // cout << 11111 << endl; } } } } if(p[t] == -1) return 0; flow += f[t]; value += f[t] * d[t]; for(int i = t; i != s; i = Node[p[i]].u) { Node[p[i]].c -= f[t]; Node[p[i] ^ 1].c += f[t]; } return 1; } void max_flow() { flow = value = 0; while(spfa()); printf("%d\n", value); } int main() { while(cin >> n >> m && n + m) { s = 0, t = n * m + 1; mem(head, -1); cnt = 0; g.clear(); ff.clear(); for(int i = 1; i <= n; i++) { scanf("%s", str[i] + 1); for(int j = 1; j <= m; j++) { if(str[i][j] == 'm') g.push_back((i - 1) * m + j), add(s, (i - 1) * m + j, 0, 1); else if(str[i][j] == 'H') ff.push_back((i - 1) * m + j), add((i - 1) * m + j, t, 0, 1); } } for(int i = 0; i < g.size(); i++) { bfs(g[i]); for(int j = 0; j < ff.size(); j++) { add(g[i], ff[j], di[ff[j]] - 1, 1); // cout << g[i] << " " << ff[j] << " " << di[ff[j]] - 1 << endl; } } max_flow(); } return 0; }
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。