[题解] poj 3592 Instantaneous Transference (tarjan强连通分量 + SPFA最长路径)
- 传送门 -
http://poj.org/problem?id=3592
| Time Limit: 5000MS | | Memory Limit: 65536K |
| Total Submissions: 6863 | | Accepted: 1552 |
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
- 代码 -
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1600 + 5;
vector<int> SCC[N];
bool B[N];
int QUE[N];
int DFN[N], LOW[N], STK[N];
int BL[N], SZ[N], ANS[N];
int HD[N], NXT[N*3], TO[N*3];
int A[N];
int n, m, maxn, head, tail, T;
int sz, cnt, tot, nu, tp;
char Ch[45][45], ch;
int pos (int x, int y) { return x*m + y; }
void init() {
sz = cnt = tot = nu = tp = 0;
memset(HD, 0, sizeof (HD));
memset(BL, 0, sizeof (BL));
memset(DFN, 0, sizeof (DFN));
memset(ANS, 0, sizeof (ANS));
memset(B, false, sizeof (B));
}
void add(int x, int y) {
TO[++sz] = y; NXT[sz] = HD[x]; HD[x] = sz;
}
void get(int &x) {
char ch;
do {
ch = getchar();
if (ch == '*' || ch == '#' || (ch >= '0' && ch <= '9'))
break;
}while(true);
if (ch == '*') x = -2, nu ++;
else if (ch == '#') x = -1;
else x = ch - '0';
}
void tarjan(int x) {
int v;
LOW[x] = DFN[x] = ++tot;
STK[++tp] = x;
for (int i = HD[x]; i; i = NXT[i]) {
v = TO[i];
if (!DFN[v]) { tarjan(v); LOW[x] = min(LOW[x], LOW[v]); }
else if (!BL[v]) LOW[x] = min(LOW[x], DFN[v]);
}
if (DFN[x] == LOW[x]) {
cnt ++; SZ[cnt] = 0;
do {
v = STK[tp--];
BL[v] = cnt;
SCC[cnt].push_back(v);
if (A[v] != -2) SZ[cnt] += A[v]; //特判*的情况
}while (v != x);
}
}
void SPFA() {
QUE[tail = 1] = BL[0];
head = 0; ANS[BL[0]] = SZ[BL[0]];
while (head < tail) {
int i = QUE[++head], len = SCC[i].size();
maxn = max(maxn, ANS[i]); //对可以到达的点进行比较
B[i] = false;
for (int j = 0; j < len; ++j) {
for (int k = HD[SCC[i][j]]; k; k = NXT[k]) {
int v = BL[TO[k]];
if (v != i && ANS[i] + SZ[v] > ANS[v]) {
ANS[v] = ANS[i] + SZ[v];
if (!B[v]) { B[v] = true; QUE[++tail] = v; }
}
}
}
}
}
int main() {
scanf("%d", &T);
for (int i = 1; i <= T; ++i) {
init();
scanf("%d%d", &n, &m);
ch = getchar();
maxn = n * m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
int p = pos(i, j);
get(A[p]);
if (A[p] == -1) continue;
if (p % m != 0 && A[p - 1] != -1) add(p - 1, p);
if (p >= m && A[p - m] != -1) add(p - m, p);
}
for (int i = 0, x, y; i < maxn; ++i)
if (A[i] == -2) {
scanf("%d%d", &x, &y);
int p = pos(x, y);
if (A[p] != -1 && p < maxn) add(i, p);
}
for (int i = 0; i < maxn; ++i)
if (!DFN[i] && A[i] != -1) tarjan(i); //特判#的情况
maxn = 0;
SPFA();
printf("%d\n", maxn);
for (int i = 1; i <= cnt; ++i) SCC[i].clear();
}
return 0;
}