Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5734 | Accepted: 1275 |
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
题解:如果没有传送门的话,图就是个DAG,我们可以直接记忆化搜索答案。现在有传送门,图就不在是DAG了,但是显然对于一个强连通分量中的点都是可以获得它们的分数的。所以将强连通分量缩点,然后记忆化搜索即可。
代码如下:
#include<stdio.h>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<math.h>
#include<string.h>
#define nn 2100
#define inff 0x3fffffff
using namespace std;
int n,m;
struct node
{
int en,next;
}E[nn*nn];
int p[nn],num;
char tu[50][50];
void init()
{
memset(p,-1,sizeof(p));
num=0;
}
void add(int st,int en)
{
E[num].en=en;
E[num].next=p[st];
p[st]=num++;
E[num].en=st;
E[num].next=p[st];
p[st]=num++;
}
int dfn[nn],low[nn];
int df;
int cnt;
int fa[nn];
bool insta[nn];
vector<int>ve[nn];
stack<int>sta;
int val[nn];
void dfs(int id)
{
dfn[id]=low[id]=++df;
sta.push(id);
insta[id]=true;
int i,w;
for(i=p[id];i+1;i=E[i].next)
{
w=E[i].en;
if(dfn[w]==-1)
{
dfs(w);
low[id]=min(low[id],low[w]);
}
else if(insta[w])
{
low[id]=min(low[id],dfn[w]);
}
}
if(dfn[id]==low[id])
{
++cnt;
int ix;
val[cnt]=0;
do
{
ix=sta.top();
sta.pop();
insta[ix]=false;
fa[ix]=cnt;
if(tu[ix/m][ix%m]<='9'&&tu[ix/m][ix%m]>='0')
{
val[cnt]+=tu[ix/m][ix%m]-'0';
}
}
while(ix!=id);
}
}
int dp[nn];
void go(int id)
{
dp[id]=val[id];
int i,w;
int ix=0;
for(i=0;i<(int)ve[id].size();i++)
{
w=ve[id][i];
if(dp[w]==-1)
{
go(w);
}
ix=max(ix,dp[w]);
}
dp[id]+=ix;
}
void solve()
{
int i,j,w;
memset(insta,false,sizeof(insta));
memset(dfn,-1,sizeof(dfn));
memset(dp,-1,sizeof(dp));
df=0;
cnt=0;
for(i=0;i<n*m;i++)
{
if(dfn[i]==-1)
{
dfs(i);
}
}
for(i=0;i<=2000;i++)
ve[i].clear();
for(i=0;i<n*m;i++)
{
for(j=p[i];j+1;j=E[j].next)
{
w=E[j].en;
if(fa[w]!=fa[i])
{
ve[fa[i]].push_back(fa[w]);
}
}
}
go(fa[0]);
printf("%d\n",dp[fa[0]]);
}
int main()
{
int i,t,j,u,v;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s",tu[i]);
}
init();
vector<pair<int,int> >mj;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(tu[i][j]=='#')
continue;
if(j+1<m&&tu[i][j+1]!='#')
add(i*m+j,i*m+j+1);
if(i+1<n&&tu[i+1][j]!='#')
add(i*m+j,(i+1)*m+j);
if(tu[i][j]=='*')
{
mj.push_back(make_pair(i,j));
}
}
}
for(i=0;i<(int)mj.size();i++)
{
scanf("%d%d",&u,&v);
add(mj[i].first*m+mj[i].second,u*m+v);
}
solve();
}
return 0;
}