题目描述

一张P个点的无向图,C条正权路。CLJ要从Pb点(家)出发,既要去Pa1点NOI赛场拿金牌,也要去Pa2点CMO赛场拿金牌。(途中不必回家)可以先去NOI,也可以先去CMO。当然神犇CLJ肯定会使总路程最小,输出最小值。

题解:做两遍spfa,找出从起点开始先去pa1或者先去pa2的最小值

需要用一下spfa的优化,每次进行入队的时候都与队头进行比较,如果比队头小就放在队头,否则放队尾。

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm 200005
int dis[maxn],head[maxn],q[maxn];
bool vis[maxn];
struct edge{
int next,to,w;
}e[maxm*];
int n,m,s,t1,t2;
int ans=,cnt;
inline int read(){
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void insert(int u,int v,int w){
cnt++;
e[cnt].next=head[u];e[cnt].to=v;e[cnt].w=w;
head[u]=cnt;
}
void spfa(int x){
memset(dis,,sizeof dis);
vis[x]=;dis[x]=;q[]=x;
int top=,tail=;
while(top!=tail)
{
int now=q[top];top++;
if(top==)top=;
for(int i=head[now];i;i=e[i].next){
int p=e[i].to;
if(dis[p]>dis[now]+e[i].w){
dis[p]=dis[now]+e[i].w;
if(!vis[p]){
vis[p]=;
if(dis[p]<dis[q[top]]){
top--;if(top<)top=;
q[top]=p;
}
else{
q[tail++]=p;
if(tail==)tail=;
}
}
}
}
vis[now]=;
}
}
int main(){
m=read();n=read();s=read();t1=read();t2=read();
int u,v,w;
for(int i=;i<=m;i++){
u=read();v=read();w=read();
insert(u,v,w);insert(v,u,w);
}
spfa(t1);
ans=dis[s]+dis[t2];
spfa(t2);
ans=min(ans,dis[s]+dis[t1]);
printf("%d",ans);
}

bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易的更多相关文章

  1. bzoj2100 [Usaco2010 Dec]Apple Delivery

    Description Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, ...

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

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

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

  4. 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...

  5. bzoj 2100: [Usaco2010 Dec]Apple Delivery【spfa】

    洛谷数据好强啊,普通spfa开o2都过不了,要加双端队列优化 因为是双向边,所以dis(u,v)=dis(v,u),所以分别以pa1和pa2为起点spfa一遍,表示pb-->pa1-->p ...

  6. BZOJ 2100: [Usaco2010 Dec]Apple Delivery spfa

    由于是无向图,所以可以枚举两个终点,跑两次最短路来更新答案. #include <queue> #include <cstdio> #include <cstring&g ...

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

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

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

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

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

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

随机推荐

  1. JavaScript 基础 if switch 弹窗 运算符

    脚本语言最重要的几个部分: 数据类型 运算符 控制语句 数组  方法(函数) 一.基础知识 关键字:系统定义 有意义的名字如 background link 等 标识符:自己定 比如class的名字a ...

  2. BAT 解密(四):配置中心、服务中心、异步技术细节

    在系列文章的第二篇文章< BAT解密(二):聊聊业务如何驱动技术发展 >中我们深入分析了互联网业务发展的一个特点:复杂性越来越高.复杂性增加的典型现象就是系统越来越多,当系统的数量增加到一 ...

  3. bind方法使用案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. QT笔记 -- (4) 为QLabel添加鼠标响应方法2

    1.实现 bool eventFilter(QObject *target,QEvent *event) 函数内容如下: bool eventFilter(QObject *target,QEvent ...

  5. POJ 1990 MooFest【 树状数组 】

    题意:给出n头牛,每头牛有一个听力v,坐标x,两头牛之间的能量为max(v1,v2)*dist(v1,v2),求总的能量值 先将每头牛按照v排序,排完顺序之后,会发现有坐标比当前的x小的,会有坐标比当 ...

  6. 有趣的console

    博文第一篇,就以前端调试的“座上客”---console开始

  7. [TJOI2011]树的序(贪心,笛卡尔树)

    [TJOI2011]树的序 题目描述 众所周知,二叉查找树的形态和键值的插入顺序密切相关.准确的讲:1.空树中加入一个键值k,则变为只有一个结点的二叉查找树,此结点的键值即为k:2.在非空树中插入一个 ...

  8. Android 之 Eclipse没法生成R文件

    这几天被Eclipse整哭了.也怪自己手贱把appcompat_v7给删了. Eclipse创建project假设是兼容4.0下面,会多生成一个projectappcompat_v7,例如以下图: 这 ...

  9. BZOJ 1088 水模拟

    BZOJ水一道~ 枚举前两个位置是否放雷,模拟向下推.能够则ans++ #include "stdio.h" #include "string.h" int a ...

  10. Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix

    MainActivity例如以下: import android.os.Bundle; import android.view.MotionEvent; import android.view.Vie ...