HDU 3468 Treasure Hunting(二分匹配+最短路)

2024-04-16 11:08

本文主要是介绍HDU 3468 Treasure Hunting(二分匹配+最短路),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem Description
Do you like treasure hunting? Today, with one of his friend, iSea is on a venture trip again. As most movie said, they find so many gold hiding in their trip.
Now iSea’s clever friend has already got the map of the place they are going to hunt, simplify the map, there are three ground types:

● '.' means blank ground, they can get through it
● '#' means block, they can’t get through it
● '*' means gold hiding under ground, also they can just get through it (but you won’t, right?)

What makes iSea very delighted is the friend with him is extraordinary justice, he would not take away things which doesn’t belong to him, so all the treasure belong to iSea oneself! 
But his friend has a request, he will set up a number of rally points on the map, namely 'A', 'B' ... 'Z', 'a', 'b' ... 'z' (in that order, but may be less than 52), they start in 'A', each time friend reaches to the next rally point in the shortest way, they have to meet here (i.e. iSea reaches there earlier than or same as his friend), then start together, but you can choose different paths. Initially, iSea’s speed is the same with his friend, but to grab treasures, he save one time unit among each part of road, he use the only one unit to get a treasure, after being picked, the treasure’s point change into blank ground.
Under the premise of his friend’s rule, how much treasure iSea can get at most?


Input
There are several test cases in the input.

Each test case begin with two integers R, C (2 ≤ R, C ≤ 100), indicating the row number and the column number.
Then R strings follow, each string has C characters (must be ‘A’ – ‘Z’ or ‘a’ – ‘z’ or ‘.’ or ‘#’ or ‘*’), indicating the type in the coordinate.

The input terminates by end of file marker.

Output
For each test case, output one integer, indicating maximum gold number iSea can get, if they can’t meet at one or more rally points, just output -1.


Sample Input
  
2 4 A.B. ***C 2 4 A#B. ***C

Sample Output
  
1 2

Author
iSea @ WHU

Source
2010 ACM-ICPC Multi-University Training Contest(3)——Host by WHU
用最大流做的时候MLE了,最后用二分匹配过的
题意
         给出地图,地图上有A~Z、a~z的字母,要求按照字母顺序走遍所有字母
         每次走只能选择最短路走,但是最短路可能有多条
         每条最短路上有若干金币(也可能没有),金币在地图上用'*'表示
         '#'不能走,字母可以走,金币可以走,问怎么走能拿最多的金币(每条路上最多拿一个金币)
分析
        二分图画出来,思路就出来了。
现在 以所有的起点(不包括最后一个点)作为二分图左边的点集
以所有的金币作为二分图右边的点集
然后开始连线,怎么连? 如果左边的某个起点u在使用最短路走到它的终点时
路过了某个金币点v,那么u,v之间就可以连一条线
例如样例二:








这样一眼就明白,题目求得其实是二分图的最大匹配
但是问题是怎么建图??
看题解的时候说到一种做法:
直接bfs,bfs过程中给每个位置赋值最短距离,搜到目标位置,跳出,然后反向dfs,按照距离差值为1递减的顺序,所搜到的所有点,都是最短路径上的点。
我开始用的这个方法,超时。(应该是我没处理好)
最后换了种方法:首先对于每一段路,BFS跑一遍,记录各个点距离这段路的起点的距离
跑完之后只需对于每个金币 判断在不在最短路上?在以谁为起点的最短路上?
if dist[ i ][ i + 1] == dist[ i ][ j ] + dist[i + 1][ j ]则金币j在从 i 走到 i+1的最短路上
因为对于点的编号有两种表示(两人的汇合点1~52,每个二维坐标的一维编号i*c + j + 1)
所以最后使用的等式和上面写的不太一样,但是意思是一样的,
具体见代码:
#define mem(a,x) memset(a,x,sizeof(a))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 127;
const int inf = 1061109567;
const int INF = 0x3f3f3f3f;
const int dx[] = {-1,1,0,0};
const int dy[] = {0,0,-1,1};
struct Hungary     //二分匹配模板 =-=
{vector<int>G[N];//二分图左边的点最多只有52个,这里G[N] N 大于 52 即可int linker[N*N];//二维坐标换算成一维的点,最大能达到N*Nbool vis[N*N];bool dfs(int u){for (int i = 0;i < G[u].size();++i){int &t = G[u][i];if (!vis[t]){vis[t] = 1;if (linker[t] == -1||dfs(linker[t])){linker[t] = u;return 1;}}}return 0;}int hungary(int n)//用的匈牙利算法,传入的n是二分图左边点的个数{int res = 0;mem(linker,-1);for (int u = 1;u <= n;++u){mem(vis,0);if (dfs(u)) res++;}return res;}
}ans;
int go[N];//两人汇合(字母)的位置    最多52个
int gold[N*N];//金币的位置    最多N*N个
int d[N][N*N];//距离      d[i][gold[j]]表示第i个字母到金币gold[j](第j个金币的一维编号)的距离
char mp[N][N];//保存输入的地图
bool vis[N*N];
int r,c;//行,列
int mx;
bool bfs(int s)
{mem(vis,0);mem(d[s],INF);queue<int>q;q.push(go[s]);vis[go[s]] = 1;d[s][go[s]] = 0;while (!q.empty()){int h = q.front();q.pop();int x = (h-1)/c;int y = (h-1)%c;//一维换算成二维坐标for (int i = 0;i < 4;++i){int xx = x + dx[i];int yy = y + dy[i];if (xx>=0&&yy>=0&&xx<r&&yy<c){int nx = xx*c + yy + 1;if (mp[xx][yy] == '#') continue;if (vis[nx]) continue;q.push(nx);d[s][nx] = d[s][h] + 1;//记录距离vis[nx] = 1;}}}if (s == mx) return 1;//最后一个汇合点  不需要判断能否走到下一个点,但是还是要bfs求出距离d
//    cout<<"d["<<s<<"]["<<go[s+1]<<"] = "<<d[s][go[s+1]]<<endl;if (d[s][go[s+1]] != inf)return 1;else return 0;//能不能走到下一个汇合点
}
int judge(char c)
{if (isalpha(c)){if (c >= 'A'&&c <= 'Z') return c-'A'+1;if (c >= 'a'&&c <= 'z') return c - 'a' + 1 + 26;}return 0;
}
int main()
{while (~scanf("%d %d",&r,&c)){mem(go,-1);mem(ans.G,0);int tot = 1;mx = 0;for (int i = 0;i < r;++i){scanf("%s",mp[i]);for (int j = 0;j < c;++j){int t = judge(mp[i][j]);if (t){go[t] = i*c + j + 1;//取一维编号mx = max(mx,t);//mx保存的是最后一个字母}else if (mp[i][j] == '*'){gold[tot] = i*c + j + 1;tot++;}}}bool ok = 1;for (int i = 1;i <= mx;++i) //bfs跑最短路{if (go[i] == -1)//这句是为了防止有 A B D这样的输入(即跳过了字母C){ok = 0;break;}if (!bfs(i))//不能到达汇合点{ok = 0;break;}}if (!ok){puts("-1");continue;}for (int i = 1;i < mx;++i)  //选择金币建图{for (int j = 1;j < tot;++j){if (d[i][go[i+1]] == d[i][gold[j]] + d[i+1][gold[j]])//如果某金币到起点终点距离之和等于起点到终点的距离{ans.G[i].push_back(gold[j]);//这个金币在最短路上,则在该金币和起点之间连一条线}}}printf("%d\n",ans.hungary(mx));}return 0;
}
/*
input 2 4
A.B.
***C2 4
A#B.
***C2 4
A#B.
*#*C3 4
A#B.
..*.
C...2 4
C#B.
***A3 5
.A**.
C*...
..B..output12-1122
*/

参考资料:
http://www.cnblogs.com/ylfdrib/archive/2010/08/18/1802817.html

这篇关于HDU 3468 Treasure Hunting(二分匹配+最短路)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/908621

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问