题目:http://codevs.cn/problem/1611/

  关于题解请戳这里:http://www.cnblogs.com/hadilo/p/5892765.html

  下面给一个可以A的代码,由于没有无限栈只能用手写栈Tarjan,上面题解里给的代码是递归栈的Tarjan,会RE

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#define nil 0
#define N 500005
using namespace std; int n, m, csh[N], S, P, f[N];
int u[N << ], v[N << ], nxt[N << ], pnt[N], e;
int dfn[N], low[N], isin[N], hcash[N], tot, indx;
bool instack[N], vis[N];
int Sta[N], stop;
vector <int> g[N];
stack <int> s;
void add(int a, int b)
{
u[++e] = a; v[e] = b;
nxt[e] = pnt[a]; pnt[a] = e;
}
void tarjan(int x)
{
s.push(x);
dfn[x] = low[x] = ++indx;
Sta[++stop] = x;
instack[x] = true;
while(!s.empty())
{
int t = s.top();
for(int i = pnt[t]; i != nil; i = nxt[i])
{
if(dfn[v[i]] == )
{
dfn[v[i]] = low[v[i]] = ++indx;
Sta[++stop] = v[i]; instack[v[i]] = true;
s.push(v[i]);
break;
}
}
if(t == s.top())
{
for(int i = pnt[t]; i != nil; i = nxt[i])
{
if(dfn[v[i]] > dfn[t]) low[t] = min(low[t], low[v[i]]);
else if(instack[v[i]]) low[t] = min(low[t], dfn[v[i]]);
}
if(dfn[t] == low[t])
{
++tot;
int j;
do
{
j = Sta[stop--];
instack[j] = false;
isin[j] = tot;
hcash[tot] += csh[j];
} while(j != t);
}
s.pop();
}
}
}
void init()
{
int a, b;
scanf("%d%d", &n, &m);
for(int i = ; i <= m; ++i)
{
scanf("%d%d", &a, &b);
add(a, b);
}
for(int i = ; i <= n; ++i)
{
scanf("%d", &csh[i]);
}
scanf("%d%d", &S, &P);
for(int i = ; i <= n; ++i)
{
if(dfn[i] == )
{
tarjan(i);
}
}
for(int i = ; i <= n; ++i)
{
for(int j = pnt[i]; j != nil; j = nxt[j])
{
if(isin[i] != isin[v[j]])
{
g[isin[i]].push_back(isin[v[j]]);
}
}
}
}
void work()
{
queue <int> Q;
memset(vis, , sizeof(vis));
memset(f, , sizeof(f));
f[isin[S]] = hcash[isin[S]];
Q.push(isin[S]);
vis[isin[S]] = true;
while(!Q.empty())
{
int t = Q.front();
Q.pop();
vis[t] = false;
for(int i = ; i < g[t].size(); ++i)
{
if(f[g[t][i]] < f[t] + hcash[g[t][i]])
{
f[g[t][i]] = f[t] + hcash[g[t][i]];
if(!vis[g[t][i]])
{
vis[g[t][i]] = true;
Q.push(g[t][i]);
}
}
}
}
int ans = , a;
for(int i = ; i <= P; ++i)
{
scanf("%d", &a);
ans = max(ans, f[isin[a]]);
}
printf("%d\n", ans);
}
int main()
{
init();
work();
return ;
}

版权所有,转载请联系作者,违者必究

QQ:740929894

CodeVS1611_APIO2009_抢掠计划_C++的更多相关文章

  1. BZOJ1179_APIO2009_抢掠计划_C++

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1179 一道用 Tarjan 缩点+SPFA 最长路的题(Tarjan 算法:http://ww ...

  2. P3627 [APIO2009]抢掠计划

    P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...

  3. 【洛谷P3627】[APIO2009]抢掠计划

    抢掠计划 题目链接 比较水的缩点模板题,Tarjan缩点,重新建图,记录联通块的钱数.是否有酒吧 DAG上记忆化搜索即可 #include<iostream> #include<cs ...

  4. 洛谷 P3627 【抢掠计划】

    题库:洛谷 题号:3627 题目:抢掠计划 link:https://www.luogu.org/problem/P3627 思路 : 这道题是一道Tarjan + 最长路的题.首先,我们用Tarja ...

  5. APIO2009 抢掠计划 Tarjan DAG-DP

    APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...

  6. 题解 P3627 【[APIO2009]抢掠计划】

    咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...

  7. [APIO2009]抢掠计划(Tarjan,SPFA)

    [APIO2009]抢掠计划 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是, ...

  8. [APIO2009]抢掠计划

    题面: Description Siruseri城中的道路都是单向的.不同的道路由路口连接.按照法律的规定,在每个路口都设立了一个Siruseri银行的ATM取款机.令人奇怪的是,Siruseri的酒 ...

  9. 洛谷P3627[APOI2009] (讨厌的)抢掠计划

    题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...

随机推荐

  1. Elasticsearch和Head插件安装

    环境: CentOS7  Elasticsearch-6.3.2 JDK8 准备: JDK8 下载地址:http://www.oracle.com/technetwork/java/javase/do ...

  2. 无序数组中第K大的数

    1. 排序法 时间复杂度 O(nlogn) 2. 使用一个大小为K的数组arr保存前K个最大的元素 遍历原数组,遇到大于arr最小值的元素时候,使用插入排序方法,插入这个元素 时间复杂度,遍历是 O( ...

  3. BFS例题:A计划

    ContribContrib/a11y/accessibility-menu.js 关于 BFS要点: 1.若为可化为的坐标系图形,可用结构体存储其x值,y值和步数.(一般开now 和 next ,n ...

  4. [BZOJ1060][ZJOI2007]时态同步(树形DP)

    [我是传送门] 因为边权只能增加,那么设f[u]为u子树上从i出发到达某个叶节点的最大路径, 显然Ans应该增加f[u]-f[v]-e[i].w Code #include <cstdio> ...

  5. U2

    android的XML文件(包括layout下的和values下的)注释一般采用 <!--注释内容 -->的方式进行,也就是说,采用//是行不通的,不信你可以试试看.     在XML中, ...

  6. python-11多线程

    1-多任务可以由多进程完成,也可以由一个进程内的多线程完成. 1.1多线程代码示例 import time, threading def loop(): print("thread %s i ...

  7. TouTiao开源项目 分析笔记11 以总体到局部的思路 构建图片主列表

    1.构建图片主列表的整体片段PhotoTabLayout 1.1.首先创建一个PhotoTabLayout片段 public class PhotoTabLayout extends Fragment ...

  8. JAVA EE配TOMCAT

    纯粹就是吧百度教程上的过程走了一遍发现不行综合各种教程配出来了,四张图代表了四个阶段,以后再要配的话直接来这里看.

  9. Java Spring Controller 获取请求参数的几种方法

    技术交流群:233513714  1.直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交.若"Content-Type"=& ...

  10. 【Remove Duplicates from Sorted List 】cpp

    题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear o ...