POJ 3592--instantaneous Transference "SCC indent new diagram && SPFA find the longest road && classic"

Source: Internet
Author: User

Instantaneous transference
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6177 Accepted: 1383

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 a object uses this magic function, it'll be transferred to the specified point immediately, regardless of what far It is.

Now there was a mining area, and you were driving an ore-miner truck. Your mission is to take the maximum ores in the field.

The ore area was a rectangle region which was composed by N x m small squares, some of the squares has Nu Mbers of ores, while some does not. The ores can ' t is 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 and while it can not move to the northern or western adjacent square. And some squares has 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 would never lose its magic power; You can use the magic power whenever get there.

Input

The first line of the input was an integer T which indicates the number of test cases.

For each of the test case, the first would be the integers n, m (2≤ n, m ≤40).

The next N lines would describe the map of the mine field. Each of the N lines is a string that contains M characters. Each character would be a integer X (0≤ x ≤9) or a ' * ' or a ' # '. The integer x indicates that square have X units of ores, which your truck could get them all. The ' * ' indicates this square have a magic power which can transfer truck within an instant. The ' # ' indicates this square was full of rock and the truck can ' t move on this square. You can assume that the starting position of the truck would never be a ' # ' square.

As the map indicates, there is 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 ' * ', and the order from north to S Outh then west to east. (The original point is the northwest corner, the coordinate are 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

12 2111*0 0

Sample Output

3

Main topic:

There is a n*m matrix map, the matrix uses a variety of characters to represent the different terrain, if it is the number x (0~9), it means that the area is a mining area, there are X-units of minerals. If "*", it means that the area is a delivery point and corresponds to a unique target coordinate. If "#", it means that the area is mountainous and minecarts cannot enter. Now the starting point of the minecart is at the coordinates (0,0) point. and the (0,0) point must not be a "#" area. Minecarts can only go to the right, down, or at the point of transfer to a specified location. So the question is: how many mines can the minecart pick up.


Ideas:
If n*m a matrix unit as a n*m point, the number is 0~n*m. Then, from one coordinate to another, it is considered an edge between two points. The coordinates of the arrival of the minerals owned by the edge of the weight value. The question then becomes: the longest path the minecart can reach from node 0. But in addition to the right and down the edge, considering the transmission point and the target coordinates are composed of edges, the original image will be much more back to the edge, forming a lot of the forward ring. The emergence of a forward ring, so that the mine can be picked up a part of the mineral, as long as it can go into the ring, the ring in all the points of the mineral can be mined. But the problem also came out, if not to do the processing, direct search path, then the minecart will probably go into the ring does not come out. So I think of the contraction point, the direction of the ring to a point, that is, strong connected component contraction point. And record the total ore value in the strong connected component. When the point is shrunk, the original image becomes a directed acyclic graph (DAG). Then re-establish a new diagram (DAG), the longest path to the new graph (with the SPFA algorithm), get the source point (0,0) to the longest path of each point. The longest path is found, which is the result of the request.

This problem and POJ3126 similar, are SPFA to find the longest road, POJ312 analysis

#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <queue > #define MAXN 2000+100#define maxm 40000+100#define INF 0x3f3f3f3fusing namespace std;int N, m;struct node {int u, V, next;}; Node Edge[maxm];int HEAD[MAXN], Cnt;int LOW[MAXN], Dfn[maxn];int dfs_clock;int STACK[MAXN], Top;bool Instack[maxn];int Belong[maxn];int Scc_clock;int val[maxn];//The amount of ore deposited at each point int sumval[maxn];//The amount of ore in each indent Vector<int>map[maxm];char Map [100]    [100];void init () {cnt = 0;    Memset (Head,-1, sizeof (head));    Memset (val, 0, sizeof (Val));    memset (sumval, 0, sizeof (sumval)); Memset (val, 0, sizeof (val));}    void Addedge (int u, int v) {edge[cnt] = {u, V, Head[u]}; Head[u] = cnt++;}    void Getmap () {scanf ("%d%d", &n, &m);    for (int i = 0; i < n; ++i) scanf ("%s", Map[i]);            for (int i = 0, i < n; ++i) {for (int j = 0; j < m; ++j) {if (map[i][j] = = ' # ') continue; if (i + 1 < n && map[i+ 1][j]! = ' # ')//Go down Addedge (i * m + j, (i + 1) * m + j);            if (j + 1 < m && map[i][j + 1]! = ' # ')//Right-walk Addedge (i * m + j, I * m + j + 1);            Val[i * m + j] = map[i][j]-' 0 ';                if (map[i][j] = = ' * ') {Val[i * m + j] = 0;                int x, y;                scanf ("%d%d", &x, &y);            if (map[x][y]! = ' # ');//The location of the transmission may be # Addedge (i * m + j, x * m + y);    }}}}void Tarjan (int u) {int V;    Low[u] = dfn[u] = ++dfs_clock;    stack[top++] = u;    Instack[u] = true;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;            if (!dfn[v]) {Tarjan (v);        Low[u] = min (Low[u], low[v]);    } else if (Instack[v]) low[u] = min (Low[u], dfn[v]);        } if (dfn[u] = = Low[u]) {scc_clock++;            do{v = stack[--top];            Sumval[scc_clock] + = Val[v]; INSTACK[V] = false;           BELONG[V] = Scc_clock;    } while (V! = u);    }}void find () {memset (low, 0, sizeof (low));    memset (DFN, 0, sizeof (DFN));    memset (Belong, 0, sizeof (Belong));    memset (stack, 0, sizeof (stack));    Memset (Instack, False, sizeof (false));    Dfs_clock = Scc_clock = top = 0;    for (int i = 0; i < n * m; ++i) {if (!dfn[i]) Tarjan (i); }}void Suodian () {//indent new figure for (int i = 1; I <= scc_clock; ++i) map[i].clear ();//for (int i = 0; i < n * m ; ++i) {//for (int j = head[i]; J! =-1; j = edge[j].next) {//int u = belong[i];//int v = Belon    g[edge[j].v];//if (u! = V)//Map[u].push_back (v);//}//}//Above is also a way to build a map.        for (int i = 0; i < cnt; ++i) {int u = belong[edge[i].u];        int v = belong[edge[i].v];    if (U = v) map[u].push_back (v);    }}int vis[maxn],dist[maxn];void SPFA () {queue<int>q;    memset (Vis, 0, sizeof (VIS)); MemsetDist, 0, sizeof (Dist));    Vis[belong[0]] = 1;    Dist[belong[0]] = sumval[belong[0]];    Q.push (Belong[0]);        while (!q.empty ()) {int u = q.front ();        Q.pop ();        Vis[u] = 0;            for (int i = 0; i < map[u].size (); ++i) {int v = map[u][i];                if (Dist[v] < Dist[u] + Sumval[v]) {Dist[v] = Dist[u] + sumval[v];                    if (!vis[v]) {vis[v] = 1;                Q.push (v);    }}}}}int main () {int T;    scanf ("%d", &t);        while (t--) {init ();        Getmap ();        Find ();        Suodian ();        SPFA ();        Sort (dist + 1, dist + scc_clock + 1);    printf ("%d\n", Dist[scc_clock]); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3592--instantaneous Transference "SCC indent new diagram && SPFA find the longest road && classic"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.