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

对于后者我们很好解决,就是很经典的单源最短路径,跑一边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. ubuntu 18.04设置系统自带系统截图快捷键

    0.前言 ubuntu 18.04自带一个截图工具gnome-screenshot,有三种模式,全屏截图.当前活动窗口截图.选取活动区域截图 1.设置快捷键 Setting->Devices-& ...

  2. 20180805-Java 异常处理

    try{ //程序代码}catch(ExceptionName e1){ //Catch 块} 下面的例子中声明有两个元素的一个数组,当代码试图访问数组的第三个元素的时候就会抛出一个异常. //文件名 ...

  3. laravel5.6 操作数据 Eloquent ORM

    建立Users模型 <?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class ...

  4. 虚拟机中安装Linux_Centos7操作系统(最小化安装)

    我们打开之前安装的VM, 点击 “创建新的虚拟机”: 我们选 典型 安装  简单点 然后下一步: 我们稍后安装操作系统 ,点下一步: 这里选 Linux 然后下拉 选CentOS 64位,然后下一步: ...

  5. Ubuntu 18.04 截图工具-flameshot(安装及使用)

    安装flameshot:https://github.com/lupoDharkael/flameshot 安装命令: sudo apt-get install flameshot 设置>设备& ...

  6. 使用python执行sql语句和外键解析

    一.下载并导入pymysql pip install pymysql && import pymysql db=pymysql.connect(host=) #如果报错host大概率因 ...

  7. Python变量和字符串详解

    Python变量和字符串详解 几个月前,我开始学习个人形象管理,从发型.妆容.服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找到参考对象去模仿,可以是自己欣 ...

  8. Vagrant 入门 - 配置

    原文地址 现在我们已经有了一个运行 Ubuntu 的虚拟机,并且可以在宿主机上编辑文件并自动同步到虚拟机.现在让我们安装一个 web 服务器,通过服务器访问这些文件. 可以通过 SSH 进入并安装一个 ...

  9. Aizu - ALDS1_4_C Dictionary

    Search III Your task is to write a program of a simple dictionary which implements the following ins ...

  10. Activation Functions and Their Derivatives

    1. Sigmoid Function: when z=0,g'(z)=0.25 2. tanh Function: when x=0,tanh'(x)=1 3. Relu