Description

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的C(1<=C<=200000)条“牛路”都被包含在一种常用的图中,包含了P(1<=P<=100000)个牧场,分别被标为1..P。没有“牛路”会从一个牧场又走回它自己。“牛路”是双向的,每条牛路都会被标上一个距离。最重要的是,每个牧场都可以通向另一个牧场。每条牛路都连接着两个不同的牧场P1_i和P2_i(1<=P1_i,p2_i<=P),距离为D_i。所有“牛路”的距离之和不大于2000000000。

现在,贝西要从牧场PB开始给PA_1和PA_2牧场各送一个苹果(PA_1和PA_2顺序可以调换),那么最短的距离是多少呢?当然,PB、PA_1和PA_2各不相同。

Input

第一行,5个整数,\(C,P,PB,PA1,PA2\)(含义如题所述)

接下来\(C\)行,每行三个整数\(x,y,z\)描述一条无向边。

Output

一个整数,代表最小距离.

难得遇到一个这么裸的最短路题。

貌似\(Spfa\)会被\(Hack\)?

直接写\(Dijkstra\)。

分别以\(PA1\),\(PA2\)为起点跑\(Dijkstra\)。

每次对于到达其他两个点的距离取\(min\)即可。

代码

#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
#define R register using namespace std; const int gz=200008; inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
} int m,n,s,a,b,ans=214748364;
int head[gz],tot,dis[gz];
bool vis[gz]; struct hop
{
int u,d;
bool operator <(const hop&a)const
{
return d>a.d;
}
}; struct cod{int u,v,w;}edge[gz<<2]; inline void add(R int x,R int y,R int z)
{
edge[++tot].u=head[x];
edge[tot].v=y;
edge[tot].w=z;
head[x]=tot;
} inline void dijkstra(R int s)
{
for(R int i=1;i<=n;i++)dis[i]=214748364,vis[i]=false;
priority_queue<hop>q;
q.push((hop){s,dis[s]=0});
while(!q.empty())
{
R int u=q.top().u;q.pop();
if(vis[u])continue;
vis[u]=true;
for(R int i=head[u];i;i=edge[i].u)
{
if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v]=dis[u]+edge[i].w;
q.push((hop){edge[i].v,dis[edge[i].v]});
}
}
}
} int main()
{
in(m),in(n),in(s),in(a),in(b);
for(R int i=1,x,y,z;i<=m;i++)
{
in(x),in(y),in(z);
add(x,y,z);add(y,x,z);
} dijkstra(a);
ans=min(ans,dis[b]+dis[s]);
dijkstra(b);
ans=min(ans,dis[a]+dis[s]); printf("%d",ans);
}

Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery的更多相关文章

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

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

  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 题目描述 Bessie has two crisp red apples to deliver to two of he ...

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

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

  5. luoguP3003 [USACO10DEC]苹果交货Apple Delivery

    LOL新英雄卡莎点击就送 一句话题意: 三个点a1,a2,b,求从b到a1和a2的最短路 做法:求出a1->b和a2->b的最短路,两者取min,之后再加上a1->a2的最短路 为啥 ...

  6. 洛谷P3003 苹果交货Apple Delivery

    题目描述 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的\(C(1 \leq C \leq 200000)\)条"牛路"都被包含在一种常用的图中,包含了\(P(1 \ ...

  7. 【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 ...

  8. USACO Apple Delivery

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

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

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

随机推荐

  1. SRM708 div1 PalindromicSubseq(动态规划+容斥原理)

    题目大意:给定一个字符串,记X[i]为包含s[i]这个字符的所有子列是回文串的个数(注意是子列而不是子串),求出所有的X[i]*(i+1),然后异或起来作为返回结果 题解: 首先用容斥来想,如果当前枚 ...

  2. 洛谷 P1607 [USACO09FEB]庙会班车Fair Shuttle 解题报告

    P1607 [USACO09FEB]庙会班车Fair Shuttle 题目描述 Although Farmer John has no problems walking around the fair ...

  3. javascript 随机数区间

    生成[0,max]之间的随机数 parseInt(Math.random()*(max+1),10);Math.floor(Math.random()*(max+1)); 生成[1,max]之间的随机 ...

  4. Mysql History list length 值太大引起的问题

    1. 环境 Mysql 主从 Mysql版本:5.1.49-log 系统:Red Hat Enterprise Linux Server release 5.4  64bit 2. 表面现象 数据库操 ...

  5. 转:极小极大搜索方法、负值最大算法和Alpha-Beta搜索方法

    转自:极小极大搜索方法.负值最大算法和Alpha-Beta搜索方法 1. 极小极大搜索方法    一般应用在博弈搜索中,比如:围棋,五子棋,象棋等.结果有三种可能:胜利.失败和平局.暴力搜索,如果想通 ...

  6. Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合

    Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...

  7. noip车站分级 拓扑排序

    题目传送门 这道题呢 每次输入一段数就把1~n里面没有在这组数里面的数和他们连一波 表示这些数比他们等级低 然后就搞一搞就好了哇 #include<cstdio> #include< ...

  8. codeforce C. Okabe and Boxes

    题目传送门 这道题 每次删除一个点 那么这个点必然在栈里面 那么如果堆顶不是他 我们就需要一次操作使得堆合理 这时我们可以把他删除然后把他下面的点打个标记表示这下面的点以后想怎么排就怎么排以后都不需要 ...

  9. wget.vbs & wget.ps1

    Wget-like tool for file transfer when do post exploitation. CODE echo strUrl = WScript.Arguments.Ite ...

  10. Django-urls路由分发

      例如:127.0.0.0:8000/blog/lucaq.html,当有多个应用时,需要在blog应用下进行路由,我们在blog应用下做一个urls路由分发,就需要include模块实现.   导 ...