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. thinkphp 中模型究竟是什么用?

    thinkphp 中模型究竟是什么用? 问题 似乎所有的操作都能在控制器中就能完成,模型除了几种验证之外,究竟是干什么用的,这个问题一直没理解透 解答 解答一 要明白这个问题,必须了解 MVC 历史. ...

  2. 前端到后台ThinkPHP开发整站--php开发案例

    前端到后台ThinkPHP开发整站--php开发案例 总结 还是需要做几个案例,一天一个为佳,那样才能做得快. 从需求分析着手,任务体系要构建好,这样才能非常高效. 转自: 前端到后台ThinkPHP ...

  3. Android属性动画-Interpolator和ViewPropertyAnimator的用法

    Interpolator的用法 Interpolator这个东西很难进行翻译,直译过来的话是补间器的意思,它的主要作用是可以控制动画的变化速率,比如去实现一种非线性运动的动画效果.那么什么叫做非线性运 ...

  4. VS2013+PTVS,python编码问题

    1.调试,input('中文'),乱码2.调试,print('中文'),正常3.不调试,input('中文'),正常4.不调试,print('中文'),正常 页面编码方式已经加了"# -- ...

  5. Slimming Paint (a.k.a. Redesigning Painting and Compositing)

    Slimming Paint is a Paint team project to re-implement the Blink<->cc picture recording API to ...

  6. caioj 1070 动态规划入门(二维一边推3:字符距离)(最长公共子序列拓展)

    复制上一题总结 caioj 1069到1071 都是最长公共字序列的拓展,我总结出了一个模型,屡试不爽    (1) 字符串下标从1开始,因为0用来表示字符为空的情况,而不是第一个字符     (2) ...

  7. Unity C# 设计模式(一)单例模式

    动机(Motivation):    在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性.以及良好的效率 意图:    保证一个类仅有一个实例,并提供一 ...

  8. rhcs clustat

    http://www.cnblogs.com/jyzhao/p/4775942.html https://access.redhat.com/documentation/zh-CN/Red_Hat_E ...

  9. 洛谷—— P2419 [USACO08JAN]牛大赛Cow Contest

    https://www.luogu.org/problem/show?pid=2419 题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, convenie ...

  10. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...