Description

Input

第一行包含两个整数N、M。N表示路口的个数,M表示道路条数。接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号。接下来N行,每行一个整数,按顺序表示每个路口处的ATM机中的钱数。接下来一行包含两个整数S、P,S表示市中心的编号,也就是出发的路口。P表示酒吧数目。接下来的一行中有P个整数,表示P个有酒吧的路口的编号

Output

输出一个整数,表示Banditji从市中心开始到某个酒吧结束所能抢劫的最多的现金总数。

Sample Input

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

Sample Output

47

HINT

50%的输入保证N, M<=3000。所有的输入保证N, M<=500000。每个ATM机中可取的钱数为一个非负整数且不超过4000。输入数据保证你可以从市中心沿着Siruseri的单向的道路到达其中的至少一个酒吧。

/*
非递归tarjan要爆栈,实用性不是很强,需要用网上的这个非递归版tarjan
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
const int maxn = ;
struct edge{
int v;
int nxt;
}e[maxn];
int n,m,s,p,mny[maxn],nmny[maxn];
int head[maxn],cnt;
int stop,sta[maxn],dfn[maxn],low[maxn],isin[maxn],indx;
int tot;
int d[maxn],vis[maxn];
bool inst[maxn];
stack<int> st;
vector<int> g[maxn];
int read(){
char ch=getchar();
int x=,f=;
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
return x*f;
}
void ins(int u,int v){
cnt++;
e[cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void input(){
n = read();
m = read();
int u,v;
for(int i = ;i <= m;i++){
u = read();
v = read();
ins(u,v);
}
for(int i = ;i <= n;i++) mny[i] = read();
s = read();
p = read();
}
void st_psh(int x){
dfn[x] = low[x] = ++indx;
inst[x] = true;
sta[++stop] = x;
st.push(x);
}
void tarjan(int x){
int t;
st_psh(x);
while(!st.empty()){
t = st.top();
for(int i = head[t];i;i = e[i].nxt){
if(dfn[e[i].v] == ){
st_psh(e[i].v);
break;
}
}
if(t == st.top()){
for(int i = head[t];i;i = e[i].nxt){
if(dfn[e[i].v] > dfn[t]) low[t] = min(low[e[i].v],low[t]);
else if(inst[e[i].v]){low[t] = min(dfn[e[i].v],low[t]);}
}
if(dfn[t] == low[t]){
++tot;
int j;
do{
j = sta[stop--];
inst[j] = false;
isin[j] = tot;
nmny[tot] += mny[j];
}while(j != t);
}
st.pop();
}
}
}
void spfa(){
int u,to;
queue<int> q;
u = isin[s];
d[isin[s]] = nmny[isin[s]];
vis[isin[s]] = true;
q.push(isin[s]);
while(!q.empty()){
u = q.front();
q.pop();
for(int i = ;i < g[u].size();i++){
to = g[u][i];
if(d[to] < d[u] + nmny[to]){
d[to] = d[u] + nmny[to];
if(!vis[to]){
vis[to] = true;
q.push(to);
}
}
}
vis[u] = false;
}
}
void work(){
for(int i = ;i <= n;i++){
if(!dfn[i]) tarjan(i);
}
for(int i = ;i <= n;i++){
for(int j = head[i];j;j = e[j].nxt){
if(isin[i] != isin[e[j].v]){
g[isin[i]].push_back(isin[e[j].v]);
//cout<<isin[i]<<" "<<isin[e[j].v]<<endl;
}
}
}
spfa();
int qs,ans = ;
while(p--){
qs = read();
ans = max(d[isin[qs]],ans);
}
cout<<ans;
}
int main(){
input();
work();
return ;
}

bzoj1179 [Apio2009]Atm的更多相关文章

  1. BZOJ1179 [Apio2009]Atm 【tarjan缩点】

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MB Submit: 4048  Solved: 1762 [Submit][Sta ...

  2. BZOJ1179 : [Apio2009]Atm 缩点+spfa

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 2069  Solved: 826[Submit][Status ...

  3. BZOJ1179 [Apio2009]Atm Tarjan 强连通缩点 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1179 题意概括 有一个有向图,每一个节点有一个权值,其中有一些结束点. 现在,你要从S出发,到达任 ...

  4. bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】

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

  5. 【强连通分量+spfa】Bzoj1179 Apio2009 Atm

    Description Solution 显然缩强连通分量,然后求最长路,虽然是DAG但还是有点麻烦,于是用了spfa. Code 重建图_数组写错好多次,感觉做这题也就是练了一下实现. #inclu ...

  6. bzoj1179: [Apio2009]Atm scc缩点+dag上dp

    先把强连通缩点,然后变成了dag,dp求终点是酒吧的最长路即可, /************************************************************** Pro ...

  7. 【强联通分量缩点】【最短路】【spfa】bzoj1179 [Apio2009]Atm

    缩点后转化成 DAG图上的单源最长路问题.spfa/dp随便. #include<cstdio> #include<queue> #include<algorithm&g ...

  8. [BZOJ1179] [Apio2009]Atm(tarjan缩点 + spfa)

    传送门 题意 N个点M条边的有向图 每个点有点权 从某一个结点出发 问能获得的最大点权和 一个点的点权最多被计算一次 N<=500000 M<=500000 思路 先tarjan缩点,然后 ...

  9. bzoj1179 [Apio2009]Atm——缩环最长路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 tarjan 缩环,然后求到有酒吧的点的最长路即可: 但一开始想缩环后用拓扑序求答案, ...

随机推荐

  1. GPU keylogger && GPU Based rootkit(Jellyfish rootkit)

    catalog . OpenCL . Linux DMA(Direct Memory Access) . GPU rootkit PoC by Team Jellyfish . GPU keylogg ...

  2. RBM Formula Deduction

    Energy based Model the probability distribution (softmax function): \[p(x)=\frac{\exp(-E(x))}{\sum\l ...

  3. tomcat密码的坑

    <role rolename="tomcat"/> <role rolename="role1"/> <user username ...

  4. django rest framework

    Django-Rest-Framework 教程: 4. 验证和权限 作者: Desmond Chen, 发布日期: 2014-06-01, 修改日期: 2014-06-02 到目前为止, 我们的AP ...

  5. CSS3-border-radius的兼容写法大全

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  6. CIQRCodeGenerator Core Image Filter Reference

    https://developer.apple.com/library/prerelease/content/documentation/GraphicsImaging/Reference/CoreI ...

  7. 9.13 JS循环

    循环:循环操作某一个功能(执行某段代码) 四要素: 循环初始值 循环条件 状态改变 循环体       for  穷举     迭代            i++;等价于i=i+1;  ++I;等价于 ...

  8. win7下如何建立ftp服务器

    前段时间正在做一个项目,需要上传东西到ftp服务器,纠结于如何建立ftp服务器.经过一番摸索.终于成功建立ftp服务器.现将我的经验跟大家分享一下.不足之处还望多多指点! 步骤/方法 首先在本地机器上 ...

  9. 关键词提取1-C#

    C# 中文分词算法(实现从文章中提取关键字算法) using System;using System.IO;using System.Text;using System.Collections;usi ...

  10. URL组分

    url通常包含多个组成部分,在js中可通过location对象获取其中各项信息 访问http://mp.weixin.qq.com/s?__biz=MjM5NjA0NjgyMA==&mid=2 ...