Instantaneous Transference poj3592(【强连通分量】【最长路】)

Instantaneous Transference
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6577 Accepted: 1489

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的格子地图,每一格上面是0~9,“*”或“#”。如果格子上是数字代表这个格子上有当前数量的矿石。如果是“*” 代表着当前格子是一个传送阵可以传送到指定的地方。如果是“#”代表当前格子不可达。

        现在有一个矿车在坐标(0,0),也就是左上角。他只能向右和下行驶。当遇到传送阵时可以被传送到指定的位置。当他遇到数字时就可以得到那些数量的矿石,那个地方的矿石数量就变为“0”。问矿车最多可以采多少矿。

解题思路:

1、我们先要根据格子地图建图。(注1)

2、因为建立出的图可能会有环,我们要用Tarjan算法将环缩成点。(注2)

3、我们将缩点处理后的图重新建图。(注3)

4、对图求最长路。

#include <stdio.h>
#include <string.h>
#include <queue>
#define min(a,b) a>b?b:a
#define Max 50050
using namespace std;
struct Edge
{
    int to,next;
} edge[Max],edge1[Max];

int head[Max],head1[Max],tol,tol1;
void add(int u,int v)
{
    edge[tol].to = v;
    edge[tol].next = head[u];
    head[u] = tol++;
}

void add1(int u,int v)
{
    edge1[tol1].to = v;
    edge1[tol1].next = head1[u];
    head1[u] = tol1++;
}

int cost[Max],sum[Max];
int belong[Max],Stack[Max],instack[Max],dfn[Max],low[Max];
int top,bcnt,time;
void tarjan( int u )
{
    dfn[u] = low[u] = ++time;
    Stack[top++] = u;
    instack[u] = true;

    for( int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        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])
    {
        bcnt++;
        int v;
        do
        {
            v = Stack[--top];
            belong[v] = bcnt;
            sum[bcnt] += cost[v];
            instack[v] = false;
        }
        while(u != v);
        }
}

void Rebuild(int n,int m)
{
    tol1 = 0;
    for( int u = 0; u < n*m; u++)
    {
        for( int j = head[u]; j != -1; j = edge[j].next)
        {
            int v = edge[j].to;
            if(belong[u] != belong[v])
                add1(belong[u], belong[v]);
        }
    }
}
int vis[Max],dis[Max];
void spfa()
{
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    queue<int>q;
    q.push(belong[0]);
    vis[belong[0]] = 1;
    dis[belong[0]] = sum[belong[0]];

    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for( int i = head1[u]; i != -1; i = edge1[i].next)
        {
            int v = edge1[i].to;
            if(dis[v] < dis[u] + sum[v])
            {
                dis[v] = dis[u] + sum[v];
                if(!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }

        }
    }

}
int main()
{
    int t,m,n;
    int u,v;
    char s[60][60];
    scanf("%d",&t);

    while(t--)
    {
       memset(dfn, 0, sizeof(dfn));
       memset(low, 0, sizeof(low));
       memset(s,0,sizeof(s));
       memset(head,-1,sizeof(head));
       memset(head1,-1,sizeof(head1));
       memset(cost,0,sizeof(cost));
       memset(sum, 0, sizeof(sum));
        top = time = bcnt = 0;
        tol = tol1 = 0;

        scanf("%d%d",&n,&m);
        for( int i = 0; i < n; i++)
            scanf("%s",s[i]);

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

                    cost[ i * m + j] = s[ i ][ j ] - '0';

                    if( s[i][j] == '*')
                    {
                        cost[ i * m + j] = 0;
                        int x, y;
                        scanf("%d%d", &x, &y);
                        if(s[x][y] != '#')
                            add( i * m + j, x * m + y);
                    }
                }


            }
        }

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

        Rebuild(n,m);
        spfa();
//        for( int i = 1; i <= bcnt;i++)
//            printf("%d\n",dis[i]);
        int ans  =  -1;
        for( int i = 1; i <= bcnt; i++)
        {

            if(ans < dis[i])
                ans = dis[i];
        }
       printf("%d\n",ans);

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值