POJ 3592 Instantaneous Transference

Instantaneous Transference
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4741 Accepted: 1029

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
  做这题思路还算清晰,缩点+最长路,不断的tle,在求强连通分量的时候出错了,这题构建出的图,可能不是连通图的,刚开始以为是连通图呢。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cstdlib>
#define N 1610
using namespace std;
struct num
{
	int end,next;
}a[N*1000];
int pt[N];
int init[50][50],trans[50][50],pre_ener[N];
int low[N],dfn[N],chan[N],end_ener[N],dis[N],id,pos,cou,Top;
bool instack[N];
int n,m;
stack<int> sta;
int main()
{
	//freopen("data1.in","r",stdin);
    void pre_build();
	void end_build();
    void Tarjan(int u);
	int spfa();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i=0; i<=n-1;i++)
        {
			char s1[100];
            scanf("%s",s1);
            int l=strlen(s1);
            for(int j=0; j<=l-1; j++)
            {
                if(s1[j]>='0'&&s1[j]<='9')
                {
                    init[i][j] = (int)(s1[j]-'0');
                    pre_ener[i * m + j + 1] = init[i][j];
                }else if(s1[j]=='#')
                {
                    init[i][j] = -1;
                    pre_ener[i*m+j+1] = 0;
                }else if(s1[j]=='*')
                {
                    init[i][j] = -2;
                    pre_ener[i*m+j+1]=0;
                }
            }
        }
		for(int i=0;i<=n-1;i++)
		{
			for(int j=0;j<=m-1;j++)
			{
				if(init[i][j]==-2)
				{
					int x,y;
					scanf("%d %d",&x,&y);
					if(init[x][y]!=-1)
					{
						trans[i][j] = m * x + y + 1;
					}else 
					{
						trans[i][j] = -1;
					}
				}
			}
		}
        pre_build();
		memset(low,0,sizeof(low));
		memset(dfn,0,sizeof(dfn));
		memset(instack,false,sizeof(instack));
		cou = id= 0;
		for(int i=1;i<=n*m;i++)
		{
			if(!dfn[i])
			{
		       while(!sta.empty())
		       {
		        	sta.pop();
		       }
               Tarjan(i);
			}
		}
        end_build();
        int ans = spfa();
        printf("%d\n",ans);
    }
    return 0;
}
inline void addeage(int x,int y)
{
	a[Top].end = y;
	a[Top].next = pt[x];
	pt[x] = Top;
	Top++;
}
void pre_build()
{
	memset(pt,-1,sizeof(pt));
	Top = 0;
    for(int i=0 ;i<=n-1; i++)
    {
        for(int j=0;j<=m-1;j++)
        {
            if(init[i][j] == -1)
            {
                continue;
            }
            if(j<m-1&&init[i][j+1]!=-1)
            {
                int x = i * m + j + 1;
                int y = i * m + j + 2;
                addeage(x,y);
            }
            if(i<n-1&&init[i+1][j]!=-1)
            {
                int x = i * m + j + 1;
                int y = (i+1) * m + j + 1;
                addeage(x,y);
            }
			if(init[i][j]==-2&&trans[i][j]!=-1)
			{
				int x = i * m + j + 1;
				int y = trans[i][j];
				addeage(x,y);
			}
        }
    }
}
void Tarjan(int u)
{
    low[u] = dfn[u] = ++cou;
    sta.push(u);
    instack[u] = true;
    for(int i=pt[u]; i!=-1; i=a[i].next)
    {
        int v = a[i].end;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u] = min(low[u],low[v]);
        }else if(instack[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u])
    {
        id++;
        int x,s=0;
        do
        {
            x = sta.top();
            sta.pop();
            instack[x] = false;
			if(x == 1)
			{
				pos = id;
			}
			s += pre_ener[x];
            chan[x] = id;
        }while(x!=u);
		end_ener[id] = s;
    }
}
void end_build()
{
	memset(pt,-1,sizeof(pt));
	Top=0;
	for(int i=0; i<=n-1; i++)
	{
		for(int j=0; j<=m-1; j++)
		{
			if(init[i][j] == -1)
			{
				continue;
			}
			if(j<m-1&&init[i][j+1]!=-1)
			{
				int x = i * m + j + 1;
				int y = i * m + j + 2;
				x = chan[x]; y=chan[y];
			    if(x!=y)
				{
				   addeage(x,y);
				}
			}
			if(i<n-1&&init[i+1][j]!=-1)
			{
				int x = i * m + j + 1;
				int y = (i+1) * m  + j + 1;
				x = chan[x]; y = chan[y];
				if(x!=y)
				{
				  addeage(x,y);
				}
			}
			if(init[i][j] == -2&&trans[i][j]!=-1)
			{
				int x = i * m + j + 1;
				int y = trans[i][j];
				x= chan[x]; y = chan[y];
				if(x != y)
				{
					addeage(x,y);
				}
			}
		}
	}
}
int spfa()
{
	queue<int> que;
	memset(instack,false,sizeof(instack));
	memset(dis,-1,sizeof(dis));
	que.push(pos);
	instack[pos] = true;
	dis[pos] = end_ener[pos];
	while(!que.empty())
	{
		int x = que.front();
		que.pop();
		instack[x] = false;
		for(int i=pt[x]; i!=-1; i=a[i].next)
		{
			int v = a[i].end;
			if((dis[x] + end_ener[v]) > dis[v])
			{
				dis[v] = dis[x] + end_ener[v];
				if(!instack[v])
				{
					instack[v] = true;
					que.push(v);
				}
			}
		}
	}
	int Max=-1;
	for(int i=1; i<=id; i++)
	{
		Max = max(Max,dis[i]);
	}
	return Max;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值