POJ 3592 强联通+最长路。

Instantaneous Transference











Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 5153Accepted: 1123

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 N, M
(2 ≤ N, M ≤ 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,M -
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

 

解题思路:根据题目要求连边,然后tarjan缩点,再spfa求最长路,粗心大意,白白的无数次wa,惨痛的教训呀。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<string.h>
#include<string>
using namespace std;
const int maxn=1700;
int head[maxn],tol,low[maxn],dfn[maxn],Stack[maxn],instack[maxn],belong[maxn],scc,indexx,top,weight[maxn],head1[maxn],tot,weight1[maxn],vis[maxn],dist[maxn],que[100*maxn];
struct node
{
        int next,to,val;
}edge[4000000],edge1[4000000];
void add(int u,int v)
{
        edge[tol].to=v;
        edge[tol].next=head[u];
        head[u]=tol++;
}
void add1(int u,int v,int w)
{
        edge1[tot].to=v;
        edge1[tot].next=head1[u];
        edge1[tot].val=w;
        head1[u]=tot++;
}
void tarjin(int u)
{
        low[u]=dfn[u]=++indexx;
        instack[u]=1;Stack[top++]=u;
        int i,v;
        for(i=head[u];i!=-1;i=edge[i].next)
        {
                v=edge[i].to;
                if(!dfn[v])
                {
                        tarjin(v);
                        if(low[u]>low[v])low[u]=low[v];
                }
                else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
        }
        if(low[u]==dfn[u])
        {
                scc++;
                do
                {
                        v=Stack[--top];
                        instack[v]=0;
                        belong[v]=scc;
                }while(u!=v);
        }
}
int m,n;
char str[50][50];
void spfa(int st)
{
        memset(dist,0,sizeof(dist));
        dist[st]=weight1[st];
        memset(vis,0,sizeof(vis));vis[st]=1;
        int front=0,rear=0;
        que[rear++]=st;
        while(front!=rear)
        {
                int u=que[front++];
                vis[u]=0;
                for(int i=head1[u];i!=-1;i=edge1[i].next)
                {
                        int v=edge1[i].to;
                        if(dist[v]<dist[u]+edge1[i].val)
                        {
                                dist[v]=dist[u]+edge1[i].val;
                                if(vis[v]==0)
                                {
                                        vis[v]=1;
                                        que[rear++]=v;
                                }
                        }
                }
        }
}
void solve()
{
        memset(instack,0,sizeof(instack));
        memset(belong,0,sizeof(belong));
        memset(dfn,0,sizeof(dfn));
        indexx=top=scc=0;
        int i,j,u,v;
        for(i=0;i<n;i++)if(!dfn[i])tarjin(i);
        memset(head1,-1,sizeof(head1));tot=0;
        memset(weight1,0,sizeof(weight1));
        for(i=0;i<n;i++)weight1[belong[i]]+=weight[i];
        for(u=0;u<n;u++)
        {
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        v=edge[i].to;
                        if(belong[u]!=belong[v])add1(belong[u],belong[v],weight1[belong[v]]);
                }
        }
       spfa(belong[0]);
       int ans=0;
       for(i=1;i<=scc;i++)if(ans<dist[i])ans=dist[i];
       printf("%d\n",ans);
}
int main()
{
        int i,j,k,T,p,q,g,h;
        scanf("%d",&T);
        while(T--)
        {
                scanf("%d%d",&n,&m);
                memset(head,-1,sizeof(head));tol=0;
                memset(weight,0,sizeof(weight));
                for(i=0;i<n;i++)scanf("%s",str[i]);
                for(i=0;i<n;i++)
                for(j=0;j<m;j++)
                {
                              if(str[i][j]=='#')continue;
                              if(str[i][j]>='0'&&str[i][j]<='9')weight[i*m+j]+=str[i][j]-'0';
                              p=i+1;q=j;
                              if(p<n&&str[p][q]!='#')add(i*m+j,p*m+q);
                              p=i;q=j+1;
                              if(q<m&&str[p][q]!='#')add(i*m+j,p*m+q);
                }
                for(i=0;i<n;i++)
                for(j=0;j<m;j++)
                if(str[i][j]=='*')
                {
                        scanf("%d%d",&p,&q);
                        if(str[p][q]=='#')continue;
                        add(i*m+j,p*m+q);
                }
                n*=m;
               // cout<<n<<endl;
                
//cout<<tol<<endl;
                solve();
        }
        return 0;
}

 

 

posted @ 2013-09-10 15:04  线性无关  阅读(121)  评论(0)    收藏  举报