BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa
由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案.
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100006
#define M 200007
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
deque<int>q;
int n,m,A,B,C,edges;
int hd[N],to[M<<1],nex[M<<1],val[M<<1],d[2][M],inq[2][M];
void addedge(int u,int v,int c)
{
nex[++edges]=hd[u],hd[u]=edges,to[edges]=v,val[edges]=c;
}
void spfa(int s,int t)
{
// printf("%d\n",s);
memset(d[t],0x3f,sizeof(d[t]));
memset(inq[t],0,sizeof(inq[t]));
d[t][s]=0, inq[t][s]=1, q.push_back(s);
for(;!q.empty();)
{
int u=q.front(); q.pop_front();
inq[t][u]=0;
for(int i=hd[u];i;i=nex[i])
{
int v=to[i];
if(d[t][u] + val[i] < d[t][v])
{
d[t][v] = d[t][u] + val[i];
if(!inq[t][v])
{
if(q.empty()||d[t][v] < d[t][q.front()]) q.push_front(v);
else q.push_back(v);
inq[t][v]=1;
}
}
}
}
}
int main()
{
int i,j;
// setIO("input");
scanf("%d%d%d%d%d",&m,&n,&A,&B,&C);
for(i=1;i<=m;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c), addedge(a,b,c), addedge(b,a,c);
}
spfa(B, 0);
// printf("%d %d\n",d[0][A],d[0][C]);
spfa(C, 1);
printf("%d\n",min(d[0][A] + d[0][C], d[1][A] + d[1][B]));
return 0;
}
BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa的更多相关文章
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...
- bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】
洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...
- 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)
http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...
- bzoj2100 [Usaco2010 Dec]Apple Delivery
Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...
- 【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 ...
- bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
题目描述 一张P个点的无向图,C条正权路.CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌.(途中不必回家)可以先去NOI,也可以先去CMO.当然神犇CLJ肯 ...
- BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱( dp )
dp( l , r ) = sum( l , r ) - min( dp( l + 1 , r ) , dp( l , r - 1 ) ) 被卡空间....我们可以发现 l > r 是无意义的 ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...
- BZOJ 2101 [Usaco2010 Dec]Treasure Chest 藏宝箱:区间dp 博弈【两种表示方法】【压维】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2101 题意: 共有n枚金币,第i枚金币的价值是w[i]. 把金币排成一条直线,Bessie ...
随机推荐
- Linux系列(17)之系统服务
我们知道,在我们登陆Linux后,系统就为我们提供了很多服务,比如例行工作调度服务crond.打印服务.邮件服务等.那么这些服务是如何被启动的呢? 这个问题先放一下,接下来我们先了解一下Linux的启 ...
- Prometheus Operator 的安装
Prometheus Operator 的安装 接下来我们用自定义的方式来对 Kubernetes 集群进行监控,但是还是有一些缺陷,比如 Prometheus.AlertManager 这些组件服务 ...
- linux实现一个定时任务
设置定时任务删除logs脚本数据 编写脚本 touch cleanLogs.sh #! /bin/sh -name "*.log*" -exec rm -f {} \; 使用r ...
- WindowsAPI使用详解——GetVersion|GetVersionEx 获取操作系统版本和名称
Windows API 中有两个函数可以得到系统版本信息:GetVersion和GetVersionEx. GetVersion这个函数曾经困扰了很多程序员,其本来设计的是在DWORD返 ...
- 【原创】大叔经验分享(81)marathon上app无法重启
通过api调用marathon重启app后出现deployment,但是app不会重启,配置如下: "constraints": [ [ "hostname", ...
- 【Android】二、HelloWorld
1. 按照该网址写HelloWorld 例子 http://www.runoob.com/android/android-hello-world-example.html 2.点击 make pr ...
- html-proload
对基础与初始化进行预加载 加载顺序 document > preload > 正常加载 media 属性 不同设备时的响应式加载 - media="(max-width: 600 ...
- ES6入门十:iterator迭代器
迭代模式 ES6迭代器标准化接口 迭代循环 自定义迭代器 迭代器消耗 一.迭代模式 迭代模式中,通常有一个包含某种数据集合的对象.该数据可能存在一个复杂数据结构内部,而要提供一种简单的方法能够访问数据 ...
- 前端必学TypeScript之第一弹,st基础类型!
TypeScript 是微软开发的 JavaScript 的超集,TypeScript兼容JavaScript,可以载入JavaScript代码然后运行.TypeScript与JavaScript相比 ...
- ASE2019 model组 事后诸葛亮会议记录
诸葛亮文档 设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 传统编程教育模式下,初学者(主要是刚刚接触编程的学生)往往依靠老师与助教的课堂教学,由 ...