POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
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缩点新建图 && SPFA求最长路 && 经典】的更多相关文章
- poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6204 Accep ...
- POJ 3592 Instantaneous Transference(强连通+DP)
POJ 3592 Instantaneous Transference 题目链接 题意:一个图.能往右和下走,然后有*能够传送到一个位置.'#'不能走.走过一个点能够获得该点上面的数字值,问最大能获得 ...
- POJ 3592 Instantaneous Transference(强联通分量 Tarjan)
http://poj.org/problem?id=3592 题意 :给你一个n*m的矩阵,每个位置上都有一个字符,如果是数字代表这个地方有该数量的金矿,如果是*代表这个地方有传送带并且没有金矿,可以 ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- poj 3592 Instantaneous Transference 缩点+最长路
题目链接 给一个n*m的图, 从0, 0这个点开始走,只能向右和向下. 图中有的格子有值, 求能获得的最大值. 其中有些格子可以传送到另外的格子, 有些格子不可以走. 将图中的每一个格子都看成一个点, ...
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3007 Accep ...
- poj 3592 Instantaneous Transference
http://poj.org/problem?id=3592 #include <cstdio> #include <cstring> #include <algorit ...
- POJ 2762--Going from u to v or from v to u?【scc缩点新建图 && 推断是否是弱连通图】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15755 ...
- poj3592 Instantaneous Transference tarjan缩点+建图
//给一个n*m的地图.坦克从(0 , 0)開始走 //#表示墙不能走,*表示传送门能够传送到指定地方,能够选择也能够选择不传送 //数字表示该格的矿石数, //坦克从(0,0)開始走.仅仅能往右和往 ...
随机推荐
- Gym - 100338E Numbers 贪心
Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsign ...
- 浅谈 C 语言中模块化设计的范式
今天继续谈模块化的问题.这个想慢慢写成个系列,但是不一定连续写.基本是想起来了,就整理点思路出来.主要还是为以后集中整理做点铺垫. 我们都知道,层次分明的代码最容易维护.你可以轻易的换掉某个层次上的某 ...
- Linux常用视频播放器
1.SMplayer是一款跨平台的视频播放工具,可以支持大部分的视频和音频文件.它支持音频轨道切换.允许调节亮度.对比度.色调.饱和度.伽玛值,按照倍速.4倍速等多种速度回放.还可以进行音频和字幕延迟 ...
- C/C++(C++返回对象与应用区别,类成员的存储)
返回对象与应用区别: 拷贝构造器发生的时机: 1.构造新对象 A a, A b = a; 2.传参或返回对象 对于普通变量来说,传引用效果不是很明显,对于类对象而言,传对象效果很高. 传引用等价于扩大 ...
- vue踩坑- 报错npm ERR! cb() never called!
在vue项目中引入饿了么elementUI组件的步骤之中,出现以下的错误: D:\my-project-first>npm i element-ui -S Unhandled rejection ...
- HDOJ 5306 Gorgeous Sequence 线段树
http://www.shuizilong.com/house/archives/hdu-5306-gorgeous-sequence/ Gorgeous Sequence Time Limit: 6 ...
- Python下opencv使用笔记(一)(图像简单读取、显示与储存)
写在之前 从去年開始关注python这个软件,途中间间断断看与学过一些关于python的东西.感觉python确实是一个简单优美.easy上手的脚本编程语言,众多的第三方库使得python异常的强大. ...
- js02---字符串
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Python 面向对象 —— super 的使用(Python 2.x vs Python 3.x)
注意区分当前的 Python 版本是 2.X 还是 3.X,Python 3.X 在 super 的使用上较之 Python 2.X 有较大的变化: 1. Python 2.x class Conta ...
- POJ 2226 二分图最小覆盖
题意: 思路: 把横着的连通块放在一个集合 竖着的放在一个集合 如果有交 就连边 求最小覆盖即可 (数值上等于最大匹配) //By SiriusRen #include <cstdio> ...