您的位置:首页 > Web前端

【连通图|强连通分量+最长路】POJ-3592 Instantaneous Transference

2015-03-12 21:55 495 查看
Instantaneous Transference

Time Limit: 5000MS Memory Limit: 65536K

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

题意: 给出一个有向图,其中每个点向自己下面的和右边的那个点有边相连。’*’号的点可以进行瞬间转移(可以选择转移或者不转移),每个点有点权且不为负。’#’号的点是不能到达的点。每到一个点获得它的权值且仅一次,求出能获得的最大点权和。

思路: 其实本来我是拒绝用强连通分量做这道题的,但是导演说,做完加特技……Duang~

看了样例就会发现这个求最长路的题目存在正环。那么用强连通分量可以把所有的环缩成一个点,然后重新建图,跑一遍spfa即可。

注意: ‘#’结点不向其它的点连边即可,一开始我是其它的点不向’#’连边,WA了,我猜是因为有些瞬间转移牵扯到了’#’。

代码如下:

/*
* ID: j.sure.1
* PROG:
* LANG: C++
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 2e3+5, M = 3*N;
int r, c;
int n;
int head
, dfn
, scc_id
, sum
;
int scc_cnt, deep, tot, poi;
struct Edge {
int v, next;
Edge(){}
Edge(int _v, int _next):
v(_v), next(_next){}
}e[M];

void init()
{
poi = tot = deep = scc_cnt = 0;
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(scc_id, 0, sizeof(scc_id));
memset(sum, 0, sizeof(sum));
}

void add(int u, int v)
{
e[tot] = Edge(v, head[u]);
head[u] = tot++;
}

int w
, trans
;
void input()
{
char str[50];
for(int i = 0; i < r; i++) {
scanf("%s", str);
for(int j = 0; j < c; j++) {
int u = i * c + j;
if(str[j] == '*') {
w[u] = 0;
trans[poi++] = u;
}
else if(str[j] == '#') {
w[u] = -1;
}
else {
w[u] = str[j] - '0';
}
}
}
}

void build()
{
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
int u = i * c + j;
if(~w[u]) {
if(j != c-1) add(u, u+1);
if(i != r-1) add(u, u+c);
}
}
}
int x, y;
for(int i = 0; i < poi; i++) {
scanf("%d%d", &x, &y);
int v = x * c + y;
add(trans[i], v);
}
}

stack <int> s;
int dfs(int u)
{
int lowu = dfn[u] = ++deep;
s.push(u);
for(int i = head[u]; ~i; i = e[i].next) {
int v = e[i].v;
if(!dfn[v]) {
int lowv = dfs(v);
lowu = min(lowu, lowv);
}
else if(!scc_id[v]) {
lowu = min(lowu, dfn[v]);
}
}
if(lowu == dfn[u]) {
scc_cnt++;
while(1) {
int x = s.top(); s.pop();
scc_id[x] = scc_cnt;//nmuber from 1
sum[scc_cnt] += w[x];
if(x == u) break;
}
}
return lowu;
}

void tarjan()
{
for(int i = 0; i < n; i++) {
if(!dfn[i]) dfs(i);
}
}

vector <int> ef
;
void rebuild()
{
for(int i = 1; i <= scc_cnt; i++) {
ef[i].clear();
}
for(int u = 0; u < n; u++) {
for(int i = head[u]; ~i; i = e[i].next) {
int v = e[i].v;
if(scc_id[u] != scc_id[v]) {
ef[scc_id[u]].PB(scc_id[v]);
}
}
}
}

int dis
;//!
bool inq
;//!
void spfa(int st)
{
memset(dis, 0, sizeof(dis));
queue <int> q;
q.push(st);
inq[st] = true;
dis[st] = sum[st];
while(!q.empty()) {
int u = q.front(); q.pop();
inq[u] = false;
size_t len = ef[u].size();
for(size_t i = 0; i < len; i++) {
int v = ef[u][i];
if(dis[v] < dis[u] + sum[v]) {
dis[v] = dis[u] + sum[v];
if(!inq[v]) {
q.push(v);
inq[v] = true;
}
}
}
}
}

int solve()
{
rebuild();
spfa(scc_id[0]);
int ret = 0;
for(int i = 1; i <= scc_cnt; i++) {
ret = max(ret, dis[i]);
}
return ret;
}

int main()
{
#ifdef J_Sure
freopen("000.in", "r", stdin);
//freopen("999.out", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &r, &c);
n = r * c;
init();
input();
build();
tarjan();
int ans = solve();
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: