bzoj 1179 tarjan+spfa
首先我们可以将这个图缩成DAG,那么问题中的路线就可以简化为DAG中的一条链,那么我们直接做一遍spfa就好了。
反思:开始写的bfs,结果bfs的时候没有更新最大值,而是直接赋的值,后来发现不能写bfs,因为每个点可能进队好多次,当让可以改成循环队列什么的bfs,然后我就改成了spfa,伪的spfa,就是判一下这个点是不是更优了,更优才入队。
/**************************************************************
Problem: 1179
User: BLADEVIL
Language: C++
Result: Accepted
Time:6640 ms
Memory:109356 kb
****************************************************************/
//By BLADEVIL
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxm 2000010
#define maxn 2000010
using namespace std;
int n,m,s,tot,time,num,p,l;
int pre[maxm],other[maxm],last[maxn],key[maxn],flag[maxn];
int stack[maxn],dfn[maxn],low[maxn],vis[maxn],col[maxn],que[maxn],w[maxn];
void connect(int x,int y) {
pre[++l]=last[x];
last[x]=l;
other[l]=y;
}
void dfs(int x) {
stack[++tot]=x; vis[x]=;
dfn[x]=low[x]=++time;
for (int p=last[x];p;p=pre[p]) {
if (!dfn[other[p]])
dfs(other[p]),low[x]=min(low[x],low[other[p]]); else
if (vis[other[p]]) low[x]=min(low[x],dfn[other[p]]);
}
if (dfn[x]==low[x]) {
int cur=-;
num++;
while (cur!=x) {
cur=stack[tot--];
vis[cur]=;
col[cur]=num;
}
}
}
int main() {
scanf("%d%d",&n,&m); num=n;
for (int i=;i<=m;i++) {
int x,y; scanf("%d%d",&x,&y);
connect(x,y);
}
for (int i=;i<=n;i++) scanf("%d",&key[i]);
scanf("%d%d",&s,&p);
while (p--) {
int x; scanf("%d",&x);
flag[x]=;
}
dfs(s);
for (int i=;i<=n;i++) {
for (int p=last[i];p;p=pre[p]) if (col[i]!=col[other[p]]) connect(col[i],col[other[p]]);
}
for (int i=;i<=n;i++) key[col[i]]+=key[i],flag[col[i]]|=flag[i];
memset(vis,,sizeof vis);
que[]=col[s]; vis[col[s]]=; w[col[s]]=key[col[s]];
int h=,t=,ans=;
while (h<t) {
int cur=que[++h];
if (flag[cur]) ans=max(ans,w[cur]);
for (int p=last[cur];p;p=pre[p]) {
if (w[cur]+key[other[p]]>w[other[p]]) {
w[other[p]]=max(w[other[p]],w[cur]+key[other[p]]);
que[++t]=other[p]; vis[other[p]]=;
}
}
}
printf("%d\n",ans);
return ;
}
bzoj 1179 tarjan+spfa的更多相关文章
- BZOJ 1179 (Tarjan缩点+DP)
题面 传送门 分析 由于一个点可以经过多次,显然每个环都会被走一遍. 考虑缩点,将每个强连通分量缩成一个点,点权为联通分量上的所有点之和 缩点后的图是一个有向无环图(DAG) 可拓扑排序,按照拓扑序进 ...
- bzoj 1179[Apio2009]Atm (tarjan+spfa)
题目 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- BZOJ 1179 Atm 题解
BZOJ 1179 Atm 题解 SPFA Algorithm Tarjan Algorithm Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来 ...
- 【BZOJ1179】[Apio2009]Atm (tarjan+SPFA)
显而易见的tarjan+spfa...不解释了 ; type edgetype=record toward,next:longint; end; var edge1,edge2:..maxn] of ...
- bzoj 1179 [APIO 2009]Atm(APIO水题) - Tarjan - spfa
Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每行一 ...
- 【BZOJ】1179: [Apio2009]Atm(tarjan+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1179 缩点建图... #include <cstdio> #include <cs ...
- bzoj 1179: [Apio2009]Atm【tarjan+spfa】
明明优化了spfa还是好慢-- 因为只能取一次值,所以先tarjan缩点,把一个scc的点权和加起来作为新点的点权,然后建立新图.在新图上跑spfa最长路,最后把酒吧点的dis取个max就是答案. # ...
- bzoj 1179 [Apio2009]Atm——SCC缩点+spfa
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...
- BZOJ 1179: [Apio2009]Atm( tarjan + 最短路 )
对于一个强连通分量, 一定是整个走或者不走, 所以tarjan缩点然后跑dijkstra. ------------------------------------------------------ ...
随机推荐
- C# WebBrowser控件详解
作者:827969653 0.常用方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表 ...
- Thinkphp5的ajax接口实现
前一篇讲到thinkphp5从数据库获取数据之后赋给视图view,前一篇从数据渲染方式来说是服务端数据渲染,这一章则是浏览器端数据渲染.按照知识总结依据来划分,这是两种不同的技术场景. 下面介绍具体的 ...
- asp.net MVC4 @Html.DropDownList的使用
在MVC4中使用Razor语法,一使用就爱上他了, 一般项目都是有一些增删改查功能,表单下拉框是经常使用的,除了用原始的<select>外,还可以用@Html.DropDownList和@ ...
- BZOJ 1211 树的计数(purfer序列)
首先考虑无解的情况, 根据purfer序列,当dee[i]=0并且n!=1的时候,必然无解.否则为1. 且sum(dee[i]-1)!=n-2也必然无解. 剩下的使用排列组合即可推出公式.需要注意的是 ...
- Django+haystack实现全文搜索出现错误 ImportError: cannot import name signals
原因是在你的settings.py或者其他地方使用了 "import haystack" 当我们使用django-haysatck库时,表面上会有haystack库,但实际上并不 ...
- Andorid API Package ---> android.app
包名: android.app Added in API level 1 URL:http://developer. ...
- 转:使用 python Matplotlib 库 绘图 及 相关问题
使用 python Matplotlib 库绘图 转:http://blog.csdn.net/daniel_ustc/article/details/9714163 Matplotlib ...
- Android 通知机制 Toast和Notification
Android常用的反馈系统状态信息的方式主要有三种 Toast提醒 通知栏提醒 对话框提醒 三种提醒分别适用于页面的提示.系统交互事件的通知和非常重要的提醒: 一.Toast Toast toast ...
- BZOJ5248:[九省联考2018]一双木棋——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5248 https://www.luogu.org/problemnew/show/P4363#su ...
- redux的bindActionCreators
bindActionCreators是redux的一个API,作用是将单个或多个ActionCreator转化为dispatch(action)的函数集合形式. 开发者不用再手动dispatch(ac ...