Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery
Description
贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的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各不相同。
Input
第一行,5个整数,\(C,P,PB,PA1,PA2\)(含义如题所述)
接下来\(C\)行,每行三个整数\(x,y,z\)描述一条无向边。
Output
一个整数,代表最小距离.
难得遇到一个这么裸的最短路题。
貌似\(Spfa\)会被\(Hack\)?
直接写\(Dijkstra\)。
分别以\(PA1\),\(PA2\)为起点跑\(Dijkstra\)。
每次对于到达其他两个点的距离取\(min\)即可。
代码
#include<cstdio>
#include<algorithm>
#include<queue>
#include<iostream>
#define R register
using namespace std;
const int gz=200008;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int m,n,s,a,b,ans=214748364;
int head[gz],tot,dis[gz];
bool vis[gz];
struct hop
{
int u,d;
bool operator <(const hop&a)const
{
return d>a.d;
}
};
struct cod{int u,v,w;}edge[gz<<2];
inline void add(R int x,R int y,R int z)
{
edge[++tot].u=head[x];
edge[tot].v=y;
edge[tot].w=z;
head[x]=tot;
}
inline void dijkstra(R int s)
{
for(R int i=1;i<=n;i++)dis[i]=214748364,vis[i]=false;
priority_queue<hop>q;
q.push((hop){s,dis[s]=0});
while(!q.empty())
{
R int u=q.top().u;q.pop();
if(vis[u])continue;
vis[u]=true;
for(R int i=head[u];i;i=edge[i].u)
{
if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v]=dis[u]+edge[i].w;
q.push((hop){edge[i].v,dis[edge[i].v]});
}
}
}
}
int main()
{
in(m),in(n),in(s),in(a),in(b);
for(R int i=1,x,y,z;i<=m;i++)
{
in(x),in(y),in(z);
add(x,y,z);add(y,x,z);
}
dijkstra(a);
ans=min(ans,dis[b]+dis[s]);
dijkstra(b);
ans=min(ans,dis[a]+dis[s]);
printf("%d",ans);
}
Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery的更多相关文章
- 洛谷——P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...
- 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
- P3003 [USACO10DEC]苹果交货Apple Delivery
题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...
- luoguP3003 [USACO10DEC]苹果交货Apple Delivery
LOL新英雄卡莎点击就送 一句话题意: 三个点a1,a2,b,求从b到a1和a2的最短路 做法:求出a1->b和a2->b的最短路,两者取min,之后再加上a1->a2的最短路 为啥 ...
- 洛谷P3003 苹果交货Apple Delivery
题目描述 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的\(C(1 \leq C \leq 200000)\)条"牛路"都被包含在一种常用的图中,包含了\(P(1 \ ...
- 【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 ...
- USACO Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 洛谷传送门 JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery JDOJ ...
- BZOJ 2100: [Usaco2010 Dec]Apple Delivery( 最短路 )
跑两遍最短路就好了.. 话说这翻译2333 ---------------------------------------------------------------------- #includ ...
随机推荐
- Visual Studio调试之断点技巧篇
原文链接地址:http://blog.csdn.net/Donjuan/article/details/4618717 函数断点 在前面的文章Visual Studio调试之避免单步跟踪调试模式里面我 ...
- git使用笔记(二)分支与合并
By francis_hao Nov 18,2016 查看分支,* 表示当前所在分支 $ git branch 查看分支和最后一次提交记录 $ git branch -v 新建分支 $ git ...
- idea 的http client的使用
1.打开idea的http client的工具步骤如下图所示: 然后在http client 的工具里写:请求头,请求参数,请求体即可.
- Active Directory Domain Services in Windows Server 2016/2012
Applies To: Windows Server 2016, Windows Server 2012 R2, Windows Server 2012 You will find links to ...
- AngularJs学习——模拟用户登录的简单操作
效果截图:
- Sencha touch中Ext.List的使用及高度自适应
最近在做 Sencha 的一个项目,需要用到 Ext.List 来列出所需商品及相关信息,平时我们使用 Ext.List 都是使用 fullscreen:true 来设置 List 全屏显示, 但 ...
- io缓冲为何可以提高效率
问题 据我了解,运用FileInputStream读写一段数据是一个字节一个字节的读取,如果有10个字节大小的文件,就要调用10次系统调用,每次将读取的数据赋值给变量,然后程序使用变量. 缓冲区可以看 ...
- 在Servlet中出现一个输出中文乱码的问题
添加:reqeust.setCharacterEncoding("utf-8");
- 【NOI2014】起床困难综合症 位运算+贪心
这道题先求出0和-1经过处理后的答案 具体看代码吧 #include<cstdio> #include<cstring> #include<algorithm> u ...
- CCCC练习即感
字符串进行初始化时不能通过char a[10]={'\0'}来简单进行,写循环或者memset,亲测有效,以及初始化分好情况,用空格还是'\0',别乱搞. 有一个有意思的题,连续因子,从2开始,依次向 ...