APIO2009 抢掠计划 Tarjan DAG-DP
APIO2009 抢掠计划 Tarjan spfa/DAG-DP
一道\(Tarjan\)缩点水题。因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或者dp就好了。
在DAG上跑dp,复杂度\(O(n)\),而\(spfa\)为\(O(kn)\),所以还是优先选择dp(当然后面有\(spfa\)的代码)
拓扑时搞DP,\(f[i]\)表示在DAG上\(i\)节点时,当前最大钱数,转移\(f[v]=max(f[v], f[u]+w[v])\)
#include <cstdio>
#include <queue>
#define MAXN 500005
#define MIN(A,B) ((A)<(B)?(A):(B))
#define MAX(A,B) ((A)>(B)?(A):(B))
using namespace std;
int n,m,sta,p;
bool hav[MAXN],col_hav[MAXN];
int head[MAXN],nxt[MAXN],vv[MAXN],tot;
inline void add_edge(int u, int v){
vv[++tot]=v;
nxt[tot]=head[u];
head[u]=tot;
}
int s[MAXN],top;
bool ins[MAXN];
int col[MAXN],col_cnt;
int val[MAXN],col_val[MAXN];
int low[MAXN],dfn[MAXN],cnt;
void tarjan(int u){
dfn[u]=++cnt;
low[u]=cnt;
s[++top]=u;
ins[u]=1;
for(register int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(dfn[v]==0){
tarjan(v);
low[u]=MIN(low[u], low[v]);
}else if(ins[v]){
low[u]=MIN(low[u], dfn[v]);
}
}
if(dfn[u]==low[u]){
col[u]=++col_cnt;
ins[u]=0;
col_val[col_cnt]=val[u];
while(s[top]!=u){
col[s[top]]=col_cnt;
ins[s[top]]=0;
col_val[col_cnt]+=val[s[top]];
top--;
}
top--;
}
}
int head2[MAXN],nxt2[MAXN],vv2[MAXN],tot2;
inline void add_edge2(int u, int v){
vv2[++tot2]=v;
nxt2[tot2]=head2[u];
head2[u]=tot2;
}
int rdu[MAXN];
inline void build(){
for(register int u=1;u<=n;++u)
if(dfn[u]!=0){
for(register int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(col[u]==col[v]) continue;
rdu[col[v]]++;
add_edge2(col[u], col[v]);
}
}
}
queue <int> q;
int f[MAXN],ans=0;
void dp(){
q.push(col[sta]);f[col[sta]]=col_val[col[sta]];
while(!q.empty()){
int u=q.front();q.pop();
for(register int i=head2[u];i;i=nxt2[i]){
int v=vv2[i];
f[v]=MAX(f[v], f[u]+col_val[v]);
if((--rdu[v])==0) q.push(v);
}
}
for(register int i=1;i<=col_cnt;++i)
if(col_hav[i]) ans=MAX(f[i], ans);
}
int main()
{
scanf("%d %d", &n, &m);
while(m--){
int a,b;scanf("%d %d", &a, &b);
add_edge(a,b);
}
for(register int i=1;i<=n;++i) scanf("%d", &val[i]);
scanf("%d %d", &sta, &p);
while(p--){
int x;scanf("%d", &x);
hav[x]=1;
}
tarjan(sta);
for(register int i=1;i<=n;++i)
if(hav[i]) col_hav[col[i]]=1;
build();
dp();
printf("%d", ans);
return 0;
}
但是比赛时,拓扑dp写炸了,于是换成了\(spfa\),下面是\(spfa\)的写法:
#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 500005
#define MAXM 500005
#define MIN(A,B) ((A)<(B)?(A):(B))
#define MAX(A,B) ((A)>(B)?(A):(B))
using namespace std;
int n,m,val[MAXM],sta,p;
int head[MAXM],nxt[MAXM],vv[MAXM],tot;
inline void add_edge(int u, int v){
vv[++tot]=v;
nxt[tot]=head[u];
head[u]=tot;
}
int low[MAXN],dfn[MAXN],cnt;
int s[MAXN],top;
int col[MAXN],col_cnt;
long long col_val[MAXN];
bool ins[MAXN];
bool col_can[MAXN];
bool hav[MAXN];
bool col_hav[MAXN];
void tarjan(int u){
ins[u]=1;
dfn[u]=low[u]=++cnt;
s[++top]=u;
for(register int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(dfn[v]==0){
tarjan(v);
low[u]=MIN(low[u], low[v]);
}else if(ins[v]){
low[u]=MIN(low[u], dfn[v]);
}
}
if(low[u]==dfn[u]){
ins[u]=0;
col[u]=++col_cnt;
col_val[col_cnt]=val[u];
if(hav[u]) col_hav[col_cnt]=1;
while(s[top]!=u){
col[s[top]]=col_cnt;
ins[s[top]]=0;
col_val[col_cnt]+=val[s[top]];
if(hav[s[top]]) col_hav[col_cnt]=1;
top--;
}
top--;
}
}
int du[MAXN];
int head2[MAXN],nxt2[MAXM],vv2[MAXM],tot2;
inline void add_edge2(int u, int v){
vv2[++tot2]=v;
nxt2[tot2]=head2[u];
head2[u]=tot2;
}
void build(){
for(register int u=1;u<=n;++u)
for(register int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(col[u]==col[v]) continue;
add_edge2(col[u], col[v]);
du[col[v]]++;
}
}
queue <int> q;
long long f[MAXN];
long long ans;
bool inq[MAXN];
void spfa(){
f[col[sta]]=col_val[col[sta]];
q.push(col[sta]);
while(!q.empty()){
int u=q.front();q.pop();
inq[u]=0;
for(register int i=head2[u];i;i=nxt2[i]){
int v=vv2[i];
if(f[v]<f[u]+col_val[v]){
f[v]=f[u]+col_val[v];
if(!inq[v]) inq[v]=1,q.push(v);
}
}
}
for(register int i=1;i<=col_cnt;++i)
if(col_hav[i]) ans=MAX(ans, f[i]);
}
int main()
{
scanf("%d %d", &n, &m);
while(m--){
int a,b;scanf("%d %d", &a, &b);
add_edge(a,b);
}
for(register int i=1;i<=n;++i)
scanf("%d", &val[i]);
scanf("%d %d", &sta, &p);
while(p--){
int x;scanf("%d", &x);
hav[x]=1;
}
tarjan(sta);
build();
spfa();
printf("%lld", ans);
return 0;
}
APIO2009 抢掠计划 Tarjan DAG-DP的更多相关文章
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- [APIO2009]抢掠计划 tarjan缩点+spfa BZOJ1179
题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri 的酒吧也都设 ...
- [APIO2009]抢掠计划 ($Tarjan$,最长路)
题目链接 Solution 裸题诶... 直接 \(Tarjan\) 缩点+ \(SPFA\) 最长路即可. 不过在洛谷上莫名被卡... RE两个点... Code #include<bits/ ...
- [luogu3627 APIO2009] 抢掠计划 (tarjan缩点+spfa最长路)
传送门 Description Input 第一行包含两个整数 N.M.N 表示路口的个数,M 表示道路条数.接下来 M 行,每行两个整数,这两个整数都在 1 到 N 之间,第 i+1 行的两个整数表 ...
- P3627 [APIO2009]抢掠计划
P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...
- [APIO2009]抢掠计划(Tarjan,SPFA)
[APIO2009]抢掠计划 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是, ...
- 题解 P3627 【[APIO2009]抢掠计划】
咕了四个小时整整一晚上 P3627 [APIO2009] 抢掠计划(https://www.luogu.org/problemnew/show/P3627) 不难看出答案即为该有向图的最长链长度(允许 ...
- 【洛谷P3627】[APIO2009]抢掠计划
抢掠计划 题目链接 比较水的缩点模板题,Tarjan缩点,重新建图,记录联通块的钱数.是否有酒吧 DAG上记忆化搜索即可 #include<iostream> #include<cs ...
- Tarjan缩点+Spfa最长路【p3627】[APIO2009] 抢掠计划
Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siruseri ...
随机推荐
- SAS学习笔记42 宏程序
Autocall Macro是由SAS提供的一些实现特定功能的Macro Program,可以在代码中直接使用 其中以Q开头的相比正常的多了隐藏特殊字符的功能(称之为Macro Quoting): K ...
- java之集合那些事
集合概述: 集合和数组都可以保存多个对象,但是数组的长度不可变,集合可以保存数量变化的数据.java中的集合类主要由两个接口派生出,Collection和Map Collection接口和Iterat ...
- hdu 1203 转换的01包问题。。。。
俗话说的话 正难则反.,. 这个基本的思想都用不好的话 回家种田去吧. #include<cstdio> #include<string.h> #include<ios ...
- 基于【 centos7】一 || 安装ELK
一.安装jdk 上传安装包并解压:tar -zxvf ... 配置环境变量: 在配置文件中添加如下配置信息:vi /etc/profile export JAVA_HOME=/usr/local/jd ...
- oracle exists和 not exists 的用法
比如 a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在: select * from a where exists(select 1 from b where b ...
- MMU简介
MMU(Memory Management Unit)内存管理单元 负责虚拟地址到物理地址的映射,并提供硬件机制的内存访问权限检查.内存访问权限的检查可以保护每个进程所用的内存不会被其他进程所破坏 地 ...
- 开源证书检查工具:fossy(fossology)
工具下载: https://github.com/fossology/fossology 其他说明: http://archive15.fossology.org/projects/fossology ...
- v-cloak 的用法
https://blog.csdn.net/knqiufan/article/details/81002957
- 《构建之法》——Alpha2项目的测试
这个作业属于哪个课程 课程的链接 这个作业要求在哪里 作业要求的链接 团队名称 Runningman 这个作业的目标 测试其他组的项目,互相借鉴 作业正文 作业正文 测试人姓名 陈嘉莹 测试人学号 2 ...
- 索引 _id
_id索引是绝大多数集合默认建立的索引,对于每个插入的数据,mongodb都会自动生成一条唯一的_id字段 增加一个数据 > db.test2.insert({x:1}) WriteResult ...