由题意可知,我们需要求的是很多个点到同一个店的最短距离,然后再求同一个点到很多个点的最短距离。

对于后者我们很好解决,就是很经典的单源最短路径,跑一边dijkstra或者SPFA即可。

然而对于前者,我们应该怎么解决呢?难道我们需要求一边Floyd?当然不可能!\(O(n^3)\)的时间复杂度,对于我们的\(n<=1000\)是果断要超时的。

深入分析,对于一张图,A到B的最短距离,应该等于B到A,在反转一张图以后的最短距离。所谓反转一张图,就是把变得方向调转。这一点是很显然的!

因此,对于问题一,我们只需要把图反转,然后求那个点到其它的最短距离即可。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,a,n) for(register int i=(a);i<=(n);++i)
#define per(i,a,n) for(register int i=(a);i>=(n);--i)
#define fec(i,x) for(register int i=head[x];i;i=Next[i])
#define debug(x) printf("debug:%s=%d\n",#x,x)
#define mem(a,x) memset(a,x,sizeof(a))
template<typename A>inline void read(A&a){a=0;A f=1;int c=0;while(c<'0'||c>'9'){c=getchar();if(c=='-')f*=-1;}while(c>='0'&&c<='9'){a=a*10+c-'0';c=getchar();}a*=f;}
template<typename A,typename B>inline void read(A&a,B&b){read(a);read(b);}
template<typename A,typename B,typename C>inline void read(A&a,B&b,C&c){read(a);read(b);read(c);} const int maxn=1000+7,maxm=100000+7,INF=0x7f7f7f7f;
int u[maxm],v[maxm],w[maxm],Next[maxm],head[maxn],tot;
int u2[maxm],v2[maxm],w2[maxm],Next2[maxm],head2[maxn],tot2;
int n,m,p,x,y,z,ans;
int dist1[maxn],dist2[maxn];
bool visit[maxn]; inline void addedge(int x,int y,int z){
u[++tot]=x;v[tot]=y;w[tot]=z;
Next[tot]=head[x];head[x]=tot;
}
inline void addedge2(int x,int y,int z){
u2[++tot2]=x;v2[tot2]=y;w2[tot2]=z;
Next2[tot2]=head2[x];head2[x]=tot2;
} void Dijkstra(int *u,int *v,int *w,int *head,int *Next,int *dist,int s){
mem(visit,0);dist[s]=0;
rep(i,1,n){
int Min=INF,x;
rep(i,1,n)if(!visit[i]&&dist[i]<Min)Min=dist[i],x=i;
visit[x]=1;
fec(i,x)if(!visit[v[i]]&&dist[v[i]]>dist[x]+w[i])dist[v[i]]=dist[x]+w[i];
}
} void Init(){
read(n,m,p);
rep(i,1,m){
read(x,y,z);
addedge(x,y,z);
addedge2(y,x,z);
}
} void Work(){
mem(dist1,0x7f);mem(dist2,0x7f);
Dijkstra(u,v,w,head,Next,dist1,p);
Dijkstra(u2,v2,w2,head2,Next2,dist2,p);
rep(i,1,n)ans=max(ans,dist1[i]+dist2[i]);
printf("%d\n",ans);
} int main(){
Init();
Work();
return 0;
}

[Luogu1821][USACO07FEB]银牛派对Silver Cow Party的更多相关文章

  1. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  2. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  3. P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  4. 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  5. luogu P1821 [USACO07FEB]银牛派对Silver Cow Party

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  6. 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party

    更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...

  7. [USACO07FEB]银牛派对Silver Cow Party

    题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...

  8. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

  9. 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party

    银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...

随机推荐

  1. php key()函数 语法

    php key()函数 语法 作用:返回数组内部指针当前指向元素的键名.大理石构件支架 语法:key(array) 参数: 参数 描述 array 必需.规定要使用的数组. 说明:返回数组内部指针当前 ...

  2. 20180822-Java接口

    Java 接口 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并 ...

  3. AT3576 E Popping Balls——计数思路

    题目:https://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_e 题解:https://w ...

  4. xsxsxsxsxsxsxsxs

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  5. 【OPCAutomation】 使用OPCAutomation实现对OPC数据的访问

    折腾了一段时间研究OPC,理清了下位机.OPCServer 和OPCClient的关系和通信模型,终于能够来写一篇相关的博客了. 我们使用西门子的 S7 200 SMART作为下位机端,通过3G路由器 ...

  6. shell脚本一一项目3

    主题:批量创建100个用户并设置密码 脚本内容 user_list=$@user_file=./user.infofor USER in ${user_list};do if ! id $USER & ...

  7. Jenkins使用五:创建部署任务

    创建部署任务 选择运行节点 选择使用shell # 如果是持续进程,这里最好加一个kill进程的命令 判断如果/root/production目录存在,就删除if [ -d /root/product ...

  8. 【MM系列】SAP ABAP 在选择画面显示输出结果

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 在选择画面显示 ...

  9. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  10. JavaScript PriorityQueue

    function PriorityQueue() { var items = []; function QueueElement(element, priority) { this.element = ...