【连通图|强连通分量+最长路】POJ-3592 Instantaneous Transference

Instantaneous Transference

Time Limit: 5000MS Memory Limit: 65536K

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

Source

South Central China 2008 hosted by NUDT



题意: 给出一个有向图,其中每个点向自己下面的和右边的那个点有边相连。’*’号的点可以进行瞬间转移(可以选择转移或者不转移),每个点有点权且不为负。’#’号的点是不能到达的点。每到一个点获得它的权值且仅一次,求出能获得的最大点权和。

思路: 其实本来我是拒绝用强连通分量做这道题的,但是导演说,做完加特技……Duang~

看了样例就会发现这个求最长路的题目存在正环。那么用强连通分量可以把所有的环缩成一个点,然后重新建图,跑一遍spfa即可。

注意: ‘#’结点不向其它的点连边即可,一开始我是其它的点不向’#’连边,WA了,我猜是因为有些瞬间转移牵扯到了’#’。

代码如下:

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 2e3+5, M = 3*N;
int r, c;
int n;
int head[N], dfn[N], scc_id[N], sum[N];
int scc_cnt, deep, tot, poi;
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];

void init()
{
    poi = tot = deep = scc_cnt = 0;
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(scc_id, 0, sizeof(scc_id));
    memset(sum, 0, sizeof(sum));
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

int w[N], trans[N];
void input()
{
    char str[50];
    for(int i = 0; i < r; i++) {
        scanf("%s", str);
        for(int j = 0; j < c; j++) {
            int u = i * c + j;
            if(str[j] == ‘*‘) {
                w[u] = 0;
                trans[poi++] = u;
            }
            else if(str[j] == ‘#‘) {
                w[u] = -1;
            }
            else {
                w[u] = str[j] - ‘0‘;
            }
        }
    }
}

void build()
{
    for(int i = 0; i < r; i++) {
        for(int j = 0; j < c; j++) {
            int u = i * c + j;
            if(~w[u]) {
                if(j != c-1) add(u, u+1);
                if(i != r-1) add(u, u+c);
            }
        }
    }
    int x, y;
    for(int i = 0; i < poi; i++) {
        scanf("%d%d", &x, &y);
        int v = x * c + y;
        add(trans[i], v);
    }
}

stack <int> s;
int dfs(int u)
{
    int lowu = dfn[u] = ++deep;
    s.push(u);
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!dfn[v]) {
            int lowv = dfs(v);
            lowu = min(lowu, lowv);
        }
        else if(!scc_id[v]) {
            lowu = min(lowu, dfn[v]);
        }
    }
    if(lowu == dfn[u]) {
        scc_cnt++;
        while(1) {
            int x = s.top(); s.pop();
            scc_id[x] = scc_cnt;//nmuber from 1
            sum[scc_cnt] += w[x];
            if(x == u) break;
        }
    }
    return lowu;
}

void tarjan()
{
    for(int i = 0; i < n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

vector <int> ef[N];
void rebuild()
{
    for(int i = 1; i <= scc_cnt; i++) {
        ef[i].clear();
    }
    for(int u = 0; u < n; u++) {
        for(int i = head[u]; ~i; i = e[i].next) {
            int v = e[i].v;
            if(scc_id[u] != scc_id[v]) {
                ef[scc_id[u]].PB(scc_id[v]);
            }
        }
    }
}

int dis[N];//!
bool inq[N];//!
void spfa(int st)
{
    memset(dis, 0, sizeof(dis));
    queue <int> q;
    q.push(st);
    inq[st] = true;
    dis[st] = sum[st];
    while(!q.empty()) {
        int u = q.front(); q.pop();
        inq[u] = false;
        size_t len = ef[u].size();
        for(size_t i = 0; i < len; i++) {
            int v = ef[u][i];
            if(dis[v] < dis[u] + sum[v]) {
                dis[v] = dis[u] + sum[v];
                if(!inq[v]) {
                    q.push(v);
                    inq[v] = true;
                }
            }
        }
    }
}

int solve()
{
    rebuild();
    spfa(scc_id[0]);
    int ret = 0;
    for(int i = 1; i <= scc_cnt; i++) {
        ret = max(ret, dis[i]);
    }
    return ret;
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &r, &c);
        n = r * c;
        init();
        input();
        build();
        tarjan();
        int ans = solve();
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-10 21:14:17

【连通图|强连通分量+最长路】POJ-3592 Instantaneous Transference的相关文章

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(强连通+DP)

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

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

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

POJ 3592 Instantaneous Transference Tarjan+SPFA

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

【连通图|强连通分量+缩点】POJ-1236 Network of Schools

Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes softwa

【连通图|强连通分量+dfs】POJ-3160 Father Christmas flymouse

Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Description After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contr

强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且可以传递, 即1欢迎2不代表2欢迎1, 但是如果2也欢迎3那么1也欢迎3. 求被所有牛都欢迎的牛的数量. 限制: 1 <= N <= 10000 1 <= M <= 50000 思路: Kosaraju算法, 看缩点后拓扑序的终点有多少头牛, 且要判断是不是所有强连通分量都连向它. Kosaraju算法,分拆完连通分量后,也完成了拓扑序. /*poj 2186

TarJan 算法求解有向连通图强连通分量

[有向图强连通分量] 在有向图G中,如果两个 顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components). 下图中,子图{1,2,3,4}为一个强连通分量,因为顶点1,2,3,4两两可达.{5},{6}也分别是两个强连通分量. 大体来说有3中算法Kosaraju,Trajan,Gabow这三种!后续文章中将相继

(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