洛谷 P3003 [USACO10DEC]苹果交货Apple Delivery

洛谷传送门

JDOJ 2717: USACO 2010 Dec Silver 1.Apple Delivery

JDOJ传送门

Description

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.

Input

* 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

Output

* Line 1: The shortest distance Bessie must travel to deliver both

apples

Sample Input

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

Sample Output

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各不相同。

题解:

这道题如果用裸的SPFA做会TLE,所以要加优化。

如果有像20分钟之前的我一样对SPFA算法优化一无所知的人,请移步我的上一篇博客:

优化SPFA基础知识传送门

但是光知道怎么优化是不够的,我们还要就这个题想一想怎么写。

首先,这个题的源点不再是一,所以SPFA的时候要传参数。

其次,最后统计答案的时候要比较一下。

最后,拍一遍SPFA模板(SLF LLL优化版都可以)(本蒟蒻比较喜欢SLF),AC。

注意双向边的问题。

CODE:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int m,n,s,t1,t2;
int tot,to[400001],val[400001],nxt[400001],head[100001];
int dist[100001],v[100001];
void add(int x,int y,int z)
{
to[++tot]=y;
val[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
void spfa(int start)
{
memset(v,0,sizeof(v));
memset(dist,0x3f,sizeof(dist));
deque<int> q;
dist[start]=0;
v[start]=1;
q.push_front(start);
while(!q.empty())
{
int x=q.front();
q.pop_front();
v[x]=0;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(dist[x]+val[i]<dist[y])
{
dist[y]=dist[x]+val[i];
if(!v[y])
{
if(!q.empty() && dist[y]<dist[q.front()])
q.push_front(y);
else
q.push_back(y);
v[y]=1;
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&m,&n,&s,&t1,&t2);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int ans1=0;
int ans2=0;
spfa(t1);
ans1+=dist[t2]+dist[s];
spfa(t2);
ans2+=dist[t1]+dist[s];
int ans=min(ans1,ans2);
printf("%d",ans);
return 0;
}

最新文章

  1. Solr术语介绍:SolrCloud,单机Solr,Collection,Shard,Replica,Core之间的关系
  2. (转)mysql中InnoDB表为什么要建议用自增列做主键
  3. localStorage实现购物车数量单价和结算页面的实时同步
  4. [SAP ABAP开发技术总结]RETURN、STOP、EXIT、CHECK、LEAVE、REJECT
  5. 《Java数据结构与算法》笔记-CH2有序数组
  6. Gradle Goodness: Set Java Compiler Encoding--转载
  7. Package org.xml.sax Description
  8. NS2仿真:使用NS仿真软件模拟简单网络模型
  9. 如果用float实现居中
  10. Generating Faces with Deconvolution Networks
  11. 【Hadoop】 2.7.3版本 hdfs 命令行使用
  12. yaf框架刚开始遇到的问题
  13. easyui_validatebox常用验证
  14. 详解Linux内核红黑树算法的实现
  15. [lightoj P1306] Solutions to an Equation
  16. Mysql建了索引查询很慢
  17. 退出循环break,在while、for、do...while、循环中使用break语句退出当前循环,直接执行后面的代码。
  18. tflearn 实现DNN 全连接
  19. xmpp实现的即时通讯聊天(一)
  20. 20155334 2016-2017-2《Java程序设计》课程总结

热门文章

  1. vscode中关于launch.json和tasks.json的变量说明
  2. 中国移动明确5G手机入网技术标准:四大要求缺一不可!
  3. 【shell脚本】打印九九乘法表
  4. git 创建标签 tag
  5. Logstash:运用jdbc_streaming来丰富我们的数据
  6. virtualbox 配置记录
  7. SQL Server中,如何查看每个数据库的Owner是哪个SQL Server账户,也就是谁创建的
  8. The connection string name is missing for the MySqlSiteMapProvider
  9. ASP.NET Core 进程外(out-of-process)托管
  10. App_Code下类无法引用问题