POJ 3592 Instantaneous Transference 圖論演算法tarjan+spfa

畫船聽雨發表於2014-02-13

題意:就是在紅警當中的超時空礦車,它可以傳送到別的地方,求礦車在這個矩形中可以採到的最多的礦產資源。

注意:1、傳送的地方可以選擇,傳送和不傳送;2、就是每次都只能向右或者向下傳遞;

這道題目得建立兩次模型。

第一次:如果一個點是可以到達的(除了#的情況)那麼就建立一條有向邊指向它,表示可以到達,這裡要注意當是*的時候它既可以指向要傳送到的地方又可以到達下一點。這樣就可以建立起來一個有向圖。


然後把有向圖中的強聯通分量進行縮點,得到不同的縮點,再根據縮點之間的聯絡建立起來一個有向圖,再spfa求最長路,就是最大值。


Instantaneous Transference
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5367   Accepted: 1162

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 1000100;
using namespace std;

struct node
{
    int u;
    int v;
    int w;
    int next;
} edge1[maxn], edge2[maxn];
stack<int> q;
int low[maxn];//圖中的最小時間戳
int dfn[maxn];//到達這一步的的時候的時間戳
int isb[maxn];
int n, m, nums;
int t, dfs_clock;
int out[maxn];
int num[maxn];
int d[maxn];
int vis[maxn];
int w[maxn];//表示每個強聯通分量中的價值;
int Ore_worth[maxn];
int head1[maxn];
int head2[maxn];
int cnt1;

void init()
{
    t = 0;
    memset(num, 0 , sizeof(num));
    memset(head1, -1, sizeof(head1));
    memset(head2, -1, sizeof(head2));
    nums = 0;
    memset(low, 0 , sizeof(low));
    memset(dfn, 0 , sizeof(dfn));
    memset(w, 0 , sizeof(w));
    dfs_clock = 0;
    cnt1 = 0;
}
void add(int u, int v, int w, struct node edge[], int head[])
{
    edge[cnt1].u = u;
    edge[cnt1].v = v;
    edge[cnt1].w = w;
    edge[cnt1].next = head[u];
    head[u] = cnt1++;
}

void tarjan(int u)
{
    low[u] = dfn[u] = ++dfs_clock;
    q.push(u);
    for(int i = head1[u]; i != -1; i = edge1[i].next)
    {
        int v = edge1[i].v;
        if(!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(!num[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(low[u] == dfn[u])
    {
        ++nums;
        while(1)
        {
            int x = q.top();
            q.pop();
            num[x] = nums;
            if(x==u) break;
        }
    }
}

void spfa(int s)
{
    for(int i = 0; i <= n*m; i++)
    {
        vis[i] = 0;
        d[i] = -INF;
    }
    queue<int>qu;
    d[s] = 0;
    vis[s] = 1;
    qu.push(s);
    while(!qu.empty())
    {
        int u = qu.front();
        qu.pop();
        vis[u] = 0;
        for(int i = head2[u]; i != -1; i = edge2[i].next)
        {
            int v = edge2[i].v;
            int w = edge2[i].w;
            if(d[u]+w > d[v])
            {
                d[v] = d[u]+w;
                if(!vis[v])
                {
                    qu.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}
int main()
{
    char str[110][110];
    int k;
    cin >>k;
    while(k--)
    {
        init();
        int i;
        cin >>n>>m;
        for(i = 0; i < n; i++)
            cin >>str[i];
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                if(str[i][j] != '#')
                {
                    if(i)
                        add((i-1)*m+j, i*m+j , 0, edge1, head1);
                    if(j)
                        add(i*m+j-1, i*m+j, 0, edge1, head1);
                    if(str[i][j]=='*')
                    {
                        int x, y;
                        cin>>x>>y;
                        if(str[x][y]!='#')
                            add(i*m+j, x*m+y, 0, edge1, head1);
                    }
                }
        }
        for(i = 0; i < n*m; i++)
            if(!dfn[i])
                tarjan(i);
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(str[i][j]!='*' && str[i][j]!='#')
                {
                    int no = num[i*m+j];
                    w[no] += str[i][j]-'0';
                }
            }
        }
        for(int u = 0; u < n*m; u++)
        {
            for(int i = head1[u]; i != -1; i = edge1[i].next)
            {
                int v = edge1[i].v;
                if(num[u] != num[v])
                {
                    add(num[u], num[v], w[num[v]], edge2, head2);
                }
            }
        }
        spfa(num[0]);
        int _max = -1;
        for(i = 1; i <= nums; i++)
            if(d[i] > _max)
                _max = d[i];
        cout<<_max+w[num[0]]<<endl;
    }
    return 0;
}


相關文章