【强连通分量】Instantaneous Transference

原创 2012年03月29日 22:18:33
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;
}

poj 3592 Instantaneous Transference(强连通分量+dp)

题意:给出一个n×m的矩阵,每个位置可能会有矿,最开始矿车在(0,0)的位置,每次矿车只能向下或者向左走,如果这个位置的字符是'0'~'9'的数字,代表该位置有相应的矿,如果是‘*’,表示这是一个传送...
  • qian99
  • qian99
  • 2013年10月15日 13:05
  • 642

POJ3592 Instantaneous Transference【强连通分量】【最长路】

题目大意: 有一个N*M的矩阵地图,矩阵中用了多种字符代表不同的地形,如果是数字X(0~9),则表示 该区域为矿区,有X单位的矿产。如果是"*",则表示该区域为传送点,并且对应唯一一个目标 坐标。如果...

poj 3592 Instantaneous Transference (借助强连通分量求缩点在建图spfa求最长路)

题目链接:http://poj.org/problem?id=3592 Description It was long ago when we played the game Red Al...
  • txgANG
  • txgANG
  • 2016年11月30日 20:20
  • 145

POJ 3592 Instantaneous Transference 强连通分量+缩点+DP

这是一道变形题,不得不说是好题啊 题目大意是,有一张n*m的地图,每个点上可能是数字,代表矿石的数目,可能是*,表示一个传送阵,送往某个坐标,可能是#,代表不通。每次矿车只能往右方或者下方走一格,那...

poj 3592 Instantaneous Transference(强连通分量)

题目链接 Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total S...

【POJ 3592】 Instantaneous Transference(强连通缩点+最长路)

【POJ 3592】 Instantaneous Transference(强连通缩点+最长路) Instantaneous Transference Time Limit...

poj 3592 Instantaneous Transference 强连通+缩点+最长路

题目链接:http://poj.org/problem?id=3592 题目大意 :坦克一开始在(0,0),只可以向右边或下边走,走过的时候可以获得该格子上面的矿物(只能采集一次,之后矿物消失)。 ...

【POJ】3592 Instantaneous Transference 强连通+最长路

Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissi...

【强连通缩点+最长路】Instantaneous Transference

Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissio...

强连通分量的Kosaraju算法实现

  • 2014年04月14日 20:41
  • 979KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【强连通分量】Instantaneous Transference
举报原因:
原因补充:

(最多只允许输入30个字)