P3003 [USACO10DEC]苹果交货Apple Delivery

这题没什么可说的,跑两遍单源最短路就好了

$Spfa$过不了,要使用堆优化的$dijkstra$

细节:1.必须使用优先队列+堆

2.更新方式跟$Spfa$有所不同

#include<bits/stdc++.h>

using namespace std;

void in(int &x){
register char c=getchar();x=;int f=;
while(!isdigit(c)){if(c=='-') f=-;c=getchar();}
while(isdigit(c)){x=x*+c-'';c=getchar();}
x*=f;
} int c,p,p_b,pa_1,pa_2,head[],vis[],d[],tot,ans;
struct node{
int to,dis,pre;
}e[];
void add(int u,int v,int w){
e[++tot].to=v;e[tot].pre=head[u];head[u]=tot;e[tot].dis=w;
}
struct npde{
int to,dis;
bool operator < (const npde &x) const{
return dis>x.dis;
}
};
priority_queue<npde>Q;
void spfa(int s){
while(!Q.empty()) Q.pop();
memset(vis,,sizeof(vis));memset(d,0x7f,sizeof(d));
d[s]=;Q.push((npde){s,});
while(!Q.empty()){
int u=Q.top().to;Q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=head[u],v;v=e[i].to,i;i=e[i].pre){
if(d[v]>d[u]+e[i].dis){
d[v]=d[u]+e[i].dis;
Q.push((npde){v,d[v]});
}
}
}
}
int main()
{
in(c),in(p),in(p_b),in(pa_1),in(pa_2);
for(int i=;i<=c;i++){
int u,v,w;
in(u),in(v),in(w);
add(u,v,w);add(v,u,w);
}spfa(p_b);
if(d[pa_1]<d[pa_2])ans+=d[pa_1],spfa(pa_1),ans+=d[pa_2];
else ans+=d[pa_2],spfa(pa_2),ans+=d[pa_1];
printf("%d\n",ans);
return ;
}

洛谷——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 题目描述 Bessie has two crisp red apples to deliver to two of her f ...

  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. ARP协议(4)ARP编程

    之前的几篇文章,分别介绍了 ARP 协议格式,在vs2012里配置winpcap环境,我们该做的准备都已经做完了.如今我们真正来实现了. 一.定义数据结构 依据ARP的协议格式,设计一个ARP协议格式 ...

  2. 从service启动activity startActivity慢 的解决方案

    Intent intent = new Intent(context, A.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Pendin ...

  3. 用msys1.0+mingw gcc4.7.1编译libodb(C++ ORM)

    这里主要使用的是libodb 对sqlite数据库的支持. (1)下载编译sqlite3,当初编译go-sqlite3的时候下载过sqlite3,就直接拿过来用了. start msys.bat gc ...

  4. MySQL5.7 windows二进制安装

    200 ? "200px" : this.width)!important;} --> 介绍 1.下载解压 下载地址:http://dev.mysql.com/get/Dow ...

  5. table样式测试总结tr td宽度分析

    题外话:一直以来习惯布局用ul,li样式调整比较方便,不会互相影响出现一些问题,but~现在公司涉及很多表格打印,都是用table写的,好多宽度高度合并啊,组合啊~~~,单元格之间互相影响,有的样式设 ...

  6. bzoj 1045: [HAOI2008] 糖果传递【瞎搞】

    感觉我的智商可能不够写题解,就直接截了hzwer的blog 地址http://hzwer.com/2656.html #include<iostream> #include<cstd ...

  7. bzoj 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店【高精度+完全背包】

    居然要用高精度! 懒得operator了,转移是裸的完全背包 #include<iostream> #include<cstdio> using namespace std; ...

  8. 解方程 2014NOIP提高组 (数学)

    解方程  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 输入描述 Input Description 输入文 ...

  9. mysql插入二千万的测试数据进行测试,int和datetime比较

    时间存储用int和datetime哪个字段更合适,建立下面的两个表 测试环境是内存2个G,一核的虚拟机 CREATE TABLE `test_inttime` ( `id` int(11) NOT N ...

  10. 数据传递-------@PathVariable

    package com.wh.handler; /** * 通过@PathVariable可以绑定占位符参数到方法参数中,例如 * @PathVariable("userId") ...