题目: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. 15、python之导入模块

    一.什么是模块? 模块本质是一个py文件,我们可以通过关键字import将py文件对象导入到当前名称空间. 二.导入模块 1.import module 2.from module import ob ...

  2. Enhacing the content with JavaScript

    What not to do :  In theory , you could use JavaScript to add important content to a web page. Howev ...

  3. 九、MySQL 5.7.9版本sql_mode=only_full_group_by问题

    MySQL 5.7.9版本sql_mode=only_full_group_by问题 用到GROUP BY 语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...

  4. [WC2002][洛谷P1578]奶牛浴场

    洛谷题解里那个人可真是话多呢. 题目描述 由于John建造了牛场围栏,激起了奶牛的愤怒,奶牛的产奶量急剧减少.为了讨好奶牛,John决定在牛场中建造一个大型浴场.但是John的奶牛有一个奇怪的习惯,每 ...

  5. 4.bootstrap的form表单的form-group和form-control的区别与联系

    1. form-group一般用于div form-control一般用于置于div中的标签元素,为了让控件在各种表单风格中样式不出错,需要添加类名“form-control”,如: <form ...

  6. java文件基本操作

    public static void main(String [] args) { try { /* * File类 */ /*String directory = "D:/Workspac ...

  7. LCS+LIS

    #include<iostream> #include<string> using namespace std; string a,b; ][]; int main() { w ...

  8. nyoj 题目44 子串和

    子串和 时间限制:5000 ms  |  内存限制:65535 KB 难度:3   描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...

  9. .Net MVC无限循环或无限递归

    错误往往是service的相互引用之类的. 好好排查

  10. 像Excel的表格table

    推荐:Spread.js 地址:点击打开链接 Demo:点击打开链接