hdoj 3468 Treasure Hunting 【BFS找所有最短路径上的点 + 最大流】



Treasure Hunting

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1635    Accepted Submission(s): 429


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
 



现在A个题好难啊,花了差不多3小时才KO。  不过靠自己AC确实挺有成就感的。



题意:有一个N*M的地图,地图上只会有A~Z  a~z  *   .  #这些字符。.表示该处是空的,可以走。*表示该处有宝藏,取过宝藏后会变成.。#表示该处不可走。现在你沿着A, B, C, ... Z, a, b, c,...z的路线(A只能到B,Z只能到a)来寻找宝藏,要求在每个小过程中始终走最短路且只有一次获得宝藏的机会,问你最多能得到的宝藏数。若中间出现路线中断——当前点不能到达下一目标点或者下一目标点不存在则输出-1。


BFS少了一个判断WA了1小时,醉了。。。



分析:两点之间的最短路径可能不是唯一的,因此不能单纯用BFS。因为我们不能保证每次BFS找到的最短路能够确保整个过程中解的最优性,也就是说当前的选择可能会影响到后面的选择。为了达到目的,匹配、最大流、dp是解决此类问题的方法。至于dp就不说了,应该可以写吧,但我不会o(╯□╰)o 。匹配和最大流差不多吧,这里只说下最大流。


思路:

对每个小过程,比如说A-B。 我用 use[i][j]表示字符A所在位置到(i, j)的最短距离

1,首先BFS(A, B),在BFS过程中记录遍历过的所有点的use值。

2,再BFS(B, A),沿着use值依次递减1的顺序查询(use值不能为0,不理解的自己画个图琢磨琢磨),BFS中找到的所有点全是A-B最短路上的点。



设置超级源点S,超级汇点T

1,S向A...Z a...z建边,容量为1,表示每个小过程只能选择一个宝藏;

2,对A-B而言(其它一样的),A向-B最短路上所有宝藏点建边,容量为1;

3,所有宝藏点向T建边,容量为1;


注意坑点:Z可以到a,有一个小过程中断就可以直接跳出输出-1。


AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#define MAXN 10000+10
#define MAXM 200000+10
#define INF 0x3f3f3f3f
using namespace std;
struct Dinic
{
    struct Edge{
        int from, to, cap, flow, next;
    };
    Edge edge[MAXM];
    int head[MAXN], cur[MAXN], edgenum;
    int dist[MAXN];
    bool vis[MAXN];
    void init(){
        edgenum = 0;
        memset(head, -1, sizeof(head));
    }
    void addEdge(int u, int v, int w)
    {
        Edge E1 = {u, v, w, 0, head[u]};
        edge[edgenum] = E1;
        head[u] = edgenum++;
        Edge E2 = {v, u, 0, 0, head[v]};
        edge[edgenum] = E2;
        head[v] = edgenum++;
    }
    bool BFS(int s, int t)
    {
        queue<int> Q;
        memset(dist, -1, sizeof(dist));
        memset(vis, false, sizeof(vis));
        dist[s] = 0, vis[s] = true;
        Q.push(s);
        while(!Q.empty())
        {
            int u = Q.front();
            Q.pop();
            for(int i = head[u]; i != -1; i = edge[i].next)
            {
                Edge E = edge[i];
                if(!vis[E.to] && E.cap > E.flow)
                {
                    dist[E.to] = dist[u] + 1;
                    if(E.to == t) return true;
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
        return false;
    }
    int DFS(int x, int a, int t)
    {
        if(x == t || a == 0) return a;
        int flow = 0, f;
        for(int &i = cur[x]; i != -1; i = edge[i].next)
        {
            Edge &E = edge[i];
            if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)
            {
                edge[i].flow += f;
                edge[i^1].flow -= f;
                flow += f;
                a -= f;
                if(a == 0) break;
            }
        }
        return flow;
    }
    int Maxflow(int s, int t)
    {
        int flow = 0;
        while(BFS(s, t))
        {
            memcpy(cur, head, sizeof(head));
            flow += DFS(s, INF, t);
        }
        return flow;
    }
};
Dinic dinic;

int N, M, S, T;
char Map[110][110];
int use[110][110];
bool have[110][110];
struct Node{
    int x, y;
};
bool judge(int x, int y){
    return x >= 0 && x < N && y >= 0 && y < M && Map[x][y] != '#' && !have[x][y];
}
int point(int x, int y){
    return x*M+y+1;
}
int endx, endy;
bool bfs_short(int sx, int sy, char goal)
{
    int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
    queue<Node> Q;
    Node now, next;
    now.x = sx, now.y = sy;
    Q.push(now);
    memset(use, 0, sizeof(use));
    memset(have, false, sizeof(have));
    have[now.x][now.y] = true;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(Map[now.x][now.y] == goal)
        {
            endx = now.x; endy = now.y;
            return true;
        }
        for(int k = 0; k < 4; k++)
        {
            next.x = now.x + Move[k][0];
            next.y = now.y + Move[k][1];
            if(judge(next.x, next.y))
            {
                have[next.x][next.y] = true;
                use[next.x][next.y] = use[now.x][now.y] + 1;
                Q.push(next);
            }
        }
    }
    return false;
}
void bfs_find(int sx, int sy, char goal, int num)
{
    int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
    queue<Node> Q;
    Node now, next;
    now.x = sx, now.y = sy;
    Q.push(now);
    memset(have, false, sizeof(have));
    have[now.x][now.y] = true;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if(Map[now.x][now.y] == goal)
            break;
        for(int k = 0; k < 4; k++)
        {
            next.x = now.x + Move[k][0];
            next.y = now.y + Move[k][1];
            if(judge(next.x, next.y) && use[next.x][next.y] && use[next.x][next.y] + 1 == use[now.x][now.y])
            {//注意判断条件
                Q.push(next);
                have[next.x][next.y] = true;
                if(Map[next.x][next.y] == '*')
                {
                    //Map[next.x][next.y] = '.';//注意用过后 不能去掉 可能会被下一个过程所用的
                    dinic.addEdge(num, point(next.x, next.y), 1);//A...Z a...z向宝藏点建边
                }
            }
        }
    }
}
char ss, es, ms;
int bx, by;//记录起点
void solve()
{
    ss = 'z'+1, es = 'A'-1;
    dinic.init();
    S = 0, T = N*M+1;
    for(int i = 0; i < N; i++)
    {
        scanf("%s", Map[i]);
        for(int j = 0; j < M; j++)
        {
            if(Map[i][j] >= 'A' && Map[i][j] <= 'Z' || Map[i][j] >= 'a' && Map[i][j] <= 'z')
            {
                if(Map[i][j] > es)
                    es = Map[i][j];
                if(Map[i][j] < ss)
                {
                    bx = i;
                    by = j;
                    ss = Map[i][j];
                }
            }
            if(Map[i][j] == '*')
                dinic.addEdge(point(i, j), T, 1);//宝藏点向T建边
        }
    }
    bool flag = true;
    while(ss != es)
    {
        if(ss == 'Z')//注意这里 坑
            ms = 'a';
        else
            ms = ss + 1;
        if(bfs_short(bx, by, ms))
        {
            bfs_find(endx, endy, ss, point(bx, by));//找最短路上所有点
            dinic.addEdge(S, point(bx, by), 1);//S向A...Z a...z建边
        }
        else//查找失败 跳出
        {
            flag = false;
            break;
        }
        ss = ms;
        bx = endx, by = endy;
    }
    if(flag)
        printf("%d\n", dinic.Maxflow(S, T));
    else//失败
        printf("-1\n");
}
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值