对于一个强连通分量, 一定是整个走或者不走, 所以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. 《算法导论》读书笔记之排序算法—Merge Sort 归并排序算法

    自从打ACM以来也算是用归并排序了好久,现在就写一篇博客来介绍一下这个算法吧 :) 图片来自维基百科,显示了完整的归并排序过程.例如数组{38, 27, 43, 3, 9, 82, 10}. 在算法导 ...

  2. C# Base64解码 二进制输出

    具体的:Base64编码解码还需要学习 /// <summary> /// base64 解码Excel下载 /// </summary> /// <param name ...

  3. Netflix

    2009年Netflix举办了一场Netflix大奖赛.他们公开一批匿名数据,允许参赛团队使用以得出更好的算法.他们从获胜的团队中得到了现有算法10.06%的提升.Netflix本想再举行一场Netf ...

  4. BZOJ 1611: [Usaco2008 Feb]Meteor Shower流星雨

    1611: [Usaco2008 Feb]Meteor Shower流星雨 Description 去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个 ...

  5. BZOJ 1029: [JSOI2007]建筑抢修

    1029: [JSOI2007]建筑抢修 Description 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有 ...

  6. switch case ,while, do while,enum

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace C_编辑 ...

  7. 一个人的旅行(Dijkstra算法)

    这道题可用Dijkstra算法,好像还有floyd等算法,慢慢研究 Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途 ...

  8. ubuntu15.04更新软件源

    1,首先备份原来的源 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup2,编辑软件源的文件 sudo vim /etc/apt/so ...

  9. BZOJ 1742: [Usaco2005 nov]Grazing on the Run 边跑边吃草( dp )

    dp... dp( l , r , k )  , 表示 吃了[ l , r ] 的草 , k = 1 表示最后在 r 处 , k = 0 表示最后在 l 处 . ------------------- ...

  10. Week8(10月31日):并发

    Part I:提问  =========================== 1. 更新关联.删除关联数据,需要注意哪些问题?以Instructor类为例说明. 2. 已知某请假系统,请实现以下界面的 ...