[题目链接]

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. java线程中断

    public void Thread.interrupt() // 无返回值 public boolean Thread.isInterrupted() // 有返回值 public static b ...

  2. Java实现九宫格

    import java.util.Scanner; public class Sudoku { public static void main(String[] args) { System.out. ...

  3. Spring学习笔记之aop动态代理(3)

    Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...

  4. 三星A3、A5、A7、G7、J5、J7、S6系列等新机型的部分手机解锁 ROOT刷机

    三星A3.A5.A7.G7.J5.J7.S6系列等新机型的部分手机,三星官方加了限制,需要解锁后才能刷机如果没有解锁,刷第三方recovery或者刷非官方原版固件,都会刷不进,手机跳转到提示界面,显示 ...

  5. js-事件处理(重点)

    1:各种常用事件: 2:简单用法: <body onLoad="javascript:alert('hello world');" onUnload="javasc ...

  6. Java内存机制,内存地址

    问题一:String str1 = "abc"; String str2 = "abc"; System.out.println(str1==str2); // ...

  7. 【PostgreSQL-9.6.3】函数(2)--字符型函数

    在上一篇博文中我们交流了数值型函数,这篇我们将讨论PostgreSQL中的字符型函数. 1. reverse(string) reverse函数可以将string字符串的字母显示顺序颠倒. test= ...

  8. halcon 模板匹配 -- 转化 vector_angle_to_rigid

    ********************************模板匹配 ********************create_shape_model创建模板,这个函数有许多参数,其中金字塔的级数由N ...

  9. Memcached 之分布式算法原理

    memcached并不像mongodb一样可以配置多个节点,并且节点之间可以”自动分配数据“,即相互通信,所以我们在做memcache分布式集群的时候要有一个算法来保证当一台memcache服务器宕机 ...

  10. c# md5加密封装

    /// <summary> /// md5加密字符串 /// </summary> /// <param name="str">需要加密的字符串 ...