[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=2100

[算法]

Answer = min{ dist(PB,PA1) + dist(PA1,PA2) , dist(PB,PA2) + dist(PA1,PA2) } (其中,dist表示最短路)

对PB和PA1分别求两次最短路即可,注意要使用dijkstra算法(堆优化)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXP 100010
#define MAXC 200010 struct edge
{
int to,w,nxt;
} e[MAXC << ]; int i,C,P,PA1,PA2,PB,u,v,w,tot;
int head[MAXP],dist1[MAXP],dist2[MAXP]; inline void addedge(int u,int v,int w)
{
tot++;
e[tot] = (edge){v,w,head[u]};
head[u] = tot;
}
inline void dijkstra1()
{
int i,cur,v,w;
static bool visited[MAXP];
priority_queue< pair<int,int> > q;
memset(visited,false,sizeof(visited));
memset(dist1,0x3f,sizeof(dist1));
dist1[PB] = ;
q.push(make_pair(,PB));
while (!q.empty())
{
cur = q.top().second;
q.pop();
if (visited[cur]) continue;
visited[cur] = true;
for (i = head[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (dist1[cur] + w < dist1[v])
{
dist1[v] = dist1[cur] + w;
q.push(make_pair(-dist1[v],v));
}
}
}
}
inline void dijkstra2()
{
int i,cur,v,w;
static bool visited[MAXP];
priority_queue< pair<int,int> > q;
memset(visited,false,sizeof(visited));
memset(dist2,0x3f,sizeof(dist1));
dist2[PA1] = ;
q.push(make_pair(,PA1));
while (!q.empty())
{
cur = q.top().second;
q.pop();
if (visited[cur]) continue;
visited[cur] = true;
for (i = head[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (dist2[cur] + w < dist2[v])
{
dist2[v] = dist2[cur] + w;
q.push(make_pair(-dist2[v],v));
}
}
}
}
int main()
{ scanf("%d%d%d%d%d",&C,&P,&PB,&PA1,&PA2);
for (i = ; i <= C; i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dijkstra1();
dijkstra2();
printf("%d\n",min(dist1[PA1] + dist2[PA2],dist1[PA2] + dist2[PA2])); return ; }

[BZOJ 2100] Apple Delivery的更多相关文章

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

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

  2. 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...

  3. 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery

    P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...

  4. 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...

  5. USACO Apple Delivery

    洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...

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

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

  7. bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】

    洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...

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

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

  9. bzoj2100 [Usaco2010 Dec]Apple Delivery

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

随机推荐

  1. Android 应用安装成功之后删除apk文件

    问题: 在应用开发中遇到需要这样的需求:在用户下载我们的应用安装之后删除安装包. 解决: android会在每个外界操作APK的动作之后发出系统级别的广播,过滤器名称: android.intent. ...

  2. angular js 公告墙

    <!DOCTYPE html>   <html lang="en">   <head>   <meta charset="UTF ...

  3. VMWare linux安装mysql 5.7.13

    1.基础环境说明 虚拟机:VMWare 操作系统:linux 数据库版本:mysql 5.7.13 社区版(别问为什么不装企业版,因为企业版要钱) 背景:虚拟机可以连上外网 下载目录: /tools/ ...

  4. MatLab之HDL coder

    1 Workflow The workflow for applying HDL code generation to the hardware design process requires the ...

  5. hexo搭建博客

    在使用hexo搭建个人博客的时候,修改.yml文件后出现错误:FATAL can not read a block mapping entry; a multiline key may not be ...

  6. webpack学习(五)—webpack+react+es6(第1篇)

    如果你看过webpack学习系列的前一个文章,接下来做的东西会比较简单 :webpack学习(四)— webpack-dev-server react发展的很快,现在大部分开发react相关的项目,都 ...

  7. 【转】虚拟化(二):虚拟化及vmware workstation产品使用

    vmware workstation的最新版本是10.0.2.相信大家也都使用过,其中的简单的虚拟机的创建,删除等,都很简单,这里就不再详细说明了,下面我将简单介绍下vmware workstatio ...

  8. PAT_A1015#Reversible Primes

    Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...

  9. Linux内核tracepoints

    Linux内核tracepoints 简单介绍 内核中的每个tracepoint提供一个钩子来调用probe函数. 一个tracepoint可以打开或关闭.打开时,probe函数关联到tracepoi ...

  10. 51nod1185 威佐夫游戏 V2【博弈论】

    有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...