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

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

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 NM (2 ≤ NM ≤ 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,- 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


题目大意是给出一个n*m的图,从0,0出发 问怎样得到最高分

图中有三种点 0~9表示达到当前点可得分值 ‘*’为传送门 ‘#’为墙(不可达)

要求只能向右或者向下走

走到*时 可以选择传送 也可以不传送 *格子可以无数次传送

之后k行表示从上到下 从左到右每个*传送到的坐标x y


这是题目大意 如果没有*格子 就是一个裸的最长路 或者dp

由于存在*格子 因此可能有环 这样就没法直接bfs

因此先用邻接表存图 然后对于每个点跑Tarjan并把每个环都缩为一点

然后重新建图 新图中所有的环都变成了一个单点 就可以放心bfs了


代码量略大......

<span style="font-size:14px;">#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const double eps = 1e-8;

struct Edge
{
	int v,next;
};

//原本的权值
int val[2333];
//缩点后的权值
int nval[2333];
//第一次建图
int head[2333];
Edge eg[23333];
//第二次建图
int nhead[2333];
Edge neg[23333];
int tp;

//缩点后对应的新编号
int pos[2333];
//遍历时序
int dfn[2333],low[2333];
int vis[2333];
int dis[2333];
char mp[44][44];
int mx,tm,n,m,nid;
stack <int> s;

void Add(int u,int v)
{
	eg[tp].v = v;
	eg[tp].next = head[u];
	head[u] = tp++;
}

void Tarjan(int u)
{
	s.push(u);
	vis[u] = 1;
	dfn[u] = low[u] = tm++;
	int v;

	for(int i = head[u]; i != -1; i = eg[i].next)
	{
		v = eg[i].v;
		if(vis[v] == 0)
		{
			Tarjan(v);
			low[u] = min(low[u],low[v]);
		}
		else if(vis[v] == 1) low[u] = min(low[u],dfn[v]);
	}

	if(dfn[u] == low[u])
	{
		while(s.top() != u)
		{
			v = s.top();
			s.pop();
			pos[v] = nid;
<span style="white-space:pre">			</span>//把环中所有点的权值都加到缩成的点上
			nval[nid] += val[v];
			vis[v] = -1;
		}
		v = s.top();
		s.pop();
		pos[v] = nid;
		nval[nid] += val[v];
		vis[v] = -1;
		++nid;
	}
}

void init()
{
	memset(head,-1,sizeof(head));
	memset(val,0,sizeof(val));
	memset(nval,0,sizeof(nval));
	memset(vis,0,sizeof(vis));
	nid = tp = mx = tm = 0;
}

void spfa(int u)
{
	queue <int> q;
	q.push(u);

	memset(vis,0,sizeof(vis));
	memset(dis,0,sizeof(dis));
	vis[u] = 1;
	dis[u] = nval[u];
	mx = dis[u];
	int v;

	while(!q.empty())
	{
		u = q.front();
		q.pop();
		vis[u] = 0;

		for(int i = nhead[u]; i != -1; i = neg[i].next)
		{
			v = neg[i].v;
			if(dis[v] < dis[u]+nval[v])
			{
				dis[v] = dis[u]+nval[v];
				mx = max(mx,dis[v]);
				if(!vis[v])
				{
					q.push(v);
					vis[v] = 1;
				}
			}
		}
	}
}

int main()
{
	//fread();
	//fwrite();

	int t,x,y;
	scanf("%d",&t);

	while(t--)
	{
		init();
		scanf("%d%d",&n,&m);

		for(int i = 0; i < n; ++i)
		{
			scanf("%s",mp[i]);
		}

		for(int i = 0; i < n; ++i)
			for(int j = 0; j < m; ++j)
			{
				if(mp[i][j] == '#') continue;

				if(mp[i][j] == '*')
				{
					scanf("%d%d",&x,&y);
					if(mp[x][y] != '#');
					Add(i*m+j,x*m+y);
				}
				else val[i*m+j] = mp[i][j]-'0';

				if(i > 0 && mp[i-1][j] != '#') Add((i-1)*m+j,i*m+j);
				if(j > 0 && mp[i][j-1] != '#') Add(i*m+j-1,i*m+j);
			}
				
		for(int i = 0; i < n*m; ++i)
			if(!vis[i]) Tarjan(i);

		tp = 0;
		memset(nhead,-1,sizeof(nhead));

		for(int i = 0; i < n*m; ++i)
		{
			for(int j = head[i]; j != -1; j = eg[j].next)
			{
				if(pos[i] == pos[eg[j].v]) continue;
				neg[tp].v = pos[eg[j].v];
				neg[tp].next = nhead[pos[i]];
				nhead[pos[i]] = tp++;
			}
		}

		spfa(pos[0]);

		printf("%d\n",mx);
	}

	return 0;
}
</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值