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的单向的道路到达其中的至少一个酒吧。

  1. /*
  2. 非递归tarjan要爆栈,实用性不是很强,需要用网上的这个非递归版tarjan
  3. */
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<string>
  7. #include<cstring>
  8. #include<algorithm>
  9. #include<stack>
  10. #include<queue>
  11. #include<vector>
  12. using namespace std;
  13. const int maxn = ;
  14. struct edge{
  15. int v;
  16. int nxt;
  17. }e[maxn];
  18. int n,m,s,p,mny[maxn],nmny[maxn];
  19. int head[maxn],cnt;
  20. int stop,sta[maxn],dfn[maxn],low[maxn],isin[maxn],indx;
  21. int tot;
  22. int d[maxn],vis[maxn];
  23. bool inst[maxn];
  24. stack<int> st;
  25. vector<int> g[maxn];
  26. int read(){
  27. char ch=getchar();
  28. int x=,f=;
  29. while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
  30. while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
  31. return x*f;
  32. }
  33. void ins(int u,int v){
  34. cnt++;
  35. e[cnt].v = v;
  36. e[cnt].nxt = head[u];
  37. head[u] = cnt;
  38. }
  39. void input(){
  40. n = read();
  41. m = read();
  42. int u,v;
  43. for(int i = ;i <= m;i++){
  44. u = read();
  45. v = read();
  46. ins(u,v);
  47. }
  48. for(int i = ;i <= n;i++) mny[i] = read();
  49. s = read();
  50. p = read();
  51. }
  52. void st_psh(int x){
  53. dfn[x] = low[x] = ++indx;
  54. inst[x] = true;
  55. sta[++stop] = x;
  56. st.push(x);
  57. }
  58. void tarjan(int x){
  59. int t;
  60. st_psh(x);
  61. while(!st.empty()){
  62. t = st.top();
  63. for(int i = head[t];i;i = e[i].nxt){
  64. if(dfn[e[i].v] == ){
  65. st_psh(e[i].v);
  66. break;
  67. }
  68. }
  69. if(t == st.top()){
  70. for(int i = head[t];i;i = e[i].nxt){
  71. if(dfn[e[i].v] > dfn[t]) low[t] = min(low[e[i].v],low[t]);
  72. else if(inst[e[i].v]){low[t] = min(dfn[e[i].v],low[t]);}
  73. }
  74. if(dfn[t] == low[t]){
  75. ++tot;
  76. int j;
  77. do{
  78. j = sta[stop--];
  79. inst[j] = false;
  80. isin[j] = tot;
  81. nmny[tot] += mny[j];
  82. }while(j != t);
  83. }
  84. st.pop();
  85. }
  86. }
  87. }
  88. void spfa(){
  89. int u,to;
  90. queue<int> q;
  91. u = isin[s];
  92. d[isin[s]] = nmny[isin[s]];
  93. vis[isin[s]] = true;
  94. q.push(isin[s]);
  95. while(!q.empty()){
  96. u = q.front();
  97. q.pop();
  98. for(int i = ;i < g[u].size();i++){
  99. to = g[u][i];
  100. if(d[to] < d[u] + nmny[to]){
  101. d[to] = d[u] + nmny[to];
  102. if(!vis[to]){
  103. vis[to] = true;
  104. q.push(to);
  105. }
  106. }
  107. }
  108. vis[u] = false;
  109. }
  110. }
  111. void work(){
  112. for(int i = ;i <= n;i++){
  113. if(!dfn[i]) tarjan(i);
  114. }
  115. for(int i = ;i <= n;i++){
  116. for(int j = head[i];j;j = e[j].nxt){
  117. if(isin[i] != isin[e[j].v]){
  118. g[isin[i]].push_back(isin[e[j].v]);
  119. //cout<<isin[i]<<" "<<isin[e[j].v]<<endl;
  120. }
  121. }
  122. }
  123. spfa();
  124. int qs,ans = ;
  125. while(p--){
  126. qs = read();
  127. ans = max(d[isin[qs]],ans);
  128. }
  129. cout<<ans;
  130. }
  131. int main(){
  132. input();
  133. work();
  134. return ;
  135. }

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. shell命令xargs

    今天准备找出nginx非空的日志并压缩成一个文件 find . -name "meta.access.log.*" -type f -size +0k | tar -cjv -f ...

  2. C#常用异常类记录

    从MSDN抄下来的 ArgumentException 传递到方法的非空参数是无效的. ArgumentNullException  传递给方法的参数为空. ArgumentOutOfRangeExc ...

  3. 浅谈DDOS攻击

    preface 做过网站运维的同事来说,绝对遇到过DDOS的攻击吧,这样的攻击实属令人头疼,那么今年就说DDOS的攻击与防护吧. 原理 DDOS(Distributed Denial Of Servi ...

  4. excel2013添加坐标轴名称label

    图画好了,x.y轴没有名称,怎么办那 点击左上角有个---添加图标元素----里面有轴标题应该就是

  5. IOS VFL屏幕自适应

    -(void)fun1{ //注意使用VFL,不用设置视图的frame UIView *view = [[UIView alloc] init]; view.backgroundColor = [UI ...

  6. 遍历jsonobject

    遍历jsonobject 1 entrySet.iterator生成迭代器 2 从迭代器获取Map.Entry的单元对象 3 获取key和value Map<String,JSONObject& ...

  7. Linux常用服务部署与优化之NFS篇

    NFS(network file system)的简称,是linux系统之间常用的一种文件共享方式,下面简述其搭建过程,需要两个linux系统的虚拟机,假设客户端的ip为192.168.1.105,服 ...

  8. Flask-WTF form doesn't have attribute 'validate_on_submit'问题

    今天在学习WTF表单的时候遇到了这个问题,在stackoverflow上搜索查到了解决方案 from flask.ext.wtf import Form from wtforms import Tex ...

  9. hasClass addClass removeClass

    //函数有class function hasClass(ele,cls){ return -1<(" "+ele.className+" ").inde ...

  10. 修改输入框placeholder文字默认颜色-webkit-input-placeholder

    html5为input添加了原生的占位符属性placeholder,高级浏览器都支持这个属性,例如: <input type="text" placeholder=" ...