P3003 [USACO10DEC]苹果交货Apple Delivery

题目描述

Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000)

cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000.

What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course.

Consider this map of bracketed pasture numbers and cowpaths with distances:

               3        2       2
[1]-----[2]------[3]-----[4]
\ / \ /
7\ /4 \3 /2
\ / \ /
[5]-----[6]------[7]
1 2

If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is:

5 -> 6-> 7 -> 4 -> 3 -> 2 -> 1

with a total distance of 12.

贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的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各不相同。

输入输出格式

输入格式:

  • Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2

  • 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 
/*
基本上是个最短路模板题吧。。
就是求出t1和t2间的最短路a1
再求min(dis[s][t1],dis[s][t2])
两者相加即可
本来以为这题没这么简单,随便一写结果A了
我用的堆优化Dij
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 100010
int n,m,s,t1,t2,dis[maxn],num,head[maxn];
bool vis[maxn];
struct node{
int id,d;
bool operator < (const node b)const{
return d>b.d;
}
};
struct Node{
int to,pre,v;
}e[*];
void Insert(int from,int to,int v){
e[++num].to=to;
e[num].v=v;
e[num].pre=head[from];
head[from]=num;
}
void Dij(int st,int tt1,int tt2){
int flag=;
priority_queue<node>q;
memset(dis,/,sizeof(dis));
memset(vis,,sizeof(vis));
dis[st]=;node now;
now.id=st;now.d=;
q.push(now);
while(!q.empty()){
now=q.top();q.pop();
int u=now.id;
if(vis[u])continue;
vis[u]=;
if(u==tt1)flag++;
if(u==tt2)flag++;
if(flag==)return;
for(int i=head[u];i;i=e[i].pre){
int to=e[i].to;
if(dis[u]+e[i].v<dis[to]){
dis[to]=dis[u]+e[i].v;
node nxt;nxt.d=dis[to];nxt.id=to;
q.push(nxt);
}
}
}
}
int main(){
//freopen("Cola.txt","r",stdin);
scanf("%d%d%d%d%d",&m,&n,&s,&t1,&t2);
int x,y,z;
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
Insert(x,y,z);
Insert(y,x,z);
}
int a1,a2=0x7fffffff;
Dij(s,t1,t2);
a1=min(dis[t1],dis[t2]);
Dij(t1,t2,t2);
a2=min(dis[t2],a2);
Dij(t2,t1,t1);
a2=min(dis[t1],a2);
cout<<a1+a2;
}

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

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

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

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

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

  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. 洛谷P3004 [USACO10DEC]宝箱Treasure Chest

    P3004 [USACO10DEC]宝箱Treasure Chest 题目描述 Bessie and Bonnie have found a treasure chest full of marvel ...

  8. 洛谷——P2386 放苹果

    P2386 放苹果 题目背景 (poj1664) 题目描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发(5,1,1和1,1,5是同一种方法) 输入输出格式 输入 ...

  9. 洛谷——P2690 接苹果

    P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...

随机推荐

  1. MV45AOZZ 销售订单增强点

    [转自 http://blog.csdn.net/zhongguomao/article/details/6712580]choose the table VBAP or VBAK ( dependi ...

  2. spring 获取bean的几种方式

    1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationContext applicationContext = new ClassPathXmlApplicationCon ...

  3. X86汇编 BT

    位操作指令位操作指令包括位测试和位扫描指令,可以直接对一个二进制位进行测试,设置和扫描. 1位测试和设置指令 格式:BT DEST,SRC BTC DEST,SRC BTR DEST,SRC BTS ...

  4. iOS define 宏定义 和 const定义常量区别

    const   const 是c++中的修饰符.  c++中常用来定义常量,修饰左值. #define 宏定义语句, 在预处理阶段直接做文本替换,不做类型检查. 它们之间的最大区别: 1.  对于co ...

  5. STS、Eclipse报java.lang.OutOfMemoryError: PermGen space 内存溢出

    我使用的工具是STS, Eclipse同理: 打开如下界面: 左则选择项目启动使用的Tomcat-->在右侧面板Tab项中选择" Arguments":在VM argumen ...

  6. 一篇文章教你如何用R进行数据挖掘

    一篇文章教你如何用R进行数据挖掘 引言 R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来.得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器 ...

  7. mooc_java 集合框架下

    1.判断List中课程是否存在 /** * 测试List的contains方法 * @param args */ public void testListContains(){ Course cour ...

  8. curl的安装与使用

    linux 下的curl扩展安装,记录一下(发现网上好多抄袭的也不检测一下能不能用) 1.下载curl安装包: https://curl.haxx.se/download.html 2.解压: 如 t ...

  9. Mysql远程登陆错误:ERROR 2003

    不能远程登陆Mysql,错误:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.0.114' (10060).原因是电脑防火墙 ...

  10. 一些flag

    连续两道组合计数题 WA 在杨辉三角上,TM 我要是联赛杨辉三角萎了就剁*. 又连续两道题萎在数组越界上,TM 我要是联赛数组越界就吃*.