BZOJ1179 [Apio2009]Atm 【tarjan缩点】
1179: [Apio2009]Atm
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 4048 Solved: 1762
[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的单向的道路到达其中的至少一个酒吧。
裸的 缩点 + DAG上dp
不过要注意删去到不了的点
mmp竟码了那么久= =
我还是太弱了
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 500005,maxm = 1000005,INF = 1000000000;
inline int RD(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}
return out * flag;
}
int N,M,S,P,head[maxm],nedge = 0;
int Scc[maxn],scci = 0,dfn[maxn],st[maxn],stn = 0,sta[maxn],stan = 0,cnt = 0;
int w[maxn],T[maxn],f[maxn],inde[maxn],V[maxn],ans = 0;
bool tag[maxn],reach[maxn],vis[maxn];
struct EDGE{int to,next;}edge[maxm];
inline void build(int v,int u){edge[nedge] = (EDGE){v,head[u]}; head[u] = nedge++;}
void dfs(int u){
dfn[u] = ++cnt; st[++stn] = sta[++stan] = u; int to;
Redge(u)
if (!dfn[to = edge[k].to]) dfs(to);
else if (!Scc[to]) while (dfn[st[stn]] > dfn[to]) stn--;
if (u == st[stn]){
stn--; scci++;
do {
Scc[sta[stan]] = scci;
V[scci] += w[sta[stan]];
T[scci] = T[scci] || tag[sta[stan]];
}while (sta[stan--] != u);
}
}
void tarjan(){REP(i,N) if (!dfn[i]) dfs(i);}
void solve(){
int u,v,to;
queue<int> q;
q.push(S); vis[S] = true;
while (!q.empty()){
u = q.front(); q.pop();
reach[Scc[u]] = true;
Redge(u) if (!vis[to = edge[k].to]){
vis[to] = true;
q.push(to);
}
}
REP(i,N){
u = Scc[i];
if (!reach[u]) continue;
Redge(i) if (Scc[to = edge[k].to] != u && reach[Scc[to]]){
build(Scc[to] + N,u + N); inde[Scc[to]]++;
}
}
q.push(Scc[S] + N);
while (!q.empty()){
v = (u = q.front()) - N; q.pop();
f[v] += V[v];
if (T[v]) ans = max(ans,f[v]);
Redge(u){
f[(to = edge[k].to) - N] = max(f[to - N],f[v]);
inde[to - N]--;
if (!inde[to - N]) q.push(to);
}
}
cout<<ans;
}
int main(){
memset(head,-1,sizeof(head));
N = RD(); M = RD();
while (M--) build(RD(),RD());
REP(i,N) w[i] = RD();
S = RD(); P = RD();
while (P--) tag[RD()] = true;
tarjan();
solve();
return 0;
}

浙公网安备 33010602011771号