对于一个强连通分量, 一定是整个走或者不走, 所以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. shell实例浅谈之六文件特定行打印的多种方法

    一.问题 Sed和AWK在处理文件方面有很强的优势,还有head和tail等文件处理工具的使用,grep也可实现文本的搜索.上述命令都可以在后面直接加文件名,不需要在前面使用cat添加管道,cat会影 ...

  2. Hadoop平台安装前准备

    集群配置 准备工作 1.  Iptables #chkconfig iptables –list #chkconfig iptables –level 3456off #service iptable ...

  3. 通过加索引对sql语句优化

    今天看数据库的时候遇到这样一个SQL语句: select substr(a.djxh,) as id, (a.nd || a.yf) DECL_YM, a.zspm_dm as LEVY_ITEM_I ...

  4. 基于Bresenham算法画圆

    bresenham算法画圆思想与上篇 bresenham算法画线段 思想是一致的 画圆x^2+y^2=R^2 将他分为8个部分,如上图 1. 只要画出1中1/8圆的圆周,剩下的就可以通过对称关系画出这 ...

  5. Cocos2d-X学习——Android不同设备FPS不同问题

    2014-07-16 环境:Cocos2dx 2.2.4 AppDelegate.cpp中FPS设置为 60 pDirector->setAnimationInterval(1.0 / 60); ...

  6. 使用client对象模型回写SharePoint列表

    使用client对象模型回写SharePoint列表 client对象模型是一个有效的方式回写SharePoint列表. 1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保 ...

  7. 【JavaScript】history.back() 网页已过期

    使用history.back()进行返回时,有时会提示“网页已过期”, 多数是因为目标页面的form为post提交方式,而且是表单已经提交后的响应页面,无法找到form中的具体参数,而“报错”. 具体 ...

  8. 树莓派安装ftp服务器

    在树莓派安装ftp服务器,可上载\下载文件 vsftpd是开源的轻量级的常用ftp服务器. 1,安装vsftpd服务器 (约400KB)sudo apt-get install vsftpd 2,启动 ...

  9. docker学习笔记17:Dockerfile 指令 ONBUILD介绍

    ONBUILD指令可以为镜像添加触发器.其参数是任意一个Dockerfile 指令. 当我们在一个Dockerfile文件中加上ONBUILD指令,该指令对利用该Dockerfile构建镜像(比如为A ...

  10. kingso_module - Taocode

    kingso_module - Taocode 模块介绍 Merger 功能介绍 Merger的功能: 合并多台Searcher机器的部分查询结果,得到最终的完整查询结果 向Detail集群请求最终展 ...