Instantaneous Transference
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6204   Accepted: 1389

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 hasX 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)点,题目保证起点一定不是"#"区域。

要求每次仅仅能向右走、向下走,当遇到传送点的时候能够选择传送到指定位置或者不传送。'*'位置的传送效果能够无限使用,问你从起点出发最多能採多少矿。

思路:

1,把N*M个位置虚拟成N*M个点。依据是否可达的关系建边 来构图。

2,对构成的图求SCC。并统计全部SCC里面的矿物数;

3,缩点后,构建新图。以起点所在的SCC为源点。SPFA求最长路。

注意:

1,'*'的传送位置可能是'#'(题目说了传送位置是合法的。所以传送位置不用考虑是否越界)。并且对于'*'位置,能够选择传送或者不传送。所以必需要向下、右建边(需要考虑越界)。

2。The next K lines
describe the specified target coordinates for the squares with '*', in the order from north to south then west to east.  对于出现的'*'。先逐行遍历,再逐列遍历。

好想的思路,好写的代码。不好理解的——两个实现过程为什么一个WA,一个AC。

不知道这两个实现最长路的建图 有什么不同,我算是无语了。 O__O "…   明天再好好想吧,今天有点不舒服。。

希望路过的大牛指点迷津。

代码一:

WA了, 我只是设了一个超级源点连通起点所在的SCC,边权为当前SCC的矿物数。

然后缩点建图时边权为终点SCC的矿物数。这样跑SPFA就WA。 曾经写POJ 3160用这样的写法就AC了。

struct Node//用于跑SPFA
{
int from, to, val, next;
};
Node node[MAXM];
int Head[MAXN], nodenum;
int val[MAXN];//记录每一个SCC的 矿物数
void addNode(int u, int v, int w)
{
Node E = {u, v, w, Head[u]};
node[nodenum] = E;
Head[u] = nodenum++;
}
void suodian()
{
nodenum = 0;
memset(Head, -1, sizeof(Head));
memset(val, 0, sizeof(val));
//求出每一个SCC里面全部的矿物数
for(int i = 1; i <= scc_cnt; i++)
{
int sum = 0;
for(int j = 0; j < scc[i].size(); j++)
sum += num[scc[i][j]];
val[i] = sum;
}
//保证从左上角出发,虚拟源点0 连通原图中起点所在的SCC 边权为当前SCC的矿物数
addNode(0, sccno[0], val[sccno[0]]);
for(int i = 0; i < edgenum; i++)
{
int u = sccno[edge[i].from];
int v = sccno[edge[i].to];
if(u != v)
addNode(u, v, val[v]);//建边
}
}
int dist[MAXN];
bool vis[MAXN];
void SPFA(int sx)//跑一遍SPFA 求最长路
{
queue<int> Q;
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
vis[sx] = true;
Q.push(sx);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = Head[u]; i != -1; i = node[i].next)
{
Node E = node[i];
if(dist[E.to] < dist[u] + E.val)
{
dist[E.to] = dist[u] + E.val;
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
sort(dist, dist+scc_cnt+1);
printf("%d\n", dist[scc_cnt]);
}

代码二:

AC代码的   —— 仅仅建边 不设置边权,用数组记录SCC的矿物数,借此来更新。

struct Node//用于跑SPFA
{
int from, to, next;
};
Node node[MAXM];
int Head[MAXN], nodenum;
int val[MAXN];//记录每一个SCC的 矿物数
void addNode(int u, int v)
{
Node E = {u, v,Head[u]};
node[nodenum] = E;
Head[u] = nodenum++;
}
void suodian()
{
nodenum = 0;
memset(Head, -1, sizeof(Head));
memset(val, 0, sizeof(val));
//求出每一个SCC里面全部的矿物数
for(int i = 1; i <= scc_cnt; i++)
{
int sum = 0;
for(int j = 0; j < scc[i].size(); j++)
sum += num[scc[i][j]];
val[i] = sum;
}
for(int i = 0; i < edgenum; i++)
{
int u = sccno[edge[i].from];
int v = sccno[edge[i].to];
if(u != v)
addNode(u, v);//建边
}
}
int dist[MAXN];
bool vis[MAXN];
void SPFA()//跑一遍SPFA 求最长路
{
queue<int> Q;
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
vis[sccno[0]] = true;
dist[sccno[0]] = val[sccno[0]];
Q.push(sccno[0]);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = Head[u]; i != -1; i = node[i].next)
{
Node E = node[i];
if(dist[E.to] < dist[u] + val[E.to])
{
dist[E.to] = dist[u] + val[E.to];
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
sort(dist, dist+scc_cnt+1);
printf("%d\n", dist[scc_cnt]);
}

没想通o(╯□╰)o

AC代码:0ms

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 2000+10
#define MAXM 3000000+10
#define INF 0x3f3f3f
using namespace std;
struct Edge
{
int from, to, next;
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int sccno[MAXN], scc_cnt;
stack<int> S;
vector<int> scc[MAXN];
bool Instack[MAXN];
int N, M;
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
int point(int x, int y)
{
return x * M + y;//这里写反了 RE2次。。。 }
void addEdge(int u, int v)
{
Edge E = {u, v, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
bool judge(int x, int y)//推断越界 写多了。。。
{
return x >= 0 && x < N && y >= 0 && y < M;
}
int num[MAXN];//存储相应位置的 矿物数目
void getMap()
{
scanf("%d%d", &N, &M);
int move[2][2] = {0,1, 1,0};
char str[50][50];
int x[MAXN], y[MAXN];
memset(num, 0, sizeof(num));
int k = 0;
for(int i = 0; i < N; i++)
{
scanf("%s", str[i]);
for(int j = 0; j < M; j++)
if(str[i][j] == '*')
k++;
}
for(int i = 0; i < k; i++)
scanf("%d%d", &x[i], &y[i]);
k = 0;
for(int i = 0; i < N; i++)//注意这里 先遍历行 再遍历列
{
for(int j = 0; j < M; j++)
{
if(str[i][j] == '#')//不能走
continue;
for(int p = 0; p < 2; p++)//两个方向
{
int a = i + move[p][0];
int b = j + move[p][1];
if(judge(a, b) && str[a][b] != '#')//能走且不能越界 连通
addEdge(point(i, j), point(a, b));
}
if(str[i][j] != '*')//数字 有矿物
num[point(i, j)] = str[i][j] - '0';//记录矿物数
else//连通 传送位置
{
if(str[x[k]][y[k]] != '#')
addEdge(point(i, j), point(x[k], y[k]));//由于这里[k++] WA N次
k++;
}
}
}
}
void tarjan(int u, int fa)
{
int v;
low[u] = dfn[u] = ++dfs_clock;
S.push(u);
Instack[u] = true;
for(int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if(!dfn[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u])
{
scc_cnt++;
scc[scc_cnt].clear();
for(;;)
{
v = S.top(); S.pop();
Instack[v] = false;
sccno[v] = scc_cnt;
sumval[scc_cnt] += num[v];
scc[scc_cnt].push_back(v);
if(v == u) break;
}
}
}
void find_cut(int l, int r)
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(sccno, 0, sizeof(sccno));
memset(sumval, 0, sizeof(sumval));
memset(Instack, false, sizeof(Instack));
dfs_clock = scc_cnt = 0;
for(int i = l; i <= r; i++)
if(!dfn[i]) tarjan(i, -1);
}
struct Node//用于跑SPFA
{
int from, to, next;
};
Node node[MAXM];
int Head[MAXN], nodenum;
int val[MAXN];//记录每一个SCC的 矿物数
void addNode(int u, int v)
{
Node E = {u, v,Head[u]};
node[nodenum] = E;
Head[u] = nodenum++;
}
void suodian()
{
nodenum = 0;
memset(Head, -1, sizeof(Head));
memset(val, 0, sizeof(val));
//求出每一个SCC里面全部的矿物数
for(int i = 1; i <= scc_cnt; i++)
{
int sum = 0;
for(int j = 0; j < scc[i].size(); j++)
sum += num[scc[i][j]];
val[i] = sum;
}
//为什么这样写 会WA??? 坑死我了
//保证从左上角出发。虚拟源点0 连通原图中起点所在的SCC 边权为当前SCC的矿物数
//addNode(0, sccno[0], val[sccno[0]]);
for(int i = 0; i < edgenum; i++)
{
int u = sccno[edge[i].from];
int v = sccno[edge[i].to];
if(u != v)
addNode(u, v);//建边
}
}
int dist[MAXN];
bool vis[MAXN];
void SPFA()//跑一遍SPFA 求最长路
{
queue<int> Q;
memset(dist, 0, sizeof(dist));
memset(vis, false, sizeof(vis));
vis[sccno[0]] = true;
dist[sccno[0]] = val[sccno[0]];
Q.push(sccno[0]);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = Head[u]; i != -1; i = node[i].next)
{
Node E = node[i];
if(dist[E.to] < dist[u] + val[E.to])
{
dist[E.to] = dist[u] + val[E.to];
if(!vis[E.to])
{
vis[E.to] = true;
Q.push(E.to);
}
}
}
}
sort(dist, dist+scc_cnt+1);
printf("%d\n", dist[scc_cnt]);
}
void solve()
{
find_cut(0, N*M-1);
suodian();
SPFA();
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
init();
getMap();
solve();
}
return 0;
}

poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】的更多相关文章

  1. POJ 3592 Instantaneous Transference(强连通+DP)

    POJ 3592 Instantaneous Transference 题目链接 题意:一个图.能往右和下走,然后有*能够传送到一个位置.'#'不能走.走过一个点能够获得该点上面的数字值,问最大能获得 ...

  2. poj 3592 Instantaneous Transference 缩点+最长路

    题目链接 给一个n*m的图, 从0, 0这个点开始走,只能向右和向下. 图中有的格子有值, 求能获得的最大值. 其中有些格子可以传送到另外的格子, 有些格子不可以走. 将图中的每一个格子都看成一个点, ...

  3. POJ 3592 Instantaneous Transference(强联通分量 Tarjan)

    http://poj.org/problem?id=3592 题意 :给你一个n*m的矩阵,每个位置上都有一个字符,如果是数字代表这个地方有该数量的金矿,如果是*代表这个地方有传送带并且没有金矿,可以 ...

  4. poj 3592 Instantaneous Transference

    http://poj.org/problem?id=3592 #include <cstdio> #include <cstring> #include <algorit ...

  5. bzoj 1179 [Apio2009]Atm——SCC缩点+spfa

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...

  6. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  7. P3119 [USACO15JAN]草鉴定[SCC缩点+SPFA]

    题目描述 约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从1号草场出发,最后回到1号草场.她想经过尽可能多的草场,贝西 ...

  8. poj3592 Instantaneous Transference tarjan缩点+建图

    //给一个n*m的地图.坦克从(0 , 0)開始走 //#表示墙不能走,*表示传送门能够传送到指定地方,能够选择也能够选择不传送 //数字表示该格的矿石数, //坦克从(0,0)開始走.仅仅能往右和往 ...

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

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

随机推荐

  1. hdu5790

    都快忘了在这类题的经典做法了…… 将字符串一个个的插入字典树,在字典树维护好有该前缀串s的最大编号字符串j,我们记作j控制了前缀串s 对于当前的第i个字符串,维护此时有当前每个字符串控制了多少个前缀串 ...

  2. AC日记——pigs poj 1149

    POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...

  3. AC日记——斐波那契数列(升级版) 洛谷 P2626

    斐波那契数列(升级版) 思路: 水题: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...

  4. 使用CreateRemoteThread把代码远程注入指定exe执行

    由于本人也是新手,如果有朋友不懂windows api相关知识,我相信查阅书籍或者百度会比我说有帮助的多,下面就我所做简单复述一下过程,欢迎指正缺点. 效果图示如下: 做的这个例子首先是创建了一个MF ...

  5. Response 部分功能

    设置状态码的方法:    void setStatus(int sc)     void setStatus(int sc, String sm) 设置响应头的方法:    void setHeade ...

  6. 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)

    A. An Olympian Math Problem 54.28% 1000ms 65536K   Alice, a student of grade 66, is thinking about a ...

  7. 基于 Python + OpenCV 进行人脸识别,视频追踪代码全注释

    先来普及一下概念, 计算机对人脸是如何识别的呢? 或者说图像是如何识别的.主要是获取单张图片的特征值记录了特征值以后,如果下一张图片来了以后两张图片特征值进行对比,如果相似度很高那么计算机就认定这两个 ...

  8. 【最短路】【Heap-Dijkstra】【分层图】bzoj2662 [BeiJing wc2012]冻结

    裸的分层图最短路. #include<cstdio> #include<cstring> #include<queue> #include<algorithm ...

  9. 【bzoj1370】【团伙】原来并查集还能这么用?!

    (画师当然是武内崇啦) Description 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一 ...

  10. 【R笔记】R语言利器之ddply

    ddply()函数位于plyr包,用于对data.frame进行分组统计,与tapply有些类似 准备数据 # 使用stringsAsFactors=F来防止data.frame把向量转为factor ...