【BZOJ】1179: [Apio2009]Atm(tarjan+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1179
缩点建图。。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=1000005, oo=~0u>>1; int FF[N], LL[N], scc, p[N], vis[N], s[N], top, tot, d[N];
struct GR {
int ihead[N], cnt, n, w[N];
struct dat { int to, next; }e[N];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
}
void tarjan(int x) {
FF[x]=LL[x]=++tot; vis[x]=1; s[++top]=x;
int y;
rdm(x, i) {
y=e[i].to;
if(!FF[y]) tarjan(y), LL[x]=min(LL[x], LL[y]);
else if(vis[y]) LL[x]=min(LL[x], FF[y]);
}
if(LL[x]==FF[x]) {
++scc;
do {
y=s[top--];
vis[y]=0;
p[y]=scc;
} while(x!=y);
}
}
void tarjan() { for1(i, 1, n) if(!FF[i]) tarjan(i); }
void spfa(int x) {
for1(i, 1, n) d[i]=-oo, vis[i]=0;
d[x]=w[x]; vis[x]=1;
int front, tail; front=tail=0;
int *q=s;
q[tail++]=x;
while(front!=tail) {
int u=q[front++], v; if(front==N) front=0; vis[u]=0;
rdm(u, i) if(d[e[i].to]<d[u]+w[e[i].to]) {
v=e[i].to;
d[v]=d[u]+w[v];
if(!vis[v]) {
vis[v]=1;
if(d[q[front]]<d[v]) {
--front; if(front<0) front+=N;
q[front]=v;
}
else {
q[tail++]=v; if(tail==N) tail=0;
}
}
}
}
}
}G, g; void build() {
G.tarjan();
g.n=scc;
for1(i, 1, G.n) g.w[p[i]]+=G.w[i];
for1(x, 1, G.n) for(int i=G.ihead[x]; i; i=G.e[i].next) if(p[x]!=p[G.e[i].to]) g.add(p[x], p[G.e[i].to]);
} int n, m, ans, num, bar[N];
int main() {
read(n); read(m); G.n=n;
for1(i, 1, m) { int u=getint(), v=getint(); G.add(u, v); }
for1(i, 1, n) read(G.w[i]);
int s=getint(); read(num);
build();
g.spfa(p[s]);
for1(i, 1, num) read(bar[i]);
for1(i, 1, num) ans=max(ans, d[p[bar[i]]]);
printf("%d\n", ans);
return 0;
}
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的单向的道路到达其中的至少一个酒吧。
Source
【BZOJ】1179: [Apio2009]Atm(tarjan+spfa)的更多相关文章
- 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
[bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...
- BZOJ 1179 [Apio2009]Atm(强连通分量)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1179 [题目大意] 给出一张有向带环点权图,给出一些终点,在路径中同一个点的点权只能累 ...
- 【BZOJ】1027: [JSOI2007]合金(凸包+floyd)
http://www.lydsy.com/JudgeOnline/problem.php?id=1027 题意:$n$种材料,$m$种需求.每种材料有三个属性,给出三个属性的含量(和为1),问能否通过 ...
- 【BZOJ】1101: [POI2007]Zap(莫比乌斯+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=1101 无限膜拜数论和分块orz 首先莫比乌斯函数的一些性质可以看<初等数论>或<具 ...
- 【BZOJ】3527: [Zjoi2014]力(fft+卷积)
http://www.lydsy.com/JudgeOnline/problem.php?id=3527 好好的一道模板题,我自己被自己坑了好久.. 首先题目看错.......什么玩意.......首 ...
- 【BZOJ】1082: [SCOI2005]栅栏(二分+dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1082 题意:n个给出木板,m个给出木板.可以将那m个木板锯成泥想要的长度.问最大能锯成多少个给出的n ...
- 【BZOJ】1066: [SCOI2007]蜥蜴(最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1066 本题想一想应该懂了的. 我们想啊,,每个点都有限制,每个点都可以跳到另一个有限制的点,每个有蜥 ...
- 【BZOJ】3172: [Tjoi2013]单词(后缀自动机)
http://www.lydsy.com/JudgeOnline/problem.php?id=3172 随便搞个sam就行了.(其实一开始看到数据n<=200, 单词长度不超过1e6,然后感觉 ...
- 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...
随机推荐
- yum 配置
1.配置yum本地源 # mount /dev/cdrom /mnt/ # vim /etc/yum.repos.d/rhel-source.repo 1 [rhel-source] 2 name=R ...
- 把sql server 2000的用户表的所有者改成dbo
怎么样把sql server 2000的用户表的所有者,改成dbo,而不是用户名. 推荐使用下面介绍的第二种方法,执行以下查询便可以了.sp_configure 'allow updates','1' ...
- css+div绝对定位
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- bootstrap的select2校验及不影响原来的格式
<style> .has-error .select2-choice, .has-error .select2-choices, .has-error.simple .select2-ch ...
- Effecvtive C++笔记:让自己习惯C++
条款01:视C++为一个语言联绑 C++的四个语言层次: C:C++是以C为基础的.基本数据类型.语句.预处理器.数组.指针等统统来自C. Oject-Oriented C++:面向对象这一特性包含了 ...
- jquery validate自定义checkbox验证规则和样式
参考:http://blog.csdn.net/xh16319/article/details/9987847 自定义checkbox验证,“检查checkbox是否选中” jQuery.valida ...
- 转MYSQL学习(二) 运算符
MYSQL中的运算符很多,这一节主要讲MYSQL中有的,而SQLSERVER没有的运算符 安全等于运算符(<=>) 这个操作符和=操作符执行相同的比较操作,不过<=>可以用来判 ...
- 在eclipse中配置一个简单的spring入门项目
spring是一个很优秀的基于Java的轻量级开源框架,为了解决企业级应用的复杂性而创建的,spring不仅可用于服务器端开发,从简单性.可测试性和松耦合性的角度,任何java应用程序都可以利用这个思 ...
- window.open()读取本地图片简单使用总结
最近做了一个项目,需要读取本地图片出来,问了一些人,感觉在数据库中存取路径比较合适,故做此方法. 后台查询出来的路径
- javascript的onbeforeunload函数在IOS上运行
今天在做项目的时候,组长让我用iPad测试一下前面写的离线缓存,后退不刷新页面,发现在iPad上onbeforeunload函数在iPad上一带而过,不运行??? 无奈之下,发现原来在IOS上,有自己 ...