POJ 3592 Instantaneous Transference 强连通缩点+spfa最长路

Instantaneous Transference
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5519 Accepted: 1218

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 hasX 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

Source




<pre name="code" class="cpp">#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

const int maxn=2010;


struct node{
    int u,to,next,w;
}e[maxn*maxn],e1[maxn*maxn];

char mp[maxn][maxn];

int head[maxn],edge;

int head1[maxn],edge1;

int id[maxn],q[maxn],dfn[maxn],low[maxn],num[maxn];

int n,m,tsp,qe,cnt;

int g[maxn],pos[maxn],dis[maxn],vis[maxn];

void init()
{
    MST(head,-1);
    edge=0;
    MST(head1,-1);
    edge1=0;
    CLR(g),CLR(pos);
}

void addedge(int u,int v)
{
    e[edge].u=u;
    e[edge].to=v;
    e[edge].next=head[u];
    head[u]=edge++;
}

void addedge1(int u,int v,int w)
{
    e1[edge1].u=u;
    e1[edge1].to=v;
    e1[edge1].next=head1[u];
    e1[edge1].w=w;
    head1[u]=edge1++;
}

void tarjan(int u)
{
    //cout<<"yes\n";
    int i,v;
    dfn[u]=low[q[qe++]=u]=++tsp;
    for(i=head[u];i>=0;i=e[i].next)
        if(!dfn[v=e[i].to])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else
        {
            if(id[v]<0)
            {
                low[u]=min(low[u],dfn[v]);
            }
        }
    if(low[u]==dfn[u])
    {

        ++cnt;

             do{
                    v=q[--qe];
                ++num[id[v]=cnt];
                pos[cnt]+=g[v];
                //cout<<pos[cnt]<<endl;
            }while(v!=u);
    }
}


void solve()
{
    int i;
    tsp=qe=cnt=0;
    for(i=0;i<=n*m;++i)
        id[i]=-1,dfn[i]=0;
    //cout<<"yes\n";
    for(i=0;i<n*m;++i)
        if(!dfn[i])
            tarjan(i);
}

int spfa(int s)
{
   // cout<<pos[s]<<endl;
    FOR(i,1,cnt)
     dis[i]=-1;
    CLR(vis);
    queue<int>q;
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head1[u];~i;i=e1[i].next)
        {
            int v=e1[i].to;
            if(dis[v]<dis[u]+e1[i].w)
            {
                dis[v]=dis[u]+e1[i].w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    int res=0;
    FOR(i,1,cnt)
     res=max(res,dis[i]);
    //cout<<res<<"****"<<pos[s]<<endl;
    return res+pos[s];
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        init();
        int xx,yy;
        scanf("%d%d",&n,&m);
        REP(i,n)
         scanf("%s",mp[i]);
        REP(i,n)
        {
            REP(j,m)
            {
                if(mp[i][j]=='*')
                {
                    scanf("%d%d",&xx,&yy);
                    int a=i*m+j;
                    int b=xx*m+yy;
                    if(mp[xx][yy]!='#')
                        addedge(a,b);
                    if(i+1<n&&mp[i+1][j]!='#')
                        addedge(a,a+m);
                    if(j+1<m&&mp[i][j+1]!='#')
                        addedge(a,a+1);
                    g[a]=0;
                }
                else
                {
                    if(mp[i][j]!='#')
                    {
                        int a=i*m+j;
                        if(i+1<n&&mp[i+1][j]!='#')
                            addedge(a,a+m);
                        if(j+1<m&&mp[i][j+1]!='#')
                            addedge(a,a+1);
                        g[a]=mp[i][j]-'0';
                    }
                    else
                        g[i*m+j]=0;
                }
            }
        }
        solve();
        //cout<<cnt<<endl;
        //for(int i=0;i<n*m;i++)
            //cout<<g[i]<<endl;
        REP(u,n*m)
        {
            for(int j=head[u];~j;j=e[j].next)
            {
                int v=e[j].to;
                if(u!=v&&id[u]!=id[v])
                    {
                        //cout<<pos[id[v]]<<endl;
                        addedge1(id[u],id[v],pos[id[v]]);
                    }
            }
        }
        //cout<<id[0]<<endl;
        int ans=spfa(id[0]);
        cout<<ans<<endl;
    }
    return 0;
}


 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值