关闭

【强连通分量】Instantaneous Transference

标签: insertintegereachcharacteroutputfunction
609人阅读 评论(0) 收藏 举报
分类:
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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:261984次
    • 积分:4315
    • 等级:
    • 排名:第6985名
    • 原创:181篇
    • 转载:2篇
    • 译文:0篇
    • 评论:32条
    文章分类
    最新评论