bzoj1179: [Apio2009]Atm
2017-04-05 13:07
399 查看
链接
http://www.lydsy.com/JudgeOnline/problem.php?id=1179题解
这道题目并没有看上去的那么好做啊。首先肯定tarjan缩点,所完之后是一个DAG。
然后题目问题以某个点为开头的最长链,这个东西貌似不好dp吧….
所以只能上spfa。不太好调,细节挺多的。
代码
//tarjan+dp #include <cstdio> #include <algorithm> #include <queue> #define maxn 1000010 using namespace std; int N, M, head[maxn], to[maxn], nex[maxn], scc[maxn], stack[maxn], s[maxn], w[maxn], dfn[maxn], vis[maxn], low[maxn], u[maxn], v[maxn], rd[maxn], f[maxn], pub[maxn], S, tot, in[maxn]; queue<int> q; inline void adde(int a, int b){to[++tot]=b;nex[tot]=head[a];head[a]=tot;} inline int read(int x=0) { char c=getchar(); while(c<48 or c>57)c=getchar(); while(c>=48 and c<=57)x=(x<<1)+(x<<3)+c-48,c=getchar(); return x; } void tarjan(int pos) { int p; dfn[pos]=low[pos]=++dfn[0]; stack[++stack[0]]=pos; vis[pos]=1; for(p=head[pos];p;p=nex[p]) { if(vis[to[p]]==0)tarjan(to[p]); if(vis[to[p]]==1)low[pos]=min(low[pos],low[to[p]]); } if(dfn[pos]==low[pos]) { for(++scc[0];stack[stack[0]+1]!=pos;stack[0]--) scc[stack[stack[0]]]=scc[0],vis[stack[stack[0]]]=2; } } void dp() { int i, x, p, m=-1; f[scc[S]]=w[scc[S]]; q.push(scc[S]);in[scc[S]]=1; while(!q.empty()) { in[x=q.front()]=0;q.pop(); for(p=head[x];p;p=nex[p]) { if(f[x]+w[to[p]]>f[to[p]]) { f[to[p]]=f[x]+w[to[p]]; if(!in[to[p]])q.push(to[p]),in[to[p]]=1; } } } for(i=1;i<=N;i++)if(pub[i])m=max(m,f[scc[i]]); printf("%d",m); } void init() { int i, P, x; scanf("%d%d",&N,&M); for(i=1;i<=M;i++)u[i]=read(), v[i]=read(), adde(u[i],v[i]); for(i=1;i<=N;i++)w[i]=read(); S=read(), P=read(); for(i=1;i<=P;i++)x=read(), pub[x]=1; scc[0]=N; for(i=1;i<=N;i++)if(!vis[i])tarjan(i); for(i=1;i<=M;i++)if(scc[u[i]]^scc[v[i]])adde(scc[u[i]],scc[v[i]]),rd[scc[v[i]]]++; for(i=1;i<=N;i++)w[scc[i]]+=w[i]; } int main() { init(); dp(); return 0; }
相关文章推荐
- BZOJ 1179 [Apio2009]Atm(强连通分量)
- 【BZOJ1179】【Apio2009】Atm 强连通分量缩点+拓扑DP/拓扑最长路 kosaraju+tarjan+dfs转非递归三种代码
- [BZOJ1179] [Apio2009]Atm(强连通)
- [bzoj1179][Apio2009]Atm Tarjan+spfa
- 【bzoj1179】【apio2009】Atm【强连通分量缩点+spfa】
- 【Apio2009】Bzoj1179 Atm
- 【BZOJ 1179】[Apio2009]Atm
- BZOJ 1179: [Apio2009]Atm
- 【bzoj 1179】[Apio2009]Atm(Tarjan+spfa)
- bzoj1179 [Apio2009]Atm
- bzoj 1179: [Apio2009]Atm tarjan
- bzoj 1179: [Apio2009]Atm【tarjan+spfa】
- [BZOJ 1179][Apio2009]Atm:Tarjan+SPFA
- 【强联通分量缩点】【最短路】【spfa】bzoj1179 [Apio2009]Atm
- BZOJ 1179: [Apio2009]Atm( tarjan + 最短路 )
- BZOJ 1179 APIO2009 ATM Tarjan+堆优化SPFA
- bzoj1179 [Apio2009]Atm
- BZOJ 1179 APIO 2009 Atm Tarjan+SPFA
- BZOJ1179 [Apio2009]Atm
- bzoj 1179: [Apio2009]Atm(Trajan+SPFA)