洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化

因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->pa2和pb-->pa2-->pa1,取个min即可

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N=200005,inf=2e9+7;
int m,n,a,b1,b2,h[N],cnt,dis[N];
bool v[N];
struct qwe
{
int ne,to,va;
}e[N<<2];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
void spfa(int s)
{
deque<int>q;
for(int i=1;i<=n;i++)
dis[i]=inf;
dis[s]=0,v[s]=1,q.push_back(s);
while(!q.empty())
{
int u=q.front();
q.pop_front();
v[u]=0;
for(int i=h[u];i;i=e[i].ne)
if(dis[e[i].to]>dis[u]+e[i].va)
{
dis[e[i].to]=dis[u]+e[i].va;
if(!v[e[i].to])
{
v[e[i].to]=1;
if(!q.empty()&&dis[q.front()]>dis[e[i].to])
q.push_front(e[i].to);
else
q.push_back(e[i].to);
}
}
}
}
int main()
{
m=read(),n=read(),a=read(),b1=read(),b2=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
}
spfa(b1);
int ans=dis[a]+dis[b2];
spfa(b2);
printf("%d\n",min(ans,dis[a]+dis[b1]));
return 0;
}

bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】的更多相关文章

  1. BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )

    跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...

  2. 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...

  3. BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa

    由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案. #include <queue> #include <cstdio> #include <cstring&g ...

  4. bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】

    因为是双向边,所以相当于两条到1的最短路和,先跑spfa然后直接处理询问即可 #include<iostream> #include<cstdio> #include<q ...

  5. 【bzoj2100】[Usaco2010 Dec]Apple Delivery 最短路

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  6. bzoj2100 [Usaco2010 Dec]Apple Delivery

    Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...

  7. bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易

    题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...

  8. bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】

    tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...

  9. bzoj 4070: [Apio2015]雅加达的摩天楼【spfa】

    明明是个最短路却有网络流一样的神建图= A = 首先要是暴力建图的话最坏有O(nm)条边.所以优化建图. 考虑分块思想,设bs=sqrt(n),对于p大于bs的,直接连边即可,最多有sqrt(n)条, ...

随机推荐

  1. STM32F407 跑马灯 寄存器版 个人笔记

    更多原理请参考跑马灯 库函数版 个人笔记 步骤 使能IO口时钟.配置相关寄存器寄存器RCC->AHB1ENR 初始化IO口模式.配置四个配置寄存器 GPIOx_MODER/ GPIOx_OTYP ...

  2. IntelliJ IDEA配置本地Tomcat方法---亲测有效

    https://blog.csdn.net/hello_ljl/article/details/79258165

  3. Linux虚拟机安装学习笔记

    一.Linux系统的安装1.VMwaer虚拟机的安装使用 官方下载软件地址:www.vmwaer.com 安装的虚拟机可以与现实的计算机进行通信 安装虚拟主机可以随意定制硬件安装配置建议: CPU:1 ...

  4. CodeForcesGym 100517B Bubble Sort

    Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  5. c/c++ 位域的概念

    位域 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又提供了一种数 ...

  6. hdu 4091 数学思维题贪心

    /* 参看博客地址:http://blog.csdn.net/oceanlight/article/details/7857713 重点是取完最优的后剩余的rest=n%lcm+lcm;中性价比小的数 ...

  7. 2018/2/16 解析Logback的AppenderBase源码,并举一反三的实现Logback扩展功能的思路,以及它的实际业务应用场景

    在学习Logback的过程中,知道了它有一个可以将日志往第三方数据源写的功能,这个功能的实现就是这个AppenderBase类,不禁想看看它的源码. 下面是AppenderBase类的所有子类(也就是 ...

  8. 【HDOJ6146】Pokémon GO(DP,计数)

    题意:一个2*n的矩阵,从任意一格出发,不重复且不遗漏地走遍所有格子,问方案数 mo 10^9+7 n<=10000 思路:因为OEIS搜出来的两个数列都是错误的,所以考虑DP 设B[i]为2* ...

  9. hdu - 1072 Nightmare(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...

  10. how to read openstack code : routes

    When coding a web system, you have to think about an important problem, how to map urls to logic. Op ...