1179: [Apio2009]Atm
Time Limit: 15 Sec Memory Limit: 162 MB[ 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
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
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 500000 + 5;
int last[N],last2[N],cnt,c[N],dfn[N],low[N],belong[N],scnt,id,q[N],inq[N],top,ans,v[N],S,P,f[N];
struct Edge{
int to,next;
}e[N*2],ee[N*2];
int n,m;
void insert( int u, int v ){ e[++cnt].to = v; e[cnt].next = last[u]; last[u] = cnt; }
void insert2( int u, int v ){ ee[++cnt].to = v; ee[cnt].next = last2[u]; last2[u] = cnt;}
void tarjan( int x ){
int now = 0;
dfn[x] = low[x] = ++id;
q[++top] = x; inq[x] = 1;
for( int i = last[x]; i; i = e[i].next )
if( !dfn[e[i].to] ){
tarjan(e[i].to); low[x] = min(low[x],low[e[i].to]);
} else if( inq[e[i].to] ) low[x] = min(low[x],dfn[e[i].to]);
if( low[x] == dfn[x] ){
scnt++;
while( now != x ){
now = q[top--]; belong[now] = scnt;
v[scnt] += c[now]; inq[now] = 0;
}
}
}
void rebuild(){
cnt = 0;
for( int i = 1; i <= n; i++ )
for( int j = last[i]; j; j = e[j].next )
if( belong[i] != belong[e[j].to] )
insert2(belong[i],belong[e[j].to]);
}/**/
void spfa(){
int tail = 1, head = 0;
q[0] = belong[S]; inq[belong[S]] = 1;
f[belong[S]] = v[belong[S]];
while( tail != head ){
int now = q[head++]; if( head == 500000 ) head = 0;
for( int i = last2[now]; i; i = ee[i].next )
if( f[now] + v[ee[i].to] > f[ee[i].to] ){
f[ee[i].to] = f[now] + v[ee[i].to];
if( !inq[ee[i].to] ){
inq[ee[i].to] = 1; q[tail++] = ee[i].to; if( tail == 500000 ) tail = 0;
}
}
inq[now] = 0;
}
}
int main(){
scanf("%d%d", &n, &m);
for( int i = 1, u,v; i <= m; i++ ){
scanf("%d%d", &u, &v);
insert( u, v );
}
for( int i = 1; i <= n; i++ ) scanf("%d", &c[i]);
for( int i = 1; i <= n; i++ ) if( !dfn[i] ) tarjan(i);
scanf("%d", &S); scanf("%d", &P);
rebuild(); spfa();
for( int i = 1,x; i <= P; i++ ){
scanf("%d", &x);
if( f[belong[x]] > ans ) ans = f[belong[x]];
}
printf("%d", ans);
return 0;
}