对于一个强连通分量, 一定是整个走或者不走, 所以tarjan缩点然后跑dijkstra.

---------------------------------------------------------------------

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; ++i)
#define clr(x, c) memset(x, c, sizeof(x))
#define foreach(i, x) for(__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
using namespace std;
const int maxn = 500009;
struct edge {
int to;
edge* next;
} E[maxn], *pt = E, *head[maxn];
inline void addedge(int u, int v) {
pt->to = v, pt->next = head[u];
head[u] = pt++;
}
vector<int> G[maxn];
stack<int> S;
int dfn[maxn], low[maxn], scc[maxn], CK = 0, N = 0;
bool _bar[maxn], bar[maxn];
int _w[maxn], w[maxn], n, s, d[maxn];
void tarjan(int x) {
dfn[x] = low[x] = ++CK;
S.push(x);
foreach(it, G[x]) 
   if(!dfn[*it]) {
    tarjan(*it);
    low[x] = min(low[x], low[*it]);
   } else if(!~scc[*it]) 
       low[x] = min(low[x], dfn[*it]);
if(low[x] == dfn[x]) {
N++;
for(int t = S.top(); ; t = S.top()) {
S.pop();
scc[t] = N;
w[N] += _w[t];
bar[N] |= _bar[t];
if(t == x) break;
}
}
}
void TARJAN() {
clr(w, 0), clr(dfn, 0), clr(low, 0), clr(scc, -1);
rep(i, n) if(!dfn[i]) tarjan(i);
}
void build() {
rep(i, n) 
   foreach(it, G[i]) if(scc[*it] != scc[i])
       addedge(scc[i], scc[*it]);
s = scc[s];
}
struct node {
int x, d;
bool operator < (const node &o) const {
return d > o.d;
}
};
void dijkstra() {
rep(i, N) d[i] = 0;
priority_queue<node> Q;
d[s] = w[s];
Q.push( (node) {s, w[s]} );
while(!Q.empty()) {
node h = Q.top(); Q.pop();
int x = h.x, dist = h.d;
if(dist != d[x]) continue;
for(edge* e = head[x]; e; e = e->next) if(d[e->to] < d[x] + w[e->to]) {
d[e->to] = d[x] + w[e->to];
Q.push( (node) {e->to, d[e->to]} );
}
}
int ans = 0;
rep(i, N) if(bar[i])
   ans = max(ans, d[i]);
printf("%d\n", ans);
}
void Read() {
int m;
cin >> n >> m;
while(m--) {
int u, v;
scanf("%d%d", &u, &v); u--; v--;
G[u].push_back(v);
}
rep(i, n) scanf("%d", _w + i);
scanf("%d%d", &s, &m); s--;
clr(_bar, 0);
while(m--) {
int t;
scanf("%d", &t); t--;
_bar[t] = true;
}
}
int main() {
freopen("test.in", "r", stdin);
Read();
TARJAN();
build();
dijkstra();
return 0;
}

---------------------------------------------------------------------

1179: [Apio2009]Atm

Time Limit: 15 Sec  Memory Limit: 162 MB
Submit: 1919  Solved: 762
[Submit][Status][Discuss]

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

Source

BZOJ 1179: [Apio2009]Atm( tarjan + 最短路 )的更多相关文章

  1. bzoj 1179[Apio2009]Atm (tarjan+spfa)

    题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...

  2. bzoj 1179 [Apio2009]Atm 缩点+最短路

    [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 4290  Solved: 1893[Submit][Status][Dis ...

  3. bzoj 1179: [Apio2009]Atm【tarjan+spfa】

    明明优化了spfa还是好慢-- 因为只能取一次值,所以先tarjan缩点,把一个scc的点权和加起来作为新点的点权,然后建立新图.在新图上跑spfa最长路,最后把酒吧点的dis取个max就是答案. # ...

  4. bzoj 1179: [Apio2009]Atm

    Description Input 第 一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路 的起点和终点的 ...

  5. bzoj 1179 [Apio2009]Atm——SCC缩点+spfa

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...

  6. BZOJ 1179 [Apio2009]Atm(强连通分量)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1179 [题目大意] 给出一张有向带环点权图,给出一些终点,在路径中同一个点的点权只能累 ...

  7. 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm

    [bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...

  8. 1179: [Apio2009]Atm

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

  9. 【BZOJ】1179: [Apio2009]Atm(tarjan+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1179 缩点建图... #include <cstdio> #include <cs ...

随机推荐

  1. Django forms 关于select和checkbox设置初始选中值及让前端选中指定值

    Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...

  2. java读写

    IO流下分为字节流与字符流,每个流又分为输入输出以及读写. 字节流的两个基类为InputStream与OutputStream. 字符流为Reader和Writer

  3. window下nodejs安装指南

    相信对于很多关注javascript发展的同学来说,nodejs已经不是一个陌生的词眼.有关nodejs的相关资料网上已经铺天盖地.由于它的高并发特性,造就了其特殊的应用地位. 国内目前关注最高,维护 ...

  4. 转:触屏中的js事件

    一.触摸事件 ontouchstartontouchmoveontouchendontouchcancel 目前移动端浏览器均支持这4个触摸事件,包括IE.由于触屏也支持MouseEvent,因此他们 ...

  5. 蝕刻技術(Etching Technology)

    1. 前言 蚀刻是将材料使用化学反应或物理撞击作用而移除的技术. 蚀刻技术可以分为『湿蚀刻』(wet etching)及『干蚀刻』(dry etching)两类.在湿蚀刻中是使用化学溶液,经由化学反应 ...

  6. vs使代码可以折叠的方法

    set [工具]->[选项]->[文本编辑器]->[C/C++]->[查看]->[大纲显示]->[大纲语句块] = True

  7. 数据结构大型实验的记录(done)

    用平衡二叉树的知识实现用户登录系统模拟 基本思路: 类:AVLnode (树的节点类) AVLtree (树的基本操作类 包括insert remove search 平衡树的4种旋转) UserIn ...

  8. Going Home(最大匹配km算法)

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20115   Accepted: 10189 Desc ...

  9. 2014 北京邀请赛ABDHJ题解

    A. A Matrix 点击打开链接 构造,结论是从第一行開始往下产生一条曲线,使得这条区间最长且从上到下递减, #include <cstdio> #include <cstrin ...

  10. java多线程监听JMS、MQ队列

    本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可. 转载请注明出处和作者.http://blog.csdn.net/xtj332 背景:消息队列中有非常多的消息需要 ...