UESTC Training for Graph Theory——F、Instantaneous Transference

来源:互联网 发布:奥格斯堡同盟 知乎 编辑:程序博客网 时间:2024/05/18 04:30

Instantaneous Transference

Time Limit: 5000 ms Memory Limit: 65536 kB Solved: 42 Tried: 561

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 (1 ≤ 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

Hint

The data used in this problem is unofficial data prepared by allenlowesy. So any mistake here does not imply mistake in the offcial judge data.

Source

South Central China 2008 hosted by NUDT

/*算法思想:  给一个n*m的矩阵,从每个点出发可以向右走,向下走,那么就可以得到一幅图,将坐标(x,y)转换成点node=x*m+y  由于有传送阵的存在,使得可能在图中形成环(就像第一组样例一样)  1  2 2  11  1*  0 0  形成环之后,这个环中的所采矿点中的矿石均可以采摘完,那么就等效成一个点了,于是,我们可以在原图的基础上重新  建立一个图,这个图是由原图中的强连通分量经过缩点之后得到的,边就是以前图中的桥,每个点有个权值,就是代表能够在这个点  采摘多少矿石,这个点的权值为原图中同在一个强连通分量中的所有点的采矿值之和。这样就得到了一个新的没有环的树。  问题就转换成了在树上求一条路径,使得沿这条路径访问下去得到的权值之和最大。  我把第一个点(左边(0,0)所在的环)的权值单独拿出来,然后把每个点的权值放到进入这个点的边上,这样就构成了一个  带权值的树,然后求这棵树的最长路,用求得的最长路的权值加上起始点的权值就是本题的答案了。  由于涉及到强连通分量,并且以后还要建立新的图,所以并查集缩点在这里就显得很麻烦了,我们可以用tarjan将原图的所有强连通分  量缩成一个对应的点,这个点的标号就是它是第几个强连通分量的标号,权值就是原图中对应的强连通分量中每个点的权值之和。这样构  造新图就显得比用并查集简单了,然后按照原图中的边构造新图,虽然会产生不必要的重复的一模一样的边,但是由于这道题边的数量不  是很多,所以有重边也没有关系,依然可以很快出解*//*代码*/#include<iostream>#include<cstdio>#include<cstring>#include<stack>#include<vector>#include<algorithm>#define N 50#define INF 0x7fffffffusing namespace std;struct data{    int st,en,val,next;} edge[200000],e[200000];  //edge存储原图中的边 e存储缩点后的树中的边int tot,head[N*N];char s[N][N];int n,m,tra_num,co[N*N][2],ore[N*N];int low[N*N],dfn[N*N],belong[N*N];int val[N*N];bool vs[N*N];stack<int> zhan;int Time,node;int dis[N*N];void add_edge(int st,int en,int val)  //在原图中加边{    e[tot].st=st;    e[tot].en=en;    e[tot].val=val;    e[tot].next=head[st];    head[st]=tot++;}void add_edge1(int st,int en,int val)  //在缩点后的图中加边{    edge[tot].st=st;    edge[tot].en=en;    edge[tot].val=val;    edge[tot].next=head[st];    head[st]=tot++;}void tarjan(int u)  //用tarjan先找出强连通分量,在把强连通分量缩成一个点{    vs[u]=true; zhan.push(u);    dfn[u]=low[u]=++Time;    for(int i=head[u];i!=-1;i=edge[i].next)        if(dfn[edge[i].en]==-1)        {            tarjan(edge[i].en);            low[u]=min(low[u],low[edge[i].en]);        }        else if(vs[edge[i].en])            low[u]=min(low[u],dfn[edge[i].en]);    int v;    if(low[u]==dfn[u])  //找到一个强连通分量,缩点    {        do        {            v=zhan.top();            zhan.pop();            belong[v]=node;            val[node]+=ore[v];  //新的节点的权值为强连通分量中所有点的权值和            vs[v]=false;        }        while(v!=u);        node++;    }}bool dfs_spfa(int v)  //用spfa的dfs找最长路,由于前面写过一个代码模板,一个字符没变的照搬过来了{    if(vs[v]) return false;    vs[v]=true;    for(int i=head[v];i!=-1;i=e[i].next)        if(dis[e[i].en]<dis[v]+e[i].val)        {            dis[e[i].en]=dis[v]+e[i].val;            if(!dfs_spfa(e[i].en)) return false;        }    vs[v]=false;    return true;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)            scanf("%s",s[i]);        tra_num=0;        memset(ore,0,sizeof(ore));  //初始化每个点的权值为 0        memset(head,-1,sizeof(head));        tot=0;        for(int i=0;i<n;i++)            for(int j=0;j<m;j++)            {                if(s[i][j]=='#') continue;                if(s[i][j]=='*')                {                    co[tra_num][0]=i;                    co[tra_num][1]=j;                    tra_num++;                    ore[i*m+j]=0;                }                if(s[i][j]>='0' && s[i][j]<='9') ore[i*m+j]=s[i][j]-'0';                if(j<m-1 && s[i][j+1]!='#') add_edge1(i*m+j,i*m+j+1,0);  //加边                if(i<n-1 && s[i+1][j]!='#') add_edge1(i*m+j,i*m+j+m,0);  //加边            }        int x,y;        for(int i=0;i<tra_num;i++)        {            scanf("%d%d",&x,&y);            if(s[x][y]!='#')                add_edge1(co[i][0]*m+co[i][1],x*m+y,0);        }        Time=node=0;        memset(dfn,-1,sizeof(dfn));        memset(low,0,sizeof(low));        memset(vs,0,sizeof(vs));        memset(val,0,sizeof(val));        for(int i=0;i<n*m;i++)  //对每个点进行一次tarjan            if(dfn[i]==-1)                tarjan(i);        int total=tot;        tot=0;        memset(head,-1,sizeof(head));        for(int i=0;i<total;i++)            if(belong[edge[i].st]!=belong[edge[i].en])  //虽然会有重边,但是相对来说影响较少,不过重边的确耗费了较多时间                add_edge(belong[edge[i].st],belong[edge[i].en],val[belong[edge[i].en]]);        for(int i=0;i<node;i++)            dis[i]=-INF;        dis[belong[0]]=0;        dfs_spfa(belong[0]);        int ans=0;        for(int i=0;i<node;i++)  //找所有点中距离起始点最长的路径长度            if(ans<dis[i]) ans=dis[i];        printf("%d\n",ans+val[belong[0]]);  //加上起始点的权值,就是答案    }    return 0;}


 

 

原创粉丝点击