题目描述

一张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. xBIM 基础06 将STEP物理文件转换为XML

    系列目录    [已更新最新开发文章,点击查看详细]  一.STEP标准简介 STEP,它是Standard for the Exchange of Product model data的缩写.产品数 ...

  2. 51nod 1065 最小正字段和 解决办法:set存前缀和,二分插入和二分查找

    题目: 这题要求大于0的最小字段和,常规O(n)求最大字段和的方法肯定是没法解的. 我的解法是:用sum[i]存前i项的和,也就是前缀和. 这题就变成了求sum[j]-sum[i]的大于0的最小值( ...

  3. split(":")[0].substring(1)

    java中拆分字符中的split(":")[0].substring(1)是什么意思啊,尤其[0] 可以解释一下吗?:比如你有一个字符串 "111:222:333&quo ...

  4. SGU 180 Inversions【树状数组】

    题意:求逆序数 和POJ那题求逆序数的一样,不过这题离散化之后,要去一下重,然后要开到long long #include<iostream> #include<cstdio> ...

  5. 响应式流API的构建基础

    下面三个重要的概念是响应式流API的构建基础: 发布者是事件的发送方,可以向它订阅. 订阅者是事件订阅方. 订阅将发布者和订阅者联系起来,使订阅者可以向发布者发送信号. http://www.info ...

  6. 洛谷P3567 [POI2014]KUR-Couriers 主席树

    挺裸的,没啥可讲的. 不带修改的主席树裸题 Code: #include<cstdio> #include<algorithm> using namespace std; co ...

  7. centos安装nvidia驱动

    大部分 Linux 发行版都使用开源的显卡驱动 nouveau,对于 nvidia 显卡来说,还是闭源的官方驱动的效果更好.最明显的一点是,在使用 SAC 拾取震相的时候,使用官方显卡驱动在刷新界面的 ...

  8. Python学习————列表的增删改查

    增加:li.append(对象):追加 注:print(li.append())--->是Noneli.insert(索引,对象):插入到相应位置li.extend(对象):可迭代的添加到尾部, ...

  9. python 面向对象 继承

    什么是继承 继承表达的是一种”是“的关系,比如人是动物 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 继承是基于抽象的结 ...

  10. ZOJ 3369 Saving Princess

    Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...