poj 3592 Instantaneous Transference (借助强连通分量求缩点在建图spfa求最长路)

题目链接:http://poj.org/problem?id=3592

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”。问矿车最多可以采多少矿


ps: 看到这个题首先想到查找所有的路径找价值最大的,那么就dfs? 首先需要建图,建什么样的图的,当然是有向图,因为有回路,有可能造成环。这其实是很难往强联通分量上想的,还好这个题提示在连通分支的分类中,又是在刷缩点的题,那么就会想到,怎样把环去掉,那就用tarjan 算法求缩点,因为缩点中所包含的每个子点肯定能到达,所以在根据缩点建图求最长路,每条路径的价值为所点中所有子点的价值和,最后在加上起点缩点的价值就可以了,里面有很多坑   具体在代码已经注释

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 0x3f3f3f3f

using namespace std;

int n,m;
char s[50][50];
int value[3200];
int bccv[3200];

int ant;
int vis[3200];

struct node
{
    int v;
    int next;
} eage[31000];

struct node1
{
    int v;
    int w;
    int next;
}mp[31000];
int h[31000];
int head[31000];
//int hmp[50][50];
int low[3200],dfn[3200];
int cnt;
 int qu[30000];
int que[30000];
int bcc[3200];//缩点
int in;///队列
int out;///缩点

///开数组要大  否则re
void tarjan(int u)
{
    ///模板
    dfn[u]=low[u]=cnt++;
    vis[u]=1;
    que[in++]=u;

    for(int i=head[u];i!=-1;i=eage[i].next)
    {
        int v=eage[i].v;

        if(!vis[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v]==1)
        {
            low[u]=min(low[u],dfn[v]);
        }
    }

    if(low[u]==dfn[u])
    {
        int sum=0;
        while(que[in]!=u&&in>0)
        {
            in--;
            int v=que[in];

            vis[v]=2;
            bcc[v]=out;
            sum+=value[v];///记录连通分量中的价值和
            //if(v==u)break;
        }
        bccv[out]=sum;
        out++;
    }
}

int dist[31000];
int  spfa(int s)
{
    memset(dist,-INF,sizeof(dist));///最长路
    memset(vis,0,sizeof(vis));
    dist[s]=0;
    vis[s]=1;

    int ins,outs;
    ins=outs=0;
    qu[ins++]=s;
    while(ins>outs)
    {
        int u=qu[outs++];

        vis[u]=0;
        for(int i=h[u];i!=-1;i=mp[i].next)
        {
            int v=mp[i].v;
            int w=mp[i].w;
            if(dist[v]<dist[u]+w)
            {
                dist[v]=dist[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    qu[ins++]=v;
                }
            }
        }
    }
    int mx=-1;
    for(int i=0;i<=out;i++)
    {
        if(dist[i]>mx)mx=dist[i];///找最大的距离的一条路
    }
    return mx+bccv[s];///最后加上起始缩点的价值和
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ///涉及的数组比较多,一定要注意初始化
        memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
        memset(bccv,0,sizeof(bccv));
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(h,-1,sizeof(h));
        memset(bcc,-1,sizeof(-1));
        memset(que,-1,sizeof(que));
        memset(qu,-1,sizeof(qu));
       // memset(hmp,0,sizeof(hmp));
        memset(s,0,sizeof(s));
        memset(value,0,sizeof(value));
        in=0;
        out=0;
        cnt=1;
        ant=0;
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",s[i]);
        }
        int x,y;
        int htop=0;
        ant=n*m;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                ///只要不是以#为起点就有路径
                if(s[i][j]=='*')
                {
                    value[i*m+j]=0;///最好用i*m+j的形式处理每个坐标所代表的点(在这里wa了几发,因为有些x,y不在图中)
                    scanf("%d%d",&x,&y);
                    if(s[x][y]!='#'){
                    eage[htop].v=x*m+y;
                    eage[htop].next=head[i*m+j];
                    head[i*m+j]=htop++;
                    }

                    if(j+1<m&&s[i][j+1]!='#')
                    {
                        eage[htop].v=i*m+j+1;
                        eage[htop].next=head[i*m+j];
                        head[i*m+j]=htop++;
                    }
                    if(i+1<n&&s[i+1][j]!='#')
                    {
                        eage[htop].v=(i+1)*m+j;
                        eage[htop].next=head[i*m+j];
                        head[i*m+j]=htop++;
                    }

                }
                else if(s[i][j]>='0'&&s[i][j]<='9')
                {
                    value[i*m+j]=s[i][j]-'0';

                    if(j+1<m&&s[i][j+1]!='#')
                    {
                        eage[htop].v=i*m+j+1;
                        eage[htop].next=head[i*m+j];
                        head[i*m+j]=htop++;
                    }
                    if(i+1<n&&s[i+1][j]!='#')
                    {
                        eage[htop].v=(i+1)*m+j;
                        eage[htop].next=head[i*m+j];
                        head[i*m+j]=htop++;
                    }
                }
                else value[i*m+j]=0;///注意记录每个坐标的价值
            }
        }
        ///有向图求强联通分量
        ///套模板即可
        for(int i=0;i<ant;i++)
        {
            if(!vis[i])
            {
                tarjan(i);
            }
        }
        int ans=0;
        ///重新建图
        for(int i=0;i<ant;i++)
        {
            for(int j=head[i];j!=-1;j=eage[j].next)
            {
                int v=eage[j].v;
                if(bcc[i]!=bcc[v]&&bcc[i]!=-1&&bcc[v]!=-1)
                {
                    x=bcc[i];
                    y=bcc[v];
                    for(int k=h[x];k!=-1;k=mp[k].next)///判断有没有重复的,不判段也行ac
                    {
                        if(y==mp[k].v)continue;
                    }
                    mp[ans].v=y;
                    mp[ans].w=bccv[y];///一定注意建的是谁,是缩点(缩点的个数为out)
                    mp[ans].next=h[x];
                    h[x]=ans++;
                }
            }
        }
       int num = spfa(bcc[0]);///求最长路
       printf("%d\n",num);

    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值