对于一个强连通分量, 一定是整个走或者不走, 所以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. 简单的webservice

    Hi,大家好! 今天主要和大家分享,如何搭建一个Web服务,做Android开发,不可避免会涉及到客户端开发,我们怎么样来实现一个服务端,怎么样来实现一个客户端,并相互传递数据.就算调用别人的服务时, ...

  2. Elasticsearch 1.4.X 的CORS设置

    最近将Elasticsearch由1.2.2升级到1.4.4后,sense工具无法使用了,它发到Elasticsearch集群的请求没有任何的响应.阅读了Elasticsearch的文档才了解到,这是 ...

  3. LinkList Operation

    链表典型数据结构: #define ElemType int typedef struct LinkNode{ ElemType value; struct LinkNode* next; }; 相比 ...

  4. C语言之三大查找算法

    查找算法 1.二分查找 二分查找就是折半查找,其基本思想是:首先选取表中间位置的记录,将其关键字与给定关键字key进行比较,若相等,则查找成功.若key值比该关键字值大,则要找的元素一定在右子表中,则 ...

  5. hdu 4730 We Love MOE Girls

    http://acm.hdu.edu.cn/showproblem.php?pid=4730 直接用string类处理字符串. AC代码: #include<iostream> #incl ...

  6. Linux多线程编程(一)---多线程基本编程

    线程概念 线程是指运行中的程序的调度单位.一个线程指的是进程中一个单一顺序的控制流,也被称为轻量级线程.它是系统独立调度和分配的基本单位.同一进程中的多个线程将共享该系统中的全部系统资源,比如文件描述 ...

  7. HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)

    Attacking rooks Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB Total submit u ...

  8. COM组件

    COM组件   COM component(COM组件)是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术.在COM构架下,人们可以开发出各种各样的功能专一的组件,然后 ...

  9. python下的web服务器模块

    python下的web服务模块有三种: BaseHTTPServer: 提供基本的Web服务和处理器类,分别是HTTPServer和BaseHTTPRequestHandler SimpleHTTPS ...

  10. ThinkPHP - 关联模型 - 多对多

    表结构: 映射关系: UserRelationModel会取UserRelation为表名称.所以要自定义表名称: //定义主表名称protected $tableName = 'User'; < ...