题目描述

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的\(C(1 \leq C \leq 200000)\)条“牛路”都被包含在一种常用的图中,包含了\(P(1 \leq P \leq 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\)各不相同。

输入输出格式

输入格式:

  • Line \(1\): Line \(1\) contains five space-separated integers: \(C, P, PB, PA_1\), and \(PA_2\)

  • Lines \(2..C+1\): Line \(i+1\) describes cowpath i by naming two pastures it connects and the distance between them: \(P1_i, P2_i, D_i\)

输出格式:

  • Line 1: The shortest distance Bessie must travel to deliver both apples

输入输出样例

输入样例#1:

9 7 5 1 4
5 1 7
6 7 2
4 7 2
5 6 1
5 2 4
4 3 2
1 2 3
3 2 2
2 6 3

输出样例#1:

12

思路:一道裸的最短路题目,跟其他最短路题目不一样的是,这道题目要求的是起点到两个点的最短路之和,且这两个点可以调换,那么我们可以考虑以这两个点分别为起点,求到\(PB\)的最短路和到另一个点的最短路,然后取最小值。跑最短路用堆优化\(dijkstra\)即可。

代码:

#include<cstdio>
#include<algorithm>
#include<cctype>
#include<queue>
#include<cstring>
#define maxn 100007
using namespace std;
int n,m,num,head[maxn],s,a,b,dis[maxn];
inline int qread() {
char c=getchar();int num=0,f=1;
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) num=num*10+c-'0';
return num*f;
}
struct Edge {
int v,w,nxt;
}e[maxn<<2];
struct node {
int x,y;
bool operator < (const node &a) const{return y>a.y;}
};
inline void ct(int u, int v, int w) {
e[++num].v=v;
e[num].w=w;
e[num].nxt=head[u];
head[u]=num;
}
priority_queue<node>q;
inline void dijkstra(int s) {
memset(dis,0x3f,sizeof(dis));
q.push((node){s,0}),dis[s]=0;
while(!q.empty()) {
int u=q.top().x,d=q.top().y;
q.pop();
if(d!=dis[u]) continue;
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(dis[v]>dis[u]+e[i].w) {
dis[v]=dis[u]+e[i].w;
q.push((node){v,dis[v]});
}
}
}
}
int main() {
m=qread(),n=qread(),s=qread(),a=qread(),b=qread();
for(int i=1,u,v,w;i<=m;++i) {
u=qread(),v=qread(),w=qread();
ct(u,v,w);ct(v,u,w);
}
dijkstra(a);
int zrj=dis[s]+dis[b];
dijkstra(b);
int cyh=dis[s]+dis[a];
printf("%d\n",min(cyh,zrj));
return 0;
}

洛谷P3003 苹果交货Apple Delivery的更多相关文章

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

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

  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

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

  5. Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery

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

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

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

  7. 洛谷——P1505 苹果摘陶陶

    题目背景 根据2005年的Noip普及组第一题衍生出的一题. 但是有一点点的恶搞成分在里面..... 题目描述 话说去年苹果们被陶陶摘下来后都很生气,于是就用最先进的克隆技术把陶陶克隆了好多份> ...

  8. USACO Apple Delivery

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

  9. [洛谷P4491] [HAOI2018]染色

    洛谷题目链接:[HAOI2018]染色 题目背景 HAOI2018 Round2 第二题 题目描述 为了报答小 C 的苹果, 小 G 打算送给热爱美术的小 C 一块画布, 这块画布可 以抽象为一个长度 ...

随机推荐

  1. java jdk1.7后 sting intern()方法与之前的区别

    public static void main(String[] args) { String s1 = new StringBuilder("计算机").append(" ...

  2. codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)

    题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...

  3. STL stl_alloc.h

    # // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: http://blog.csdn.net/mdl13412 # # // ...

  4. POJ1041 John's trip

    John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  5. Poj 1458 Common Subsequence(LCS)

    一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...

  6. POJ2513(字典树+图的连通性判断)

    //用map映射TLE,字典树就AC了#include"cstdio" #include"set" using namespace std; ; ;//26个小 ...

  7. web攻击之三:SQL注入攻击的种类和防范手段

    观察近来的一些安全事件及其后果,安全专家们已经得到一个结论,这些威胁主要是通过SQL注入造成的.虽然前面有许多文章讨论了SQL注入,但今天所讨论的内容也许可帮助你检查自己的服务器,并采取相应防范措施. ...

  8. Spring boot 学习二:入门

    1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...

  9. 关于导入excel报错的处理(xls,xlsx)

    关于导入excel报错的处理(xls,xlsx) 最近在做一个将excel导入到dataGriview中的小功能在做的过程中遇到以下问题: 链接excel的链接串是这样写的 string strCon ...

  10. Linq to Object之非延迟标准查询操作符

    非延时标准查询操作符是指不具备延时查询特性的标准查询操作符,这些操作符一般用于辅助延时标准查询操作符使用. 1.ToArray操作符 ToArray操作符用于将一个输入序列转换成一个数组. 方法原型: ...