洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party


【题解】
其实解法
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define rg register
#define N 200010
using namespace std;
int n,m,s,ans,tot,last[N],dis[N],dis2[N],pos[N];
struct rec{
int u,v,d;
}r[N<<];
struct edge{
int to,pre,dis;
}e[N<<];
struct heap{
int poi,dis;
}h[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void up(int x){
int fa;
while((fa=(x>>))&&h[fa].dis>h[x].dis){
swap(h[fa],h[x]); swap(pos[h[fa].poi],pos[h[x].poi]);
x=fa;
}
}
inline void down(int x){
int son;
while((son=(x<<))<=tot){
if(son<tot&&h[son].dis>h[son+].dis) son++;
if(h[son].dis<h[x].dis){
swap(h[son],h[x]); swap(pos[h[son].poi],pos[h[x].poi]);
x=son;
}
else return;
}
}
inline void dijkstra(int x){
for(rg int i=;i<=n*;i++) dis[i]=1e9;
h[tot=pos[x]=]=(heap){x,dis[x]=};
while(tot){
int now=h[].poi; h[]=h[tot--]; if(tot) down();
for(rg int i=last[now],to;i;i=e[i].pre)
if(dis[to=e[i].to]>dis[now]+e[i].dis){
dis[to]=dis[now]+e[i].dis;
if(!pos[to]) h[pos[to]=++tot]=(heap){to,dis[to]};
else h[pos[to]].dis=dis[to];
up(pos[to]);
}
pos[now]=;
}
}
int main(){
n=read(); m=read(); s=read();
for(rg int i=;i<=m;i++){
int u=read(),v=read(),d=read();
r[i]=(rec){u,v,d};
e[++tot]=(edge){v,last[u],d}; last[u]=tot;
}
dijkstra(s);
for(rg int i=;i<=n;i++) dis2[i]=dis[i];
memset(last,,sizeof(last));
for(rg int i=;i<=m;i++){
int u=r[i].u,v=r[i].v,d=r[i].d;
e[++tot]=(edge){u,last[v],d}; last[v]=tot;
}
dijkstra(s);
for(rg int i=;i<=n;i++) ans=max(ans,dis[i]+dis2[i]);
printf("%d\n",ans);
return ;
}
就是先做一遍最短路,把所有边反向,再做一遍最短路,最后找两次的dis之和的最大值。
洛谷 1821 [USACO07FEB]银牛派对Silver Cow Party的更多相关文章
- 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party 题解
P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 洛谷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 ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...
- 「Luogu 1821」[USACO07FEB]银牛派对Silver Cow Party
更好的阅读体验 Portal Portal1: Luogu Portal2: POJ Description One cow from each of N farms \((1 \le N \le 1 ...
- 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 ...
- 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 ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
随机推荐
- 1998 NOIP 拼数 string 和sort的新理解
题目:https://www.luogu.org/problemnew/show/P1012 今天真是长了见识.这道题做了十几分钟,用模拟愣是调不出来.直到我看了题解——(当场去世)…… 题的意思是n ...
- 12. Ext.Ajax 对ajax的支持
转自:http://www.cnblogs.com/lipan/archive/2011/12/09/2272793.html 本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持,包括Ajax封 ...
- E2017614-hm
pluck n. 勇气,精神; 内脏; 快而猛的拉; 〈俚〉不及格; vt. 拔掉; 采,摘; 鼓起(勇气等); 弹(乐器); scope n. (处理.研究事务的) 范围; 眼界,见识 ...
- bzoj 1492: [NOI2007]货币兑换Cash【贪心+斜率优化dp+cdq】
参考:http://www.cnblogs.com/lidaxin/p/5240220.html 虽然splay会方便很多,但是懒得写,于是写了cdq 首先要想到贪心的思路,因为如果在某天买入是能得到 ...
- 关于Anaconda环境变量配置遇到的一些情况说明
安装和配置环境变量的话就不多说了,大家可以参照这个说的去做就行 https://blog.csdn.net/weixin_42997646/article/details/89414769 验证配置环 ...
- NetCore Netty 框架 BT.Netty.RPC 系列随讲 —(前序) REST API 与 RPC 经典网络基础服务架构
在服务体系架构内,我们所知道的,有两种请求模型: Http 请求模型,以及 RPC 请求模型.因此,在一个互联网请求模型架构上,都是这两种的请求模型的向互组合. 下面给出两种常见的互联网经典基础架构图 ...
- 认识BACnet协议
一.什么是BACnet? BACnet,Building Automation and Control networks的简称,即楼宇自动化与控制网络.是用于智能建筑的通信协议. 一般楼宇自控设备从功 ...
- Android 性能优化(7)网络优化( 3) Optimizing User-Initiated Network Use
Optimizing User-Initiated Network Use This lesson teaches you to Pre-fetch Network Data Check for Co ...
- lua调用java java调用lua[转载]
转载:http://dualface.github.io/blog/2013/01/01/call-java-from-lua/LuaJavaBridge - Lua 与 Java 互操作的简单解决方 ...
- 解决Unicode编码(璘)
随着互联网发展,B/S越来越受欢迎 Code编码格式也越来载多, 在大千花花世界 中文在Web显示看似一样但实际编码并不样,导致从页面获取的资料录入到数据库中时 存取的就是Code编码 如:Unico ...