日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 1533 Going Home(费用流板子题)

發布時間:2024/4/18 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 1533 Going Home(费用流板子题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a ‘.’ means an empty space, an ‘H’ represents a house on that point, and am ‘m’ indicates there is a little man on that point. You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of ‘H’s and ‘m’s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0

Sample Output

2 10 28

AC

  • 建邊

  • 源點到people建邊,流量為1, 費用為0
  • 所有的people到所有的house建邊, 流量為1, 費用是曼哈頓距離
  • house到匯點建邊,流量為1,費用為0
  • 費用流模板

#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <queue> #include <cmath> #include <algorithm> #include <cmath> #define N 80005 #include <cstring> #define ll long long #define P pair<int, int> #define mk make_pair using namespace std; // 本來是prev記錄前驅節點的標號,但是hdu不讓用,這里改為fuck int fuck[10200], dis[10200], head[10200] ; bool vis[10200]; int n, m, cnt; int inf = 0x3f3f3f3f;struct ac{int v, c, cost, pre; }edge[N];void addedge(int u, int v, int c, int cost) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].cost = cost;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c = 0;edge[cnt].cost = -cost;edge[cnt].pre = head[u];head[u] = cnt++; } bool spfa(int s, int e) {memset(vis, false, sizeof(vis));memset(dis, inf, sizeof(dis));memset(fuck, -1, sizeof(fuck));queue<int> que;que.push(s);dis[s] = 0;vis[s] = true;while (!que.empty()) {int u = que.front();que.pop();vis[u] = false;for (int i = head[u]; i != -1; i = edge[i].pre) {int v = edge[i].v;int c = edge[i].c;int cost = edge[i].cost;if (dis[v] > dis[u] + cost && c > 0) {dis[v] = dis[u] + cost;fuck[v] = i;if (!vis[v]) {vis[v] = true;que.push(v);}}}}if (dis[e] == inf)return false;elsereturn true; }int mincost(int s, int e, int &cost) {int maxflow = 0;int minflow = inf;while (spfa(s, e)) {for (int i = fuck[e]; i != -1; i = fuck[edge[i ^ 1].v])minflow = min(minflow, edge[i].c);for (int i = fuck[e]; i != -1; i = fuck[edge[i ^ 1].v]) {edge[i].c -= minflow;edge[i ^ 1].c += minflow;cost += minflow * edge[i].cost;}maxflow += minflow;}return maxflow; } // 存放hose, people信息 struct cell {int num, x, y; }; void debug(int a, int b, int c, int cost) { // cout << a << " -> " << b << " " << c << " " << cost << endl; } char g[102][102]; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifwhile (scanf("%d%d", &n, &m), n + m) {memset(head, -1, sizeof(head));cnt = 0;int start = 0, end = n * m + 1;for (int i = 0; i < n; ++i) {scanf("%s", g[i]);}// 統計 people和house 的信息 vector<cell> house, people;for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (g[i][j] == '.') continue;if (g[i][j] == 'H') {int num = i * m + j + 1;people.push_back((cell){num, i, j});}if (g[i][j] == 'm') {int num = i * m + j + 1;house.push_back((cell){num, i, j});}}}int house_num = house.size();int people_num = people.size();// 源點到people建邊,花費0, 流量1 for (int i = 0; i < people_num; ++i) {cell temp_people = people[i];addedge(start, temp_people.num, 1, 0);debug(start, temp_people.num, 1, 0);}// house到匯點建邊, 花費0, 流量1 for (int i = 0; i < house_num; ++i) {cell temp_house = house[i];addedge(temp_house.num, end, 1, 0);debug(temp_house.num, end, 1, 0);}// people到house建邊,花費為曼哈頓距離,流量為1 for (int i = 0; i < people_num; ++i) {for (int j = 0; j < house_num; ++j) {cell temp_house = house[j];cell temp_people = people[i];int temp_dis = fabs(temp_house.x - temp_people.x) + fabs(temp_house.y - temp_people.y);addedge(temp_people.num, temp_house.num, 1, temp_dis);debug(temp_people.num, temp_house.num, 1, temp_dis);}}int ans = 0; mincost(start, end, ans);printf("%d\n", ans);}return 0; }

總結

以上是生活随笔為你收集整理的HDU Problem - 1533 Going Home(费用流板子题)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。