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. uva_127,栈以及vector的应用

    参考自http://www.cnblogs.com/maqiang/archive/2012/05/02/2479760.html #include <iostream> #include ...

  2. php中的页面跳转和重定向

    php中的页面跳转和重定向 ThinkPHP中跳转和重定向的区别 跳转: 浏览器认为: 当前URL请求成功, 重新请求新的URL. 浏览器会 记录当前的URL 和 新的URL 在请求历史记录中. 回退 ...

  3. String methods

    A method is similar to a function – it takes arguments and returns a value – but the syntax is diffe ...

  4. 泪奔,配好了bioconductor环境

    最近因为极度忙,没有写总结.今天补一下总结. 今天完成关静最后给的大project这个作业来说,结合自己的研究方向是个让我纠结一周多的事.好在找到了对应的研究内容. R的书目前还是很多的.R我一开始觉 ...

  5. sql笔试题-1

    在oracle下sql:比较巧妙地是group by 部分 E from (select a.team,b.y from nba a,nba b ) c group by (c.y-rownum) o ...

  6. html局部页面打印

    1.局部打印函数. function preview(oper) { if (oper < 10) { bdhtml=window.document.body.innerHTML;//获取当前页 ...

  7. Design Doc: Session History for Out-of-Process iframes

    Design Doc: Session History for Out-of-Process iframes Charlie Reis, May 2014 This document outlines ...

  8. VC:当前不会命中断点,还没有为该文档载入不论什么符号

    VS2013中设置的断点无效:"当前不会命中断点,还没有为该文档载入不论什么符号".问题主要出在没有生成调试信息.解决方法例如以下: (1)项目-〉属性-〉配置属性-〉C/C++- ...

  9. HDU 4714 Tree2cycle(树型DP)

    解题思路: 将一棵树变成一个环.假设一个结点的分叉数目大于等于2.则将它与父节点断开.而且断开子结点数目sum - 2条边,并再次连接sum-2个儿子形成一条直链然后这条游离链与还有一条游离链相连,共 ...

  10. shu_1171 十-&gt;二进制转换(输入输出控制)

    cid=1079&pid=19">http://202.121.199.212/JudgeOnline/problem.php?cid=1079&pid=19 分析:主 ...