POJ 3592 Instantaneous Transference

tarjan+缩点+spfa最长路

Instantaneous Transference
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5443   Accepted: 1190

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

Source

South Central China 2008 hosted by NUDT

[Submit]   [Go Back]   [Status]   [Discuss]


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <set>

using namespace std;

const int INF=0x3f3f3f3f,dir_x[2]={-1,0},dir_y[2]={0,-1},maxn=1800;

int r,c,pnum[100][100],p,val[maxn],pp,ppnum[maxn];
char Map[100][100];

/*******************建图*********************/

struct Edge
{
    int to,next,weight;
}edge[10000],edge2[10000];

int Adj[maxn],Size,Adj2[maxn],Size2;

void init_E()
{
    Size=0;
    memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v,int w=0)
{
    edge[Size].next=Adj[u]; edge[Size].to=v; edge[Size].weight=w; Adj[u]=Size++;
}

void init_E2()
{
    Size2=0;
    memset(Adj2,-1,sizeof(Adj2));
}

void Add_Edge2(int u,int v,int w)
{
    edge2[Size2].next=Adj2[u]; edge2[Size2].to=v; edge2[Size2].weight=w; Adj2[u]=Size2++;
}

/********************tarjan*****************************/

int Low[maxn],DFN[maxn],Stack[maxn],Belong[maxn];
int Index,top,scc,n;
bool Instack[maxn]; int WE[maxn];

void tarjan(int u)
{
    int v;
    Low[u]=DFN[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    for(int i=Adj[u];~i;i=edge[i].next)
    {
        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])
    {
        scc++;
        do
        {
            v=Stack[--top];
            Instack[v]=false;
            WE[scc]+=val[v];
            Belong[v]=scc;
        }while(v!=u);
    }
}

void solve_tarjan(int n)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    memset(WE,0,sizeof(WE));

    Index=scc=top=0;

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

/**********************spfa*****************************/

int dist[maxn]; bool inQ[maxn];

bool spfa(int src)
{
    memset(dist,0xcf,sizeof(dist));
    memset(inQ,false,sizeof(inQ));

    dist[src]=0;

    queue<int> q;
    inQ[src]=true; q.push(src);

    while(!q.empty())
    {
        int u=q.front(); q.pop();

        for(int i=Adj2[u];~i;i=edge2[i].next)
        {
            int v=edge2[i].to;

            if(dist[v]<dist[u]+edge2[i].weight)
            {
                dist[v]=dist[u]+edge2[i].weight;
                if(!inQ[v])
                {
                    inQ[v]=true;
                    q.push(v);
                }
            }
        }
        inQ[u]=false;
    }
}

int main()
{
    int cas;
    scanf("%d",&cas);
while(cas--)
{
    scanf("%d%d",&r,&c);
    init_E(); init_E2();
    p=pp=0;
    for(int i=0;i<r;i++)
    {
        scanf("%s",Map[i]);
        for(int j=0;j<c;j++)
        {
            if(Map[i][j]=='#') continue;

            pnum[i][j]=p++;

            if(Map[i][j]>='0'&&Map[i][j]<='9')
                val[pnum[i][j]]=Map[i][j]-'0';
            else val[pnum[i][j]]=0;

            if(Map[i][j]=='*')
            {
                ppnum[pp++]=pnum[i][j];
            }

            for(int k=0;k<2;k++)
            {
                int nxx=i+dir_x[k],nxy=j+dir_y[k];
                if(nxx<0||nxy<0) continue;
                if(Map[nxx][nxy]=='#') continue;
                Add_Edge(pnum[nxx][nxy],pnum[i][j]);
            }
        }
    }

    for(int i=0;i<pp;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(Map[x][y]=='#') continue;
        Add_Edge(ppnum[i],pnum[x][y]);
    }

    solve_tarjan(p);

    set< pair<int,int> > sII;
    sII.clear();

    Add_Edge2(0,Belong[0],WE[Belong[0]]);
    sII.insert(make_pair(0,Belong[0]));

    for(int i=0;i<p;i++)
    {
        for(int j=Adj[i];~j;j=edge[j].next)
        {
            ///u--->v
            int u=Belong[i],v=Belong[edge[j].to];
            if(u==v) continue;
            if(sII.count(make_pair(u,v))==0)
            {
                sII.insert(make_pair(u,v));
                Add_Edge2(u,v,WE[v]);
            }
        }
    }

    spfa(0);
    int ans=0;
    for(int i=0;i<p;i++)
        ans=max(ans,dist[i]);

    printf("%d\n",ans);
}
    return 0;
}




你可能感兴趣的:(POJ 3592 Instantaneous Transference)