【强连通分量】Instantaneous Transference

Description

It was long ago when we played the game Red Alert. There is a magic function for the game objects
which is called instantaneous transfer. When an object uses this magic function, it will be
transferred to the specified point immediately, regardless of how far it is.

Now there is a mining area, and you are driving an ore-miner truck. Your mission is to take the
maximum ores in the field.

The ore area is a rectangle region which is composed by n × m small squares, some of the squares
have numbers of ores, while some do not. The ores can't be regenerated after taken.

The starting position of the ore-miner truck is the northwest corner of the field. It must move to
the eastern or southern adjacent square, while it can not move to the northern or western adjacent
square. And some squares have magic power that can instantaneously transfer the truck to a certain
square specified. However, as the captain of the ore-miner truck, you can decide whether to use this
magic power or to stay still. One magic power square will never lose its magic power; you can use
the magic power whenever you get there.

Input

The first line of the input is an integer T which indicates the number of test cases.

For each of the test case, the first will be two integers N, M (2 ≤ N, M ≤ 40).

The next N lines will describe the map of the mine field. Each of the N lines will be a string that
contains M characters. Each character will be an integer X (0 ≤ X ≤ 9) or a '*' or a '#'. The
integer X indicates that square has X units of ores, which your truck could get them all. The '*'
indicates this square has a magic power which can transfer truck within an instant. The '#' indicates
this square is full of rock and the truck can't move on this square. You can assume that the starting
position of the truck will never be a '#' square.

As the map indicates, there are K '*' on the map. Then there follows K lines after the map. The next
K lines describe the specified target coordinates for the squares with '*', in the order from north
to south then west to east. (the original point is the northwest corner, the coordinate is formatted
as north-south, west-east, all from 0 to N - 1,M - 1).

Output

For each test case output the maximum units of ores you can take.  

Sample Input
1
2 2
11
1*
0 0

Sample Output
3

先强连通分量缩点,然后SPFA之。

注意数组清零的问题。

Accode:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>

const int maxR = 50;
const int maxN = 2010;
const int SIZE = 0xffff;

struct vec
{
    int x, y;
    vec() {}
    vec(int x, int y): x(x), y(y) {}
};
struct Edge {int v; Edge *next;};

Edge *edge[maxN];
char mp[maxR][maxR];
bool marked[maxR][maxR], visit[maxN];
int belong[maxR][maxR];
int DFN[maxR][maxR], Low[maxR][maxR];
int ord[maxR][maxR], cnt[maxN];
int dist[maxN], q[SIZE + 1];
vec to[maxN], stack[maxN];
int t, n, m, K, top, Index, Bcnt, ans, f, r;

void tarjan(int x, int y)
{
    if (mp[x][y] == '#') return;
    DFN[x][y] = Low[x][y] = ++Index;
    stack[++top] = vec(x, y);
    marked[x][y] = 1;
    if (y < m - 1 && mp[x][y + 1] != '#')
    {
        int u = x, v = y + 1;
        if (!DFN[u][v])
        {
            tarjan(u, v);
            if (Low[u][v] < Low[x][y])
                Low[x][y] = Low[u][v];
        }
        else if (marked[u][v] && DFN[u][v] < Low[x][y])
	//开始在这里打错一个变量,
	//把marked[u][v]打成了marked[x][y],调了很久。
            Low[x][y] = DFN[u][v];
    }
    if (x < n - 1 && mp[x + 1][y] != '#')
    {
        int u = x + 1, v = y;
        if (!DFN[u][v])
        {
            tarjan(u, v);
            if (Low[u][v] < Low[x][y])
                Low[x][y] = Low[u][v];
        }
        else if (marked[u][v] && DFN[u][v] < Low[x][y])
            Low[x][y] = DFN[u][v];
    }
    if (mp[x][y] == '*')
    {
        int u = to[ord[x][y]].x, v = to[ord[x][y]].y;
        if (mp[u][v] != '#') //注意要有边才能枚举。
        {
            if (!DFN[u][v])
            {
                tarjan(u, v);
                if (Low[u][v] < Low[x][y])
                    Low[x][y] = Low[u][v];
            }
            else if (marked[u][v] &&
                     DFN[u][v] < Low[x][y])
                Low[x][y] = DFN[u][v];
        }
    }
    if (DFN[x][y] == Low[x][y])
    {
        ++Bcnt;
        vec tmp;
        do
        {
            tmp = stack[top--];
            marked[tmp.x][tmp.y] = 0;
            belong[tmp.x][tmp.y] = Bcnt;
            if (isdigit(mp[tmp.x][tmp.y]))
                cnt[Bcnt] += mp[tmp.x][tmp.y] - '0';
        } while (tmp.x != x || tmp.y != y);
    }
    return;
}

inline void init()
{
    memset(marked, 0, sizeof marked);
    memset(visit, 0, sizeof visit);
    memset(belong, 0, sizeof belong);
    memset(cnt, 0, sizeof cnt);
    memset(DFN, 0, sizeof DFN);
    memset(edge, 0, sizeof edge);
    K = n = m = top = Index = Bcnt = ans = f = r = 0;
    return;
}

inline int Spfa()
{
    memset(dist, ~0x3f, sizeof dist);
    int ans = cnt[belong[0][0]];
    dist[belong[0][0]] = ans;
    visit[belong[0][0]] = 1;
    q[r++] = belong[0][0];
    f &= SIZE;
    while (f != r)
    {
        int u = q[f++];
        f &= SIZE;
        visit[u] = 0;
        for (Edge *p = edge[u]; p; p = p -> next)
        if (dist[u] + cnt[p -> v] > dist[p -> v])
        {
            int v = p -> v;
            if ((dist[v] = dist[u] + cnt[v]) > ans)
                ans = dist[v];
            if (!visit[v])
            {
                visit[v] = 1;
                q[r++] = v;
                r &= SIZE;
            }                
        }
    }
    return ans;
}

inline void insert(int u, int v)
{
    if (u == v) return;
    for (Edge *p = edge[u]; p; p = p -> next)
        if (p -> v == v) return;
	//注意重新建图时的判重,以减少枚举量。
    Edge *p = new Edge;
    p -> v = v;
    p -> next = edge[u];
    edge[u] = p;
    return;
}

int main()
{
    freopen("Instantaneous_Transference.in", "r", stdin);
    freopen("Instantaneous_Transference.out", "w", stdout);
    scanf("%d", &t);
    for (; t; --t)
    {
        init();
        scanf("%d%d\n", &n, &m);
        for (int i = 0; i < n; ++i)
        {
            gets(mp[i]);
            for (int j = 0; j < m; ++j)
            if (mp[i][j] == '*')
                ord[i][j] = K++;
        }
        for (int i = 0; i < K; ++i)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            to[i] = vec(x, y);
        }
        tarjan(0, 0);
        for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
        {
            if (j < m - 1 && mp[i][j + 1] != '#')
                insert(belong[i][j], belong[i][j + 1]);
            if (i < n - 1 && mp[i + 1][j] != '#')
                insert(belong[i][j], belong[i + 1][j]);
            if (mp[i][j] == '*' &&
                mp[to[ord[i][j]].x][to[ord[i][j]].y] != '#')
                insert(belong[i][j],
                       belong[to[ord[i][j]].x][to[ord[i][j]].y]);
		//注意实际无边的情况。
        }
        printf("%d\n", Spfa());
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值