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

来源:互联网 发布:淘宝晚装短款晚礼服 编辑:程序博客网 时间:2024/05/02 06:19
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 4A.B.***C2 4A#B.***C
 

Sample Output
12
 

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*N    bool 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 4A.B.***C2 4A#B.***C2 4A#B.*#*C3 4A#B...*.C...2 4C#B.***A3 5.A**.C*.....B..output    1    2    -1    1    2    2*/

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