bzoj1179 [Apio2009]Atm
Description
Input
第一行包含两个整数N、M。N表示路口的个数,M表示道路条数。接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号。接下来N行,每行一个整数,按顺序表示每个路口处的ATM机中的钱数。接下来一行包含两个整数S、P,S表示市中心的编号,也就是出发的路口。P表示酒吧数目。接下来的一行中有P个整数,表示P个有酒吧的路口的编号
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
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的更多相关文章
- BZOJ1179 [Apio2009]Atm 【tarjan缩点】
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MB Submit: 4048 Solved: 1762 [Submit][Sta ...
- BZOJ1179 : [Apio2009]Atm 缩点+spfa
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2069 Solved: 826[Submit][Status ...
- BZOJ1179 [Apio2009]Atm Tarjan 强连通缩点 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1179 题意概括 有一个有向图,每一个节点有一个权值,其中有一些结束点. 现在,你要从S出发,到达任 ...
- bzoj1179: [Apio2009]Atm 【缩点+spfa最长路】
题目传送门 Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruser i 银行的 ATM 取款机.令人奇怪的是,S ...
- 【强连通分量+spfa】Bzoj1179 Apio2009 Atm
Description Solution 显然缩强连通分量,然后求最长路,虽然是DAG但还是有点麻烦,于是用了spfa. Code 重建图_数组写错好多次,感觉做这题也就是练了一下实现. #inclu ...
- bzoj1179: [Apio2009]Atm scc缩点+dag上dp
先把强连通缩点,然后变成了dag,dp求终点是酒吧的最长路即可, /************************************************************** Pro ...
- 【强联通分量缩点】【最短路】【spfa】bzoj1179 [Apio2009]Atm
缩点后转化成 DAG图上的单源最长路问题.spfa/dp随便. #include<cstdio> #include<queue> #include<algorithm&g ...
- [BZOJ1179] [Apio2009]Atm(tarjan缩点 + spfa)
传送门 题意 N个点M条边的有向图 每个点有点权 从某一个结点出发 问能获得的最大点权和 一个点的点权最多被计算一次 N<=500000 M<=500000 思路 先tarjan缩点,然后 ...
- bzoj1179 [Apio2009]Atm——缩环最长路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 tarjan 缩环,然后求到有酒吧的点的最长路即可: 但一开始想缩环后用拓扑序求答案, ...
随机推荐
- shell命令xargs
今天准备找出nginx非空的日志并压缩成一个文件 find . -name "meta.access.log.*" -type f -size +0k | tar -cjv -f ...
- C#常用异常类记录
从MSDN抄下来的 ArgumentException 传递到方法的非空参数是无效的. ArgumentNullException 传递给方法的参数为空. ArgumentOutOfRangeExc ...
- 浅谈DDOS攻击
preface 做过网站运维的同事来说,绝对遇到过DDOS的攻击吧,这样的攻击实属令人头疼,那么今年就说DDOS的攻击与防护吧. 原理 DDOS(Distributed Denial Of Servi ...
- excel2013添加坐标轴名称label
图画好了,x.y轴没有名称,怎么办那 点击左上角有个---添加图标元素----里面有轴标题应该就是
- IOS VFL屏幕自适应
-(void)fun1{ //注意使用VFL,不用设置视图的frame UIView *view = [[UIView alloc] init]; view.backgroundColor = [UI ...
- 遍历jsonobject
遍历jsonobject 1 entrySet.iterator生成迭代器 2 从迭代器获取Map.Entry的单元对象 3 获取key和value Map<String,JSONObject& ...
- Linux常用服务部署与优化之NFS篇
NFS(network file system)的简称,是linux系统之间常用的一种文件共享方式,下面简述其搭建过程,需要两个linux系统的虚拟机,假设客户端的ip为192.168.1.105,服 ...
- Flask-WTF form doesn't have attribute 'validate_on_submit'问题
今天在学习WTF表单的时候遇到了这个问题,在stackoverflow上搜索查到了解决方案 from flask.ext.wtf import Form from wtforms import Tex ...
- hasClass addClass removeClass
//函数有class function hasClass(ele,cls){ return -1<(" "+ele.className+" ").inde ...
- 修改输入框placeholder文字默认颜色-webkit-input-placeholder
html5为input添加了原生的占位符属性placeholder,高级浏览器都支持这个属性,例如: <input type="text" placeholder=" ...