poj3592Instantaneous Transference

Instantaneous Transference
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7067 Accepted: 1609

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

题意:有一个N*M的矩阵地图,矩阵中用了多种字符代表不同的地形,如果是数字X,则表示该区域为矿区,有X单位的矿产。如果是"*",则表示该区域为传送点,并且对应唯一一个目标坐标。如果是"#",,则表示该区域为山区,矿车不能进入。现在矿车的出发点在坐标(0,0)点。矿车只能向右走、向下走或是遇到传送点的时候可以传送到指定位置。矿车最多能采到多少矿。

思路:先进行缩点,建立一个有向无环图,然后再用spfa即可。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
using namespace std;
const int N=2000;
const int inf=0x3f3f3f3f;
int dir[2][2]={1,0,0,1};
int dfn[N],low[N],belong[N];
bool instack[N];
int w[N];
int sw[N];
int head[N],hh[N];
int vis[N];
int dis[N];
int in[N],out[N];
char str[55][55];
int cnt,index,num;
struct node
{
    int v,next;
}e[500000],ee[500000];
stack<int>s;
int m,n;
int ans;
void addedge(int u,int v)
{
    e[num].v=v;
    e[num].next=head[u];
    head[u]=num++;
}
void addtree(int u,int v)
{
    ee[num].v=v;
    ee[num].next=hh[u];
    hh[u]=num++;
}
void init()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    memset(instack,false,sizeof(instack));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(head,-1,sizeof(head));
    memset(hh,-1,sizeof(hh));
    memset(w,0,sizeof(w));
    memset(sw,0,sizeof(sw));
    num=cnt=index=0;
    while(!s.empty())
        s.pop();
}
void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    instack[u]=true;
    int v;
    s.push(u);
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        v=e[i].v;
        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])
    {
        cnt++;
        do{
            v=s.top();
            s.pop();
            instack[v]=false;
            belong[v]=cnt;
            sw[cnt]+=w[v];
        }while(u!=v);
    }
}
int spfa()
{
    memset(vis,0,sizeof(vis));
    queue<int>q;
    while(!q.empty())q.pop();
    memset(dis,0,sizeof(dis));
    vis[belong[0]]=1;
    q.push(belong[0]);
    dis[belong[0]]=sw[belong[0]];
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=hh[u];i!=-1;i=ee[i].next)
        {
            int v=ee[i].v;
            if(dis[v]<dis[u]+sw[v])
            {
                dis[v]=dis[u]+sw[v];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    int ans=-1;
    for(int i=1;i<=cnt;i++)
        ans=max(ans,dis[i]);
    return ans;

}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        init();
        int i,j;
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }

        int u,v;
       for(i = 0; i < n; ++i)
        {
            for(j = 0; j < m; ++j)
            {
                if(str[i][j] != '#')
                {
                    if(i+1 < n && str[i+1][j] != '#')   
                        addedge(i*m+j,(i+1)*m+j);
                    if(j+1 < m && str[i][j+1] != '#')   
                        addedge(i*m+j,i*m+j+1);

                    w[i*m+j] =str[i][j] - '0';

                    if(str[i][j] == '*')
                    {
                        w[i*m+j] = 0;
                        scanf("%d%d",&u,&v);
                        if(str[u][v] != '#')
                            addedge(i*m+j,u*m+v);
                    }
                }
            }
        }
        for(i=0;i<n*m;i++)
        {
            if(!dfn[i])tarjan(i);
        }
        num=0;
        for(i=0;i<n*m;i++)
        {
            for(j=head[i];j!=-1;j=e[j].next)
            {
                v=e[j].v;
                if(belong[i]!=belong[v])
                {
                    addtree(belong[i],belong[v]);
                }
            }
        }
        printf("%d\n",spfa());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值