洛谷 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---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
随机推荐
- bzoj3270
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 474 Solved: 261[Submit][Status][Discuss] ...
- Java Graphics2D类的绘图方法
Graphics2D继承自Graphics,它扩展了Graphics的绘图功能,拥有更强大的二维图形处理能力,提供对几何形状.坐标转换.颜色管理以及文字布局等更精确的控制. Graphics2D定义了 ...
- IDEA中Spark往Hbase中写数据
import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.io.ImmutableBytesWr ...
- 插入CSS的方法
传送门 选择器 selector {declaration1; declaration2; ... declarationN } 例: p { text-aligh:center; } ...
- HTML5常用知识点
github代码:https://github.com/showkawa/H5C3/tree/master/html5 1.自定义属性 data- 1.1 可以给html里的所有DOM对象都可以添加一 ...
- sql 索引详解
索引的重要性 数据库性能优化中索引绝对是一个重量级的因素,可以说,索引使用不当,其它优化措施将毫无意义. 聚簇索引(Clustered Index)和非聚簇索引 (Non- Clustered Ind ...
- boxworld开发日记2019-6-8
打算做一个类似RimWorld的游戏,这里记录一下历程.首先,简单回顾一下. 2018年12月23日 场景管理,打算使用四叉树,后来发现四叉树在空间组织和内存占用方面并不占优势,之后计划使用地图分块 ...
- 【Nodejs】记一次图像识别的冒险
笔者的团队最近接到了一个有关图像识别的需求,本来应该由后端团队提供能力,native提供容器,前端团队仅负责一些“外围的形式工作”,不过由于各种各样的原因,最后的结果变成了前端团队在原有工作基础上,承 ...
- 区间DP UVA 1351 String Compression
题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...
- CSS + radius 五环
使用CSS的外链方式,写了一个五环 CSS的布局 附加radius的使用 思路: 一个大盒子里放两个子盒子: 两个子盒子上下排列,分别放3个和2个盒子用来制作圆环: 大盒子给相对定位,连个子盒子设为绝 ...