对于一个强连通分量, 一定是整个走或者不走, 所以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. http://riddle.arthurluk.net walkthrough

    MSVFMyU4MCU4MWh0dHAlM0ElMkYlMkZyaWRkbGUuYXJ0aHVybHVrLm5ldCUyRnN0YWdlb25lLnBocCUwRCUwQTIlRTMlODAlODFo ...

  2. java读写

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

  3. querySelector和querySelectorAll方法介绍

    module dom { [Supplemental, NoInterfaceObject] interface NodeSelector { Element querySelector(in DOM ...

  4. linux下检测端口是否连通

    检测tcp端口使用telnet命令 telnet 例:telnet 192.168.0.1 80 检测udp端口使用uc命令 uc -zu 例:uc -zu 192.169.0.1 80   以上命令 ...

  5. Java学习之IO字节流

    字节流分为FileInputStream 和FileOutputStream package com.io; import java.io.File; import java.io.FileInput ...

  6. 小程序员在android移动应用上的赚钱经历

    先说说我自己吧,二线城市(以外包为主)的小程序员,工作多年了,月收入5-6K.主要从事asp.net web网站开发,java,c++,php,ruby都懂一些,属于那种对问题不求甚解型,爱好电脑游戏 ...

  7. caffe神经网络框架的辅助工具(将图片转换为leveldb格式)

    caffe中负责整个网络输入的datalayer是从leveldb里读取数据的,是一个google实现的很高效的kv数据库. 因此我们训练网络必须先把数据转成leveldb的格式. 这里我实现的是把一 ...

  8. HashMap,LinkedHashMap,TreeMap的区别(转)

    Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...

  9. Identifiers

    Identifier An identifier is an arbitrarily long sequence of digits, underscores, lowercase and upper ...

  10. Cesium 获取当前视图范围

    Cesium作为一个开源的WebGlobe解决方案已经很牛了,不过因为开发的资料不多,很多功能不知道怎么实现.下面记录下自己获取Cesium当前场景范围的方法(2维中对应的是extent). exte ...