POJ 3592 Instantaneous Transference

题目链接

题意:一个图。能往右和下走,然后有*能够传送到一个位置。'#'不能走。走过一个点能够获得该点上面的数字值,问最大能获得多少

思路:因为有环先强连通缩点。然后问题转化为dag,直接dp就可以

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std; const int N = 1605;
const int d[2][2] = {0, 1, 1, 0}; int t, n, m, val[N];
char str[45][45];
vector<int> g[N], scc[N];
stack<int> S; int pre[N], dfn[N], dfs_clock, sccno[N], sccn, scc_val[N]; void dfs_scc(int u) {
pre[u] = dfn[u] = ++dfs_clock;
S.push(u);
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!pre[v]) {
dfs_scc(v);
dfn[u] = min(dfn[u], dfn[v]);
} else if (!sccno[v]) dfn[u] = min(dfn[u], pre[v]);
}
if (dfn[u] == pre[u]) {
sccn++;
int sum = 0;
while (1) {
int x = S.top(); S.pop();
sum += val[x];
sccno[x] = sccn;
if (u == x) break;
}
scc_val[sccn] = sum;
}
} void find_scc() {
dfs_clock = sccn = 0;
memset(pre, 0, sizeof(pre));
memset(sccno, 0, sizeof(sccno));
for (int i = 0; i < n * m; i++)
if (!pre[i]) dfs_scc(i);
} int dp[N]; int dfs(int u) {
if (dp[u] != -1) return dp[u];
dp[u] = 0;
for (int i = 0; i < scc[u].size(); i++) {
int v = scc[u][i];
dp[u] = max(dp[u], dfs(v));
}
dp[u] += scc_val[u];
return dp[u];
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n * m; i++) g[i].clear();
for (int i = 0; i < n; i++)
scanf("%s", str[i]);
memset(val, 0, sizeof(val));
int a, b;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (str[i][j] == '#') continue;
if (str[i][j] >= '0' && str[i][j] <= '9') val[i * m + j] = str[i][j] - '0';
if (str[i][j] == '*') {
scanf("%d%d", &a, &b);
g[i * m + j].push_back(a * m + b);
}
for (int k = 0; k < 2; k++) {
int x = i + d[k][0];
int y = j + d[k][1];
if (x < 0 || x >= n || y < 0 || y >= m || str[x][y] == '#') continue;
g[i * m + j].push_back(x * m + y);
}
}
}
find_scc();
for (int i = 1; i <= sccn; i++) scc[i].clear();
for (int u = 0; u < n * m; u++) {
for (int j = 0; j < g[u].size(); j++) {
int v = g[u][j];
if (sccno[u] == sccno[v]) continue;
scc[sccno[u]].push_back(sccno[v]);
}
}
memset(dp, -1, sizeof(dp));
printf("%d\n", dfs(sccno[0]));
}
return 0;
}

POJ 3592 Instantaneous Transference(强连通+DP)的更多相关文章

  1. poj 3592 Instantaneous Transference 【SCC +缩点 + SPFA】

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

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

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

  3. poj 3592 Instantaneous Transference

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

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

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

  5. Instantaneous Transference(强连通分量及其缩点)

    http://poj.org/problem?id=3592 题意:给出一个n*m的矩阵,左上角代表起始点,每个格子都有一定价值的金矿,其中‘#’代表岩石不可达,‘*’代表时空门可以到达指定格子,求出 ...

  6. POJ3592 Instantaneous Transference 强连通+最长路

    题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...

  7. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  8. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  9. poj 1185(状态压缩DP)

    poj  1185(状态压缩DP) 题意:在一个N*M的矩阵中,‘H'表示不能放大炮,’P'表示可以放大炮,大炮能攻击到沿横向左右各两格,沿纵向上下各两格,现在要放尽可能多的大炮使得,大炮之间不能相互 ...

随机推荐

  1. WebView Js注入

    注入前: 注入后: 主界面: package com.example.webviewjsdemo; import android.os.Bundle; import android.app.Activ ...

  2. Python Schema使用说明

    转自https://segmentfault.com/a/1190000011777230 Python Schema使用说明 Schema是什么? 不管我们做什么应用,只要和用户输入打交道,就有一个 ...

  3. Kali linux2.0里Metasploit的服务类型探测

    不多说,直接上干货! 在MSF终端中,可以输入search name:_version命令查看所有可用的服务查点模块 该命令的执行结果如下: root@kali:~# msfconsole ..... ...

  4. java中移位操作

    /** * * @author SunRain *2013-10-14 8:09:50 *在最后一个移位运算中,结果没有直接付给b,而是直接打印出来,所以结果是正确的, *其他的是会被先转换成int型 ...

  5. springMVC学习笔记_转载(一)-----springMVC原理

    阅读目录 一.什么是springmvc 二.mvc在b/s系统下的应用 三.SpringMVC框架介绍 回到顶部 一.什么是springmvc springMVC是spring框架的一个模块,spri ...

  6. 洛谷 P2486 [SDOI2011]染色 LCT

    Code: #include <cstdio> //SDOI2010 染色 #include <algorithm> #include <cstring> #inc ...

  7. git rebase 的使用 (用于撤销某次commit)

    Q: I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files. ...

  8. linu基础入门(一)

    原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10730550.html 本人小白,有错指出.谢谢! 一:根据上一步安装与新 ...

  9. 在SSM框架中我设置拦截器filter不能通过注解获取到实现类

    我在用注解注入实现类的时候,出现了这样的错误:如以下截图: 这个地方报出的错误是说明我的一个接口类型的类没有获取到,后来我就想要是我的实现类没有获取到那么我就直接new一个实现类然后再进行调用就会出现 ...

  10. vue2.0 vue-loader

    vue-cli npm install 脚手架: vue-loader 1.0 -> new Vue({ el: '#app', components:{App} }) 2.0-> new ...