bzoj 1179 [Apio2009]Atm 缩点+最短路
[Apio2009]Atm
Time Limit: 15 Sec Memory Limit: 162 MB
Submit: 4290 Solved: 1893
[Submit][Status][Discuss]
Description

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
HINT
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue> #define N 500007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,S,p;
int dis[N],w[N],val[N];
int cnt,hed[N],rea[N<<],nxt[N<<];
int cnt1,hed1[N],rea1[N<<],nxt1[N<<];
int tim,tot,scc,bel[N],stk[N],dfn[N],low[N];
bool flag[N],instk[N],flag1[N]; void add(int u,int v)
{
nxt[++cnt]=hed[u];
hed[u]=cnt;
rea[cnt]=v;
}
void add1(int u,int v)
{
nxt1[++cnt1]=hed1[u];
hed1[u]=cnt1;
rea1[cnt1]=v;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++tim;
stk[++tot]=u,instk[u]=true;
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i];
if (!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (instk[v]) low[u]=min(low[u],dfn[v]);
}
if (low[u]==dfn[u])
{
int x=-;scc++;
while (x!=u)
{
x=stk[tot--];
bel[x]=scc;
instk[x]=false;
}
}
}
void Spfa(int st)
{
queue<int>q;q.push(bel[st]);bool boo[N];
memset(boo,,sizeof(boo));boo[bel[st]]=true;
for (int i=;i<=scc;i++) dis[i]=;
dis[bel[st]]=val[bel[st]];
while (!q.empty())
{
int u=q.front();q.pop();
for (int i=hed1[u];i!=-;i=nxt1[i])
{
int v=rea1[i];
if (dis[u]+val[v]>dis[v])
{
dis[v]=dis[u]+val[v];
if (!boo[v]) q.push(v),boo[v]=true;
}
}
boo[u]=false;
}
}
int main()
{
freopen("fzy.in","r",stdin);
freopen("fzy.out","w",stdout); memset(hed,-,sizeof(hed));
memset(hed1,-,sizeof(hed1));
n=read(),m=read();
for (int i=;i<=m;i++)
{
int x=read(),y=read();
add(x,y);
}
for (int i=;i<=n;i++) w[i]=read();
S=read(),p=read();
for (int i=;i<=p;i++)
flag[read()]=true;
for (int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
for (int u=;u<=n;u++)
{
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i];
if (bel[u]!=bel[v]) add1(bel[u],bel[v]);
}
flag1[bel[u]]|=flag[u];
val[bel[u]]+=w[u];
}
Spfa(S);
int ans=;
for (int i=;i<=scc;i++)
ans=max(ans,dis[i]*flag1[i]);
printf("%d\n",ans);
}
bzoj 1179 [Apio2009]Atm 缩点+最短路的更多相关文章
- BZOJ 1179: [Apio2009]Atm( tarjan + 最短路 )
对于一个强连通分量, 一定是整个走或者不走, 所以tarjan缩点然后跑dijkstra. ------------------------------------------------------ ...
- bzoj 1179 [Apio2009]Atm——SCC缩点+spfa
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...
- bzoj 1179: [Apio2009]Atm
Description Input 第 一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路 的起点和终点的 ...
- bzoj 1179[Apio2009]Atm (tarjan+spfa)
题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- BZOJ 1179 [Apio2009]Atm(强连通分量)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1179 [题目大意] 给出一张有向带环点权图,给出一些终点,在路径中同一个点的点权只能累 ...
- bzoj 1179: [Apio2009]Atm【tarjan+spfa】
明明优化了spfa还是好慢-- 因为只能取一次值,所以先tarjan缩点,把一个scc的点权和加起来作为新点的点权,然后建立新图.在新图上跑spfa最长路,最后把酒吧点的dis取个max就是答案. # ...
- 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
[bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...
- 1179: [Apio2009]Atm
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 1629 Solved: 615[Submit][Status ...
- BZOJ1179 : [Apio2009]Atm 缩点+spfa
1179: [Apio2009]Atm Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2069 Solved: 826[Submit][Status ...
随机推荐
- 复合词 (Compund Word,UVa 10391)
题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...
- IDEA搭载Tomcat使用JSTL连接Oracle数据库
1.在IDEA中,JSTL库添加到WEB-INF/lib下面可以直接在JSP页面上通过 <%@ taglib uri="http://java.sun.com/jsp/jstl/cor ...
- Deep Residual Learning for Image Recognition论文笔记
Abstract We present a residual learning framework to ease the training of networks that are substant ...
- 特殊符号 & 以太坊
&表示取二进制的末尾 &1表示如果末尾是奇数和偶数两种情况 0 偶数 1奇数 举例子: int a=1;int p=&a; 其中,p是指针,&a就是将a在内存中的实际地 ...
- mouseover 和 mouseout 事件是可以冒泡的 取消
mouseover 和 mouseout 事件是可以冒泡的,子元素上触发的事件会冒泡到父元素上.可以改用 mouseleave 和 mouseenter 事件,这两个事件不冒泡.
- VUE中关于表单提交的简单实现
main.js import Vue from "../vue.js"; import App from "./App.js"; //启动 new Vue({ ...
- Python的top-level脚本为什么在磁盘上没有对应的字节码?
在Python中,如果你使用python script.py这样的方式运行Python脚本,那么script.py就被称为top-level脚本.对于Python来说,这个脚本的字节码是不会写入到磁盘 ...
- python切片详解
先从原理上分析切片运算: list的切片,内部是调用__getitem__,__setitem__,__delitem__和slice函数.而slice函数又是和range()函数相关的. 给切片传递 ...
- Java容器之Iterator接口
Iterator 接口: 1. 所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象. 2. Iterator 对象称作迭代器,用以方便的 ...
- pythoh使用 xpath去除空格空格
html_str = """ <!DOCTYPE html> <html lang="en"> <head> &l ...