p3627&bzoj1179 抢掠计划(ATM)
题目

Input
Output
输出一个整数,表示Banditji从市中心开始到某个酒吧结束所能抢劫的最多的现金总数。
Sample Input
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
Sample Output
分析
Tarjan缩点的好题,先用Tarjan缩点,求出每个环的总钱数和这个环中是否有酒吧,然后重新建图跑spfa求最大点权和,但由于要在酒吧结束,所以最终答案只能在有酒吧的环中取。
插曲
作为一个最短路只会写folyd和dijstar的蒟蒻,我自己yy了一个spfa,然而它居然对了(QAQ)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int bar[510000],mey[510000],belong[510000],cnt,tmy[510000],tbr[510000];
int dfn[510000],low[510000],vis[510000],sum;
int d[510000],ist[510000],maxn,iqu[510000];
vector<int>v[500010];
vector<int>nv[500010];
stack<int>a;
queue<pair<int,int> >q;
int n,m,s,p;
inline void read(int &x){
int f=1;x=0;
char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+(s-'0');s=getchar();}
x*=f;
}
inline void tarjan(int x){
low[x]=dfn[x]=++cnt;
a.push(x);
ist[x]=1;
int i,j,k;
for(i=0;i<v[x].size();i++)
if(!dfn[v[x][i]]){
tarjan(v[x][i]);
low[x]=min(low[x],low[v[x][i]]);
}else if(ist[v[x][i]]){
low[x]=min(low[x],dfn[v[x][i]]);
}
if(dfn[x]==low[x]){
sum++;
int ok=0;
while(1){
int u=a.top();
ist[u]=0;
if(bar[u])ok=1;
belong[u]=sum;
tmy[sum]+=mey[u];
a.pop();
if(u==x)break;
}
if(ok)tbr[sum]=1;
}
return;
}
int main()
{ int i,j,k,x,y;
read(n),read(m);
for(i=1;i<=m;i++){
read(x),read(y);
v[x].push_back(y);
}
for(i=1;i<=n;i++){
read(mey[i]);
}
scanf("%d%d",&s,&p);
for(i=1;i<=p;i++){
read(x);
bar[x]=1;
}
for(i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(i=1;i<=n;i++)
for(j=0;j<v[i].size();j++)
if(belong[i]!=belong[v[i][j]]){
nv[belong[i]].push_back(belong[v[i][j]]);
}
d[belong[s]]=tmy[belong[s]];
q.push(make_pair(belong[s],tmy[belong[s]]));
iqu[belong[s]]=1;
while(!q.empty()){
int u=q.front().first;
q.pop();
iqu[u]=0;
if(tbr[u])maxn=max(maxn,d[u]);
for(i=0;i<nv[u].size();i++){
d[nv[u][i]]=max(d[nv[u][i]],d[u]+tmy[nv[u][i]]);
if(!iqu[nv[u][i]]){
q.push(make_pair(nv[u][i],d[nv[u][i]]));
iqu[nv[u][i]]=1;
}
}
}
cout<<maxn<<endl;
return 0;
}
p3627&bzoj1179 抢掠计划(ATM)的更多相关文章
- P3627 [APIO2009]抢掠计划
P3627 [APIO2009]抢掠计划 Tarjan缩点+最短(最长)路 显然的缩点...... 在缩点时,顺便维护每个强连通分量的总权值 缩完点按照惯例建个新图 然后跑一遍spfa最长路,枚举每个 ...
- 洛谷 P3627 【抢掠计划】
题库:洛谷 题号:3627 题目:抢掠计划 link:https://www.luogu.org/problem/P3627 思路 : 这道题是一道Tarjan + 最长路的题.首先,我们用Tarja ...
- 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路
题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...
- 【luogu P3627 [APIO2009]抢掠计划】 题解
题目链接:https://www.luogu.org/problemnew/show/P3627 把点权转化到边权上去. #include <stack> #include <que ...
- 【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
洛谷P3627:https://www.luogu.org/problemnew/show/P3627 思路 由于有强连通分量 所以我们可以想到先把整个图缩点 缩点完之后再建一次图 把点权改为边权 并 ...
- 洛谷 P3627 [APIO2009]抢掠计划
这题一看就是缩点,但是缩完点怎么办呢?首先我们把所有的包含酒吧的缩点找出来,打上标记,然后建立一张新图, 每个缩点上的点权就是他所包含的所有点的点权和.但是建图的时候要注意,每一对缩点之间可能有多条边 ...
- [洛谷P3627][APIO2009]抢掠计划
题目大意:给你一张$n(n\leqslant5\times10^5)$个点$m(m\leqslant5\times10^5)$条边的有向图,有点权,给你起点和一些可能的终点.问从起点开始,到任意一个终 ...
- BZOJ 1179 抢掠计划atm (缩点+有向无环图DP)
手动博客搬家: 本文发表于20170716 10:58:18, 原地址https://blog.csdn.net/suncongbo/article/details/81061601 https:// ...
- 洛谷 P3627 [APIO2009]抢掠计划 题解
Analysis 建图+强连通分量+SPFA求最长路 但要保证最后到达的点中包含酒馆 虽然思路并不难想,但要求的代码能力很高. #include<iostream> #include< ...
随机推荐
- [原创]java WEB学习笔记06:ServletContext接口
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 【leetcode刷题笔记】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- ubuntu commands mysql
use table; //选择某个表 show tables; //展现有几个表 select * from table; //显示表内容
- Spark- 求最受欢迎的TopN课程
数据库操作工具类 package com.rz.mobile_tag.utils import java.sql.{Connection, DriverManager, PreparedStateme ...
- spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持
spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...
- 分享知识-快乐自己:Shrio 案例Demo概述
Shiro 权限认证核心: POM:文件: <!--shiro-all--> <dependency> <groupId>org.apache.shiro</ ...
- 实现html表头固定,表格内的信息向上滚动
效果如下: <!doctype html>Table header header two fuck 1 fuck 2 fuck 1 fuck 2 fuck 1 fuck 2 fuck 1 ...
- MySQL--开发技巧(一)
Inner Join: Left Outer Join: Right Outer Join: Full Join: Cross Join: SELECT t1.attrs ,t2.attrs FROM ...
- Convolutional Neural Networks for Visual Recognition 5
Setting up the data and the model 前面我们介绍了一个神经元的模型,通过一个激励函数将高维的输入域权值的点积转化为一个单一的输出,而神经网络就是将神经元排列到每一层,形 ...
- H264 NALU 使用PS封装 RTP发送
最近由于项目平台需求,要将H264 NALU封装为PS再用RTP发送,PS封装按照ISO DEC-13818-1标准.一个PS包包含PS Header, PES Header, PS system h ...