Tyvj1139 向远方奔跑(APIO 2009 抢掠计划)
描述
输入格式
输出格式
测试样例1
输入
6 7
1 2
2 3
3 5
2 4
4 1
2 6
6 5
10
12
8
16
1
5
1 4
4 3 5 6
输出
47
备注
思路:
1、分析路可以重复走,想到强连通分量,如果强连通分量里面有一个点被访问,那么整个强连通分量都要被访问,否则答案偏小,于是先tarjan再缩点
2、缩点之后,整个图由一个有向有环图变为一个有向无环图,于是spfa处理,然后就AC了(bzoj上的好像能卡掉,WYW用tarjan写信息传递好像被卡了)
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#define maxn 3005
#define maxint ~0U>>1
using namespace std;
stack<int> sta;
vector<int> comp[maxn];
int incomp[maxn],instack[maxn],dfn[maxn],low[maxn];
int index,cnum;
int n,m,q,d[maxn],rice[maxn],noodle[maxn],v[maxn],s,p,ans;
vector<int> g[maxn],nowg[maxn];
int nowrice[maxn],nownoodle[maxn],nows;
void input(){
cin>>n>>m;
int u,v;
for(int i = ;i <= m;i++){
scanf("%d%d",&u,&v);
g[u].push_back(v); }
for(int i = ;i <= n;i++){
scanf("%d",&rice[i]);
}
cin>>s>>p;
int nnow;
for(int i = ;i <= p;i++){
scanf("%d",&nnow);
noodle[nnow] = ;
}
}
void tarjan(int u){
instack[u] = ;
dfn[u] = low[u] = ++index;
sta.push(u);
for(int i = ;i < g[u].size();i++){
int j = g[u][i];
if(!dfn[j]){
tarjan(j);
low[u] = min(low[u],low[j]);
}else if(instack[j] == ){
low[u] = min(low[u],dfn[j]);
}
}
if(dfn[u] == low[u]){
++cnum;
while(!sta.empty()){
int t = sta.top();
sta.pop();
instack[t] = ;
incomp[t] = cnum;
comp[cnum].push_back(t);
if(t == u){
break;
}
}
}
}
void work(){
for(int i = ;i <= n;i++){
if(!dfn[i]) tarjan(i);
}
int u,v,newu,newv;
for(int i = ;i <= n;i++){
u = i;
newu = incomp[u];
nowrice[newu] += rice[u];
if(noodle[u]) nownoodle[newu] = ;
if(u == s) nows = newu;
for(int j = ;j < g[u].size();j++){
v = g[u][j];
newv = incomp[v];
if(newu == newv) continue;
nowg[newu].push_back(newv);
}
}
}
void spfa(){
queue<int> q;
d[nows] = nowrice[nows];
v[nows] = ;
q.push(nows);
int xx,dd,to,wei;
while(!q.empty()){
xx = q.front();
dd = d[xx];
q.pop();
v[xx] = ;
if(nownoodle[xx]) ans = max(ans,dd);
for(int i = ;i < nowg[xx].size();i++){
to = nowg[xx][i];
wei = nowrice[to];
if(dd + wei > d[to]){
d[to] = dd + wei;
if(!v[to]){
v[to] = ;
q.push(to);
}
}
}
}
cout<<ans;
}
int main(){
input();
work();
spfa();
return ;
}
Tyvj1139 向远方奔跑(APIO 2009 抢掠计划)的更多相关文章
- P3627 [APIO2009]抢掠计划
P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...
- 【洛谷P3627】[APIO2009]抢掠计划
抢掠计划 题目链接 比较水的缩点模板题,Tarjan缩点,重新建图,记录联通块的钱数.是否有酒吧 DAG上记忆化搜索即可 #include<iostream> #include<cs ...
- 洛谷 P3627 【抢掠计划】
题库:洛谷 题号:3627 题目:抢掠计划 link:https://www.luogu.org/problem/P3627 思路 : 这道题是一道Tarjan + 最长路的题.首先,我们用Tarja ...
- APIO2009 抢掠计划 Tarjan DAG-DP
APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...
- 题解 P3627 【[APIO2009]抢掠计划】
咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...
- [APIO2009]抢掠计划(Tarjan,SPFA)
[APIO2009]抢掠计划 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是, ...
- [APIO2009]抢掠计划
题面: Description Siruseri城中的道路都是单向的.不同的道路由路口连接.按照法律的规定,在每个路口都设立了一个Siruseri银行的ATM取款机.令人奇怪的是,Siruseri的酒 ...
- 【BZOJ 1177】【APIO 2009】Oil
http://www.lydsy.com/JudgeOnline/problem.php?id=1177 前缀和优化,时间复杂度$O(nm)$ 因为数据不全,快速读入会导致RE,切记! #includ ...
- 洛谷P3627[APOI2009] (讨厌的)抢掠计划
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
随机推荐
- JPA中关联关系(OneToOne、OneToMany、ManyToMany,ManyToOne)映射代码片段
在使用Hibernate的时候我们常常会在类里边配置各种的关联关系,但是这个并不是很好配置,配置不当会出现各种各样的问题,下面具体来看一下: 首先我们来看User类里边有一个IdentityCard类 ...
- rman 问题
1. RMAN Repeatedly Fail To Backup Archivelogs with RMAN-20242 Cause: There is a mis-match between th ...
- Enumerable.Union<TSource> 方法
功能:生成两个序列的并集(使用默认的相等比较器). 命名空间: System.Linq 程序集: System.Core.dll 备注:实现此方法时使用了延迟执行. 它直接返回一个对象,该对象存储了执 ...
- [译]libcurl错误码
CURLcode Almost all "easy" interface functions return a CURLcode error code. No matter wha ...
- 微信JSSDK支付
var appId,timeStamp,nonceStr,package,signType,paySign; function goumai(){ $.confirm({ title: '确认购买', ...
- android studio java.io.IOException:setDataSourse fail.
这一次是针对Android开发中的一个小问题,权限获取的问题. 在写了一个一个小Android程序的时候,有时候普需要获取本机的文件(Audio&Video),这时候如果不加权限就会出现这种情 ...
- 【译】x86程序员手册25-7.1任务状态段
7.1 Task State Segment 任务状态段 All the information the processor needs in order to manage a task is st ...
- 4星|《OKR工作法》:关注公司的真正目标,以周为单位做计划和考核
本书篇幅比较小,两个小时就可以看完.主要内容讲OKR工作法的基本概念,然后用一个虚拟的创业公司的创业故事来演示实施OKR过程中可能遇到的问题.OKR给创业带来的好处. OKR工作法相对来说是比较简单的 ...
- nz-card头部右侧添加东西
<nz-card [nzBordered]="true" nzTitle="卡片标题" [nzExtra]="extraTemplate1&qu ...
- 00The C Programming Language
The C Programming Language C语言是一门面向过程.抽象化的通用程序设计语言,广泛应用于底层开发.C语言能以简易的方式编译.处理低级存储器.C语言是仅产生少量的机器语言以及不需 ...