P3003 [USACO10DEC]苹果交货Apple Delivery
题目描述
Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she travels the C (1 <= C <= 200,000)
cowpaths which are arranged as the usual graph which connects P (1 <= P <= 100,000) pastures conveniently numbered from 1..P: no cowpath leads from a pasture to itself, cowpaths are bidirectional, each cowpath has an associated distance, and, best of all, it is always possible to get from any pasture to any other pasture. Each cowpath connects two differing pastures P1_i (1 <= P1_i <= P) and P2_i (1 <= P2_i <= P) with a distance between them of D_i. The sum of all the distances D_i does not exceed 2,000,000,000.
What is the minimum total distance Bessie must travel to deliver both apples by starting at pasture PB (1 <= PB <= P) and visiting pastures PA1 (1 <= PA1 <= P) and PA2 (1 <= PA2 <= P) in any order. All three of these pastures are distinct, of course.
Consider this map of bracketed pasture numbers and cowpaths with distances:
3 2 2
[1]-----[2]------[3]-----[4]
\ / \ /
7\ /4 \3 /2
\ / \ /
[5]-----[6]------[7]
1 2
If Bessie starts at pasture [5] and delivers apples to pastures [1] and [4], her best path is:
5 -> 6-> 7 -> 4 -> 3 -> 2 -> 1
with a total distance of 12.
贝西有两个又香又脆的红苹果要送给她的两个朋友。当然她可以走的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各不相同。
输入输出格式
输入格式:
Line 1: Line 1 contains five space-separated integers: C, P, PB, PA1, and PA2
- Lines 2..C+1: Line i+1 describes cowpath i by naming two pastures it connects and the distance between them: P1_i, P2_i, D_i
输出格式:
- Line 1: The shortest distance Bessie must travel to deliver both apples
输入输出样例
9 7 5 1 4
5 1 7
6 7 2
4 7 2
5 6 1
5 2 4
4 3 2
1 2 3
3 2 2
2 6 3
/*这道题很好做,直接套slf优化的模板,跑两次取最小值,就好了,本人先开始把slf和lll优化一起加了进去,但是由于lll被常数卡爆了,卡到指数级去了,然后怎么优化都会T掉第二个点,如果用lll优化会T掉4个点(本人亲手实验),用slf可以A掉,而且很快。直接上代码吧。*/ #include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
const int gg=;
const int INF=1e9;
int head[gg];
struct node
{
int next;
int w;
int to;
} a[gg];
int dis[gg];
bool vis[gg];
int c,p,pb,pa1,pa2,cnt;
int ans;
int ans2;
int sum,tot;
int rans;
inline void add(int i,int j,int w)
{
a[++cnt].to=j;
a[cnt].next=head[i];
a[cnt].w=w;
head[i]=cnt;
}
inline void spfa(int s)
{
deque<int>q;
memset(vis,false,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
dis[s]=;
vis[s]=true;
q.push_back(s);
while(!q.empty())
{
int u=q.front();
q.pop_front();
vis[u]=false;
for(register int i=head[u]; i; i=a[i].next)
{
int v=a[i].to;
if(dis[v]>dis[u]+a[i].w)
{
dis[v]=dis[u]+a[i].w;
if(!vis[v])
{
vis[v]=true;
if(q.empty()||dis[v]>dis[q.front()])
{
q.push_back(v);
}
else
q.push_front(v);
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&c,&p,&pb,&pa1,&pa2);
for(register int i=; i<=c; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
spfa(pa1);
ans+=dis[pa2]+dis[pb];
spfa(pa2);
ans2+=dis[pa1]+dis[pb];
rans=min(ans,ans2);
printf("%d\n",rans);
return ;
}
12
P3003 [USACO10DEC]苹果交货Apple Delivery的更多相关文章
- 洛谷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 这题没什么可说的,跑两遍单源最短路就好了 $Spfa$过不了,要使用堆优化的$dijkstra$ 细节:1.必须使用优先队列+ ...
- 洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery
洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of he ...
- Dijkstra【p3003(bzoj2100)】[USACO10DEC]苹果交货Apple Delivery
Description 贝西有两个又香又脆的红苹果要送给她的两个朋友.当然她可以走的C(1<=C<=200000)条"牛路"都被包含在一种常用的图中,包含了P(1< ...
- 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 \ ...
- 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 ...
- iOS - 苹果官方Apple Pay开发文档(中文版)- Apple Pay(1)
翻译自苹果官方Apple Pay开发文档.目前版本为1.0 概览: Apple Pay为用户从你的App里购买实际的物品和服务提供简单而安全的方法.通过Touch ID,用户可使用储存在iPhone ...
随机推荐
- [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置
接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...
- [ASP.NET教程] 防止表单重复提交
第一种方法:javascript控制.缺点,一般用户使用没问题,但是懂点js的还是可以强行重复提交.而且,后退再提交,你也没啥办法.第二种方法:服务器控制.后台生成一个token,存入session或 ...
- PHP程序员40点陋习
1.不写注释 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行sql前不执行编码和安全检测 8.不使用测 ...
- 从零开始配置TypeScript + React + React-Router + Redux + Webpack开发环境
转载请注明出处! 说在前面的话: 1.为什么不使用现成的脚手架?脚手架配置的东西太多太重了,一股脑全塞给你,我只想先用一些我能懂的库和插件,然后慢慢的添加其他的.而且自己从零开始配置也能学到更多的东西 ...
- eslint使用
参考文档 http://www.cnblogs.com/hahazexia/p/6393212.html http://blog.guowenfh.com/2016/08/07/ESLint-Rule ...
- XML读取信息并显示
这个类命名叫Message.cs namespace Common { public class Message { /// <summary> /// 信息编号 /// </sum ...
- Jquery cookie操作示例,写入cookie,读取cookie,删除cookie
<html> <head> <meta name="viewport" content="width=device-width" ...
- Window2008 R2(64位)使用codesmith连接Sqlite
①打开C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config目录,找到machine.config文件新增 <add name=" ...
- DevOps之网络
唠叨话 关于德语噢屁事的知识点,仅提供专业性的精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. <网络(Network)> 关于网络的网络架构和网络模型:知识与技能的层次(知道. ...
- 史上最难的一道Java面试题 (分析篇)
博客园 匠心零度 转载请注明原创出处,谢谢! 无意中了解到如下题目,觉得蛮好. 题目如下: public class TestSync2 implements Runnable { int b = 1 ...