Instantaneous Transference--POJ3592Tarjan縮點+搜索

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.node

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.markdown

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.less

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.ide

Input

The first line of the input is an integer T which indicates the number of test cases.ui

For each of the test case, the first will be two integers N, M (2 ≤ N, M ≤ 40).this

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.spa

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). code

Output

For each test case output the maximum units of ores you can take.  orm

Sample Input

1
2 2
11
1*
0 0ip

Sample Output

3

Source

South Central China 2008 hosted by NUDT

題意:在一個礦區,你駕駛着一輛採礦的卡車,你的任務是採集最大數量的礦石,礦區是一個長方形的區域,包含n*m個的小方塊,有些礦區有礦石,有的沒有,礦石採完後不能再生,採礦車的起始位置在西北角(0,0),它只能向東面或者南面相鄰的方格,其中的一些方塊有魔法,能將礦車瞬間移動到指定的位置,做爲駕駛員,你能夠決定是否使用這個魔法,魔法不會消失。

思路:首先是建圖,建完圖發現會造成強連通份量,因此咱們先進行縮點,縮完點後會造成一個DAG圖,而後進行DFS搜索一邊就能夠,不過在建圖的時候要注意傳送到外面的點也要建圖

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>

using namespace std;

const int Max = 2000;

typedef struct node
{
    int v,next;
}Line;

Line Li[Max*1000];

int Head1[Max],Head2[Max],top;

int dfn[Max],low[Max],vis[Max],dep,pre[Max];

int Dp[Max],num;

char str[50][50];

int va[Max],a[Max];

int T,n,m;

int dir[][2]={{0,1},{1,0}};

stack<int>S;

void Init()
{
    memset(Head1,-1,sizeof(Head1));

    memset(Head2,-1,sizeof(Head2));

    memset(vis,0,sizeof(vis));

    memset(va,0,sizeof(va));

    memset(a,0,sizeof(a));

    memset(pre,-1,sizeof(pre));

    dep = 0 ;num =  0;
}

void AddEdge1(int u,int v)
{
    Li[top].v = v; Li[top].next =Head1[u];

    Head1[u] = top++;
}

void AddEdge2(int u,int v)
{
    Li[top].v = v; Li[top].next = Head2[u];

    Head2[u] = top++;
}


bool Judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&str[x][y]!='#')
    {
        return true;
    }
    return false;
}

void DFS(int x,int y)
{
    for(int i = 0;i<2;i++)
    {
        int Fx = x+dir[i][0];

        int Fy = y+dir[i][1];

        if(Judge(Fx,Fy))
        {
            AddEdge1(x*m+y,Fx*m+Fy);
        }
    }
}

void Tarjan(int u) //Tarjan強連通縮點
{
    dfn[u] = low[u] =dep++;

    vis[u]=1;

    S.push(u);

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

            low[u] = min(low[u],low[Li[i].v]);
        }
    }
    if(dfn[u]==low[u])
    {
        while(!S.empty())
        {
            int v=S.top();

            S.pop();

            pre[v] = num;

            vis[v] = 2;

            a[num]+=va[v];

            if(u==v)
            {
                break;
            }
        }
        num++;
    }
}
int dfs(int u)//搜索最大值
{
    if(!vis[u])
    {
        vis[u]=1;

        int ans=0;

        for(int i=Head2[u];i!=-1;i=Li[i].next)
        {
            ans = max(ans,dfs(Li[i].v));
        }
        a[u]+=ans;
    }
    return a[u];
}

int main()
{
    scanf("%d",&T);

    int x,y;

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

        Init();

        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }
        for(int i=0;i<n;i++)//建圖
        {
            for(int j=0;j<m;j++)
            {
                if(str[i][j] == '#')
                {
                    continue;
                }

                DFS(i,j);

                if(str[i][j]=='*')
                {
                    scanf("%d %d",&x,&y);

                    AddEdge1(i*m+j,x*m+y);
                }
                else
                {
                    va[i*m+j]=str[i][j]-'0';
                }
            }
        }


        for(int i=0;i<n*m;i++)
        {
            if(!vis[i])
            {
                Tarjan(i);
            }
        }

        for(int i=0;i<n*m;i++) //從新建圖
        {
            for(int j=Head1[i];j!=-1;j = Li[j].next)
            {
                if(pre[i]!=pre[Li[j].v])
                {
                    AddEdge2(pre[i],pre[Li[j].v]);
                }
            }
        }
        memset(vis,0,sizeof(vis));

        printf("%d\n",dfs(pre[0]));
    }
    return 0;
}
/* 1 10 10 1167811678 1*77811678 1*70001678 1*77811678 1#77800078 1#77837### 1*00037### 1*34000### 1*3451*778 37###1#345 5 5 5 5 5 6 5 7 5 8 5 9 5 10 ans=130 */
相關文章
相關標籤/搜索