1. 程式人生 > >POJ 3592--Instantaneous Transference【SCC縮點新建圖 && SPFA求最長路 && 經典】

POJ 3592--Instantaneous Transference【SCC縮點新建圖 && SPFA求最長路 && 經典】

col describe sca 搜索 hat style ecif test csdn

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 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

題目大意:

有一個N*M的矩陣地圖,矩陣中用了多種字符代表不同的地形。假設是數字X(0~9),則表示該區域為礦區,有X單位的礦產。

假設是"*",則表示該區域為傳送點,而且相應唯一一個目標坐標。假設是"#",,則表示該區域為山區,礦車不能進入。如今礦車的出發點在坐標(0。0)點。而且(0,0)點一定不是"#"區域。礦車僅僅能向右走、向下走或是遇到傳送點的時候能夠傳送到指定位置。那麽問題來了:礦車最多能採到多少礦。




思路:
假設把N*M個矩陣單位看做是N*M個點。編號為0~N*M。然後從一個坐標到還有一個坐標看做是兩點之間的邊。

到達的坐標所擁有的礦產為邊的權值。那麽問題就變成了:礦車從節點0出發,所能達到的最長路徑。可是除了向右走和向下走的邊,考慮到還有傳送點和目標坐標構成的邊。原圖上就會多了非常多回退邊。構成了非常多的有向環。

有向環的出現,使得礦車可以採到的礦產增多了一部分,僅僅要能走到有向環內,則該環內全部點的礦產都能被採到。可是問題也出來了,假設不做處理,直接搜索路徑。那麽礦車非常可能會走進環內不出來。

於是想到了縮點。把有向環縮為一個點。也就是強連通分量縮點。並記錄強連通分量中的總礦產值。

縮點後,原圖就變成了一個有向無環圖(DAG)。然後又一次建立一個新圖(DAG),對新圖求最長路徑(用SPFA算法),得到源點(0。0)到各點的最長路徑。

從中找出最長的路徑,就是所求的結果。


這題和POJ3126類似,都是縮點SPFA求最長路。POJ312解析

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#define maxn 2000+100
#define maxm 40000+100
#define INF 0x3f3f3f3f
using 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];//存每一個點的礦石量
int sumval[maxn];//存每一個縮點的礦石量
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] != '#')//向下走
                addedge(i * m + j, (i + 1) * m + j);
            if(j + 1 < m && map[i][j + 1] != '#')//向右走
                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] != '#');//傳送的位置可能為 #
                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(){//縮點新建圖
    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 = Belong[edge[j].v];
//            if(u != v)
//                Map[u].push_back(v);
//        }
//    }
    //上面也是一種建圖方式。
    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));
    memset(dist, 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;
}


POJ 3592--Instantaneous Transference【SCC縮點新建圖 &amp;&amp; SPFA求最長路 &amp;&amp; 經典】