POJ 3592 Instantaneous Transference 图论算法tarjan+spfa

46 篇文章 0 订阅
19 篇文章 0 订阅

题意:就是在红警当中的超时空矿车,它可以传送到别的地方,求矿车在这个矩形中可以采到的最多的矿产资源。

注意: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值