POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】

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 NM (2 ≤ NM ≤ 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,- 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-09 08:23:27

POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】的相关文章

poj 3592 Instantaneous Transference 强连通图 缩点 再求最长路

1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<stack> 5 #include<queue> 6 using namespace std; 7 #define maxx 44 8 #define maxx2 44*44 9 #define INF 99999999 10 char s[maxx][maxx]; 11 bool tong[maxx2

POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;amp;&amp;amp; SPFA求最长路 &amp;amp;&amp;amp; 经典】

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

POJ 3592 Instantaneous Transference(强连通+DP)

POJ 3592 Instantaneous Transference 题目链接 题意:一个图,能往右和下走,然后有*可以传送到一个位置,'#'不能走,走过一个点可以获得该点上面的数字值,问最大能获得多少 思路:由于有环先强连通缩点,然后问题转化为dag,直接dp即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <sta

POJ 3592 Instantaneous Transference Tarjan+SPFA

题目大意:给出一张地图,有数字的点代表上面有数字个矿物,*代表这个点可以传送到另一个点上,#代表不能走.从一个点只能到这个点的下方和右方.现在从(0,0)开始,问最多可以收集多少矿物. 思路:这个题肯定是建图,然后最长路,关键是有了传送,就有可能形成正权环,然后在SPFA的过程中就会死循环.一个环上的所有权值只能得到一次,所以就用一次Tarjan求出所有的环,把权值累计一下,变成一个点,然后重新建图.这样得到的图就是拓扑图了,没有环,可以无忧无虑的SPFA了. PS:这个题我用拓扑排序怎么也切不

POJ 2762--Going from u to v or from v to u?【scc缩点新建图 &amp;&amp; 判断是否是弱连通图】

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15755   Accepted: 4172 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

POJ 3592 Instantaneous Transference(建图强连通+单源最长路)

题目大意:有一张n*m的地图,每个点上可能是数字,代表矿石的数目,可能是*,表示一个传送阵,送往某个坐标,可能是#,代表不通.每次矿车只能往右方或者下方走一格,问从(0,0)点出发可以最多收集到多少矿石 思路:先根据矿车的可移动的方向建有向图,"*"导致可能会有环,所以先缩点变成有向无环图. 然后就是DAG上的最长路问题(拓扑排序+dp) 而且也是单源最长路问题,可以用最短路算法去做 吐槽一下debug了一天,原来是tarjan的时间戳写跪了,关键是直到现在仍不觉得那种写法是错的,对拍

POJ 2762--Going from u to v or from v to u?【scc缩点新建图 &amp;amp;&amp;amp; 推断是否是弱连通图】

Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15755   Accepted: 4172 Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, and one-way corridors

poj3592 Instantaneous Transference tarjan缩点+建图

//给一个n*m的地图,坦克从(0 , 0)开始走 //#表示墙不能走,*表示传送门可以传送到指定地方,可以选择也可以选择不传送 //数字表示该格的矿石数, //坦克从(0,0)开始走,只能往右和往下走, //问最多能得到多少矿石 //直接建图,但由于有传送门,需要缩点 //然后用dfs直接搜一条权值最大的路 #include<cstdio> #include<cstring> #include<iostream> #include<vector> #inc

(tarjan+SPFA求最长路) poj 3114

Countries in War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2803   Accepted: 843 Description In the year 2050, after different attempts of the UN to maintain peace in the world, the third world war broke out. The importance of indus