题目描述

一张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. HDU 1856 More is better【并查集】

    解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值 More is better Time Limit: 5000/1000 MS (J ...

  2. Paper Reading: Relation Networks for Object Detection

    Relation Networks for Object Detection笔记  写在前面:关于这篇论文的背景知识,请参考我前面的两篇随笔(<关于目标检测>和<关于注意力机制> ...

  3. map、栈————下一个更大的元素(待定,栈解法学习中)

    方法一 先遍历nums2,将每个元素后面第一个大的元素一起存入到map中,然后在遍历nums1,在map中找到. class Solution { public: vector<int> ...

  4. ES6 学习3 函数

    1.函数默认参数 在ES5我们给函数定义参数默认值是怎么样? function action(num) { num = num || 200 //当传入num时,num为传入的值 //当没传入参数时, ...

  5. 常见VPS buy地址

    ***,也是最适合新手使用的: https://bwh1.net/ (支持支付宝) vultr,以下是我的分享链接: https://www.vultr.com/(支持支付宝) SugarHosts: ...

  6. 2019年北航OO第一单元(表达式求导任务)总结

    2019面向对象课设第一单元总结 一.三次作业总结 1. 第一次作业 1.1 需求分析 第一次作业的需求是完成简单多项式导函数的求解,表达式中每一项均为简单的常数乘以幂函数形式,优化目标为最短输出.为 ...

  7. 【codeforces 404D】Minesweeper 1D

    [题目链接]:http://codeforces.com/problemset/problem/404/D [题意] 让你玩一个1维的扫雷游戏; 游戏的描述由数字0..2以及符号*表示; 分别表示这个 ...

  8. 笔记本win2008 r2的hyper-v安装centos

    一.i5以上cpu支持虚拟化,不过默认是关闭的,先到bios设置里把虚拟功能打开: 二.“服务器管理器”->“角色”里安装hyper-v并重启: 三.设置无线网络桥接,有线就不需要了,具体如下: ...

  9. POJ——T 1422 Air Raid

    http://poj.org/problem?id=1422 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8579   A ...

  10. ubuntu下不用拔盘就可以重新识别usb设备

    #!/bin/sh # Usage: ./resetusb ARGUMENT(The keyword for your usb device) var1=$ keyword=${var1:=Stora ...