POJ 3592 Instantaneous Transference 缩点 拓扑图DP

棋盘有些格子有权值,有些不可走,还有一些是传送门可以直接传送到给定点。剩余的均只能向右或向下走。求对角最长路。

首先,如果传送门产生了环,那么连通分量内的所有点都是可达的——点可以重复走,所以缩点,新点的权就是原分量内的权和。
然后的DAG跑最长路的很简单了。一个DP即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(i=j;i<k;++i)
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define ms(i,j) memset(i,j,sizeof(i))
#define id(i,j) ((i)*m+j)
const int N = 1601, M = 80000, inf = 2147483647;

struct Graph {
    int h[N], v[M], p[M], w[N], cnt;
    int dfn[N], low[N], q[M], belong[N], stack[N], top, scc, ts;
    int dp[N];

    void init() {
        cnt = scc = ts = top = 0;
        ms(h, 0); ms(w, 0); ms(dp, -1); ms(belong, 0); ms(dfn, 0);
    }

    void add(int a, int b) { v[++cnt] = b; p[cnt] = h[a]; h[a] = cnt; }

    void dfs(int x) {
        int i;
        dfn[x] = low[x] = ++ts;
        stack[++top] = x;
        for(int i = h[x]; i; i = p[i])
            if (!dfn[v[i]]) {
                dfs(v[i]);
                low[x] = min(low[x], low[v[i]]);
            } else if (!belong[v[i]])
                low[x] = min(low[x], dfn[v[i]]);
        if (dfn[x] == low[x]) {
            ++scc;
            do {
                i = stack[top--];
                belong[i]=scc;
            } while (x != i);
        }
    }

    int query(int s) {
        if (~dp[s]) return dp[s];
        int ma = 0, i;
        for (i = h[s]; i; i = p[i])
            ma = max(ma, query(v[i]));
        return dp[s] = w[s] + ma;
    }
} g, h;

int main() {
    char mp[40][40];
    int t, i, j, a, b, m, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        g.init(); h.init();
        rep(i,0,n) scanf("%s", mp[i]);
        rep(i,0,n) rep(j,0,m) {
            if (mp[i][j] == '#') continue;
            if (i + 1 < n && mp[i + 1][j] != '#') g.add(id(i, j), id(i + 1, j));
            if (j + 1 < m && mp[i][j + 1] != '#') g.add(id(i, j), id(i, j + 1));
            if (mp[i][j] == '*') {
                scanf("%d%d", &a, &b);
                if (mp[a][b] != '#') g.add(id(i, j), id(a, b));
            }
        }
        rep(i,0,n * m) if (!g.dfn[i]) g.dfs(i);
        rep(i,0,n * m) 
            for (j = g.h[i]; j; j = g.p[j])
                if (g.belong[i] != g.belong[g.v[j]])
                    h.add(g.belong[i], g.belong[g.v[j]]);
        rep(i,0,n) rep(j,0,m) if (mp[i][j] >= '0' && mp[i][j] <= '9')
            h.w[g.belong[id(i, j)]] += mp[i][j] - '0';
        printf("%d\n", h.query(g.belong[0]));
    }
    return 0;
}

Instantaneous Transference

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6265 Accepted: 1411

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

Source

South Central China 2008 hosted by NUDT

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值