[题目链接]

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. create-react-app 中设置反向代理、项目打包资源引入路径设置及 map 文件

    1.配置反向代理 (1)porxy 配置一个代理 修改package.json文件 "proxy":"http://teng.com/website/web", ...

  2. hibernate_03_session详解

    获得session对象有两种方法: 1)openSession 2)getCurrentSession 如果使用的是getCurrentSession需要在hibernate.cfg.xml文件中进行 ...

  3. 【C++】四种排序算法的时间比较

    四种排序算法的时间比较 [注]clock函数对输入(用户输入)元素N排序的计时 #include<iostream> #include<time.h> using namesp ...

  4. 第八章 Python之常用模块

    日志模块 import logging import logging #默认级别为warning,默认打印到终端 logging.debug( logging.info( logging.warnin ...

  5. Java 8 函数接口详细教程

    ay = new byte[array.length]; for (int i = 0; i < array.length; i++) { transformedArray[i] = funct ...

  6. Markdown 常用语法总结

    注意:Markdown使用#.+.*等符号来标记,符号后面必须跟上至少跟上 1个空格才有效! Markdown的常用语法 标题 Markdown标题支持两种形式. 1.用#标记 在标题开头加上1~6个 ...

  7. eas之获得任何一个KDTable的选中行

    import com.kingdee.bos.ctrl.kdf.table.util.KDTableUtil; int[] selectRows =KDTableUtil.getSelectedRow ...

  8. 【学习笔记】关于最大公约数(gcd)的定理

    手动博客搬家: 本文发表于20181004 00:21:28, 原地址https://blog.csdn.net/suncongbo/article/details/82935140 结论1 \[\g ...

  9. 洛谷 P1494 BZOJ 2038 [2009国家集训队]小Z的袜子(hose)

    //洛谷题面字体.排版我向来喜欢,却还没收录这道如此有名的题,BZOJ的题面字体太那啥啦,清橙的题面有了缩进,小标题却和正文字体一致,找个好看的题面咋这么难呐………… //2019年3月23日23:0 ...

  10. 数据库工具——Navicat Premium使用技巧

    Navicat Premium 常用功能讲解 1.快捷键 1.1. F8 快速回到当前对象列表  1.2. Ctrl + q 打开查询界面  1.3. Ctrl + d 快速修改当前的表结构  1.4 ...