洛谷—— P2934 [USACO09JAN]安全出行Safe Travel || COGS ——279|| BZOJ——1576
https://www.luogu.org/problem/show?pid=2934
题目描述
Gremlins have infested the farm. These nasty, ugly fairy-like
creatures thwart the cows as each one walks from the barn (conveniently located at pasture_1) to the other fields, with cow_i traveling to from pasture_1 to pasture_i. Each gremlin is personalized and knows the quickest path that cow_i normally takes to pasture_i. Gremlin_i waits for cow_i in the middle of the final cowpath of the quickest route to pasture_i, hoping to harass cow_i.
Each of the cows, of course, wishes not to be harassed and thus chooses an at least slightly different route from pasture_1 (the barn) to pasture_i.
Compute the best time to traverse each of these new not-quite-quickest routes that enable each cow_i that avoid gremlin_i who is located on the final cowpath of the quickest route from pasture_1 to
pasture_i.
As usual, the M (2 <= M <= 200,000) cowpaths conveniently numbered 1..M are bidirectional and enable travel to all N (3 <= N <= 100,000) pastures conveniently numbered 1..N. Cowpath i connects pastures a_i (1 <= a_i <= N) and b_i (1 <= b_i <= N) and requires t_i (1 <= t_i <= 1,000) time to traverse. No two cowpaths connect the same two pastures, and no path connects a pasture to itself (a_i != b_i). Best of all, the shortest path regularly taken by cow_i from pasture_1 to pasture_i is unique in all the test data supplied to your program.
By way of example, consider these pastures, cowpaths, and [times]:
1--[2]--2-------+
| | | [2] [1] [3]
| | | +-------3--[4]--4
TRAVEL BEST ROUTE BEST TIME LAST PATH
p_1 to p_2 1->2 2 1->2
p_1 to p_3 1->3 2 1->3
p_1 to p_4 1->2->4 5 2->4
When gremlins are present:
TRAVEL BEST ROUTE BEST TIME AVOID
p_1 to p_2 1->3->2 3 1->2
p_1 to p_3 1->2->3 3 1->3
p_1 to p_4 1->3->4 6 2->4
For 20% of the test data, N <= 200.
For 50% of the test data, N <= 3000.
TIME LIMIT: 3 Seconds
MEMORY LIMIT: 64 MB
Gremlins最近在农场上泛滥,它们经常会阻止牛们从农庄(牛棚_1)走到别的牛棚(牛_i的目的 地是牛棚_i).每一个gremlin只认识牛_i并且知道牛_i一般走到牛棚_i的最短路经.所以它 们在牛_i到牛棚_i之前的最后一条牛路上等牛_i. 当然,牛不愿意遇到Gremlins,所以准备找 一条稍微不同的路经从牛棚_1走到牛棚_i.所以,请你为每一头牛_i找出避免gremlin_i的最 短路经的长度. 和以往一样, 农场上的M (2 <= M <= 200,000)条双向牛路编号为1..M并且能让所有牛到 达它们的目的地, N(3 <= N <= 100,000)个编号为1..N的牛棚.牛路i连接牛棚a_i (1 <= a_i <= N)和b_i (1 <= b_i <= N)并且需要时间t_i (1 <=t_i <= 1,000)通过. 没有两条牛路连接同样的牛棚,所有牛路满足a_i!=b_i.在所有数据中,牛_i使用的牛棚_1到牛 棚_i的最短路经是唯一的.
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and M
- Lines 2..M+1: Three space-separated integers: a_i, b_i, and t_i
输出格式:
- Lines 1..N-1: Line i contains the smallest time required to travel from pasture_1 to pasture_i+1 while avoiding the final cowpath of the shortest path from pasture_1 to pasture_i+1. If no such path exists from pasture_1 to pasture_i+1, output -1 alone on the line.
输入输出样例
4 5
1 2 2
1 3 2
3 4 4
3 2 1
2 4 3
3
3
6
说明
感谢 karlven 提供翻译。
正解最短路径生成树+树剖(并查集可水过~、、、)
先用Dijkstra计算dis 以及 最短路径的最后一条边,
对于这两点u,v,(无向边看做两条有向边)求出lca(u,v),,
则对于lca--v这条路径每个点,都可以由u到达,路径上的上的dis[x]'=dis[u]+dis[v]+edge[i].dis-dis[x](画图意会)
用树剖(并查集)维护最小值
#include <algorithm>
#include <cstdio>
#include <queue> using namespace std; const int INF(0x3f3f3f3f);
const int M(6e5+);
const int N(4e5+);
int n,m; int head[N],sumedge=;
struct Edge
{
int u,v,next,w;
Edge(int u=,int v=,int next=,int w=):
u(u),v(v),next(next),w(w){}
}edge[M<<];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(u,v,head[u],w);
head[u]=sumedge;
} struct Node
{
int id,dis;
bool operator < (const Node &x) const
{
return dis>x.dis;
} };
priority_queue<Node>que;
bool vis[N],mark[N];
int dis[N],pre[N];
inline void Dijkstra()
{ for(int i=;i<=n;i++) dis[i]=INF;
dis[]=;
que.push((Node){,});
for(;!que.empty();)
{
int u=que.top().id;
que.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
mark[pre[v]]=;
mark[i]=;
pre[v]=i;
que.push((Node){v,dis[v]});
}
}
}
} int size[N],deep[N],dad[N],top[N],son[N],cnt,id[N],dfn[N];
void DFS(int x,int fa,int deepth)
{
size[x]=; deep[x]=deepth; dad[x]=fa;
for(int i=head[x];i;i=edge[i].next)
if(mark[i])
{
int v=edge[i].v;
if(fa==v) continue;
DFS(v,x,deepth+);
size[x]+=size[v];
if(size[son[x]]<size[v]) son[x]=v;
}
}
void DFS_(int x,int Top)
{
id[x]=++cnt; dfn[cnt]=x;
top[x]=Top;
if(son[x]) DFS_(son[x],Top);
for(int i=head[x];i;i=edge[i].next)
if(mark[i])
{
int v=edge[i].v;
if(dad[x]!=v&&son[x]!=v) DFS_(v,v);
}
}
int LCA(int x,int y)
{
for(;top[x]!=top[y];x=dad[top[x]])
if(deep[top[x]]<deep[top[y]]) swap(x,y);
return deep[x]<deep[y]?x:y;
} struct Tree
{
int l,r,minn,flag;
}tr[N<<];
#define lc (now<<1)
#define rc (now<<1|1)
#define mid (tr[now].l+tr[now].r>>1)
inline void Tree_up(int now)
{
tr[now].minn=min(tr[lc].minn,tr[rc].minn);
}
void Tree_build(int now,int l,int r)
{
tr[now].l=l; tr[now].r=r;
tr[now].minn=INF;
tr[now].flag=INF;
if(l==r) return ;
Tree_build(lc,l,mid);
Tree_build(rc,mid+,r);
}
inline void Tree_down(int now)
{
if(tr[now].flag==INF) return ;
tr[lc].flag=min(tr[lc].flag,tr[now].flag);
tr[rc].flag=min(tr[rc].flag,tr[now].flag);
tr[lc].minn=min(tr[lc].minn,tr[lc].flag);
tr[rc].minn=min(tr[rc].minn,tr[rc].flag);
}
void Tree_change(int now,int l,int r,int x)
{
if(tr[now].l==l&&tr[now].r==r)
{
tr[now].minn=min(tr[now].minn,x);
tr[now].flag=min(tr[now].flag,x);
return ;
}
Tree_down(now);
if(r<=mid) Tree_change(lc,l,r,x);
else if(l>mid) Tree_change(rc,l,r,x);
else Tree_change(lc,l,mid,x),Tree_change(rc,mid+,r,x);
Tree_up(now);
}
int Tree_query(int now,int to)
{
if(tr[now].l==tr[now].r) return tr[now].minn;
Tree_down(now);
if(to<=mid) return Tree_query(lc,to);
else return Tree_query(rc,to);
}
inline void List_change(int u,int v,int w)
{
int fx=top[u] ;
while(deep[fx]>deep[v])
{
Tree_change(,id[top[u]],id[u],w);
u=dad[fx],fx=top[u];
}
if(u!=v) Tree_change(,id[v]+,id[u],w);
} int main()
{
freopen("travel.in","r",stdin);
freopen("travel.out","w",stdout); scanf("%d%d",&n,&m);
for(int a,b,t,i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&t);
ins(a,b,t); ins(b,a,t);
}
Dijkstra();
DFS(,,);
DFS_(,);
Tree_build(,,cnt);
for(int lca,u,v,i=;i<=sumedge;i+=)
{
u=edge[i].u,v=edge[i].v,lca=LCA(u,v);
if(!mark[i]) List_change(v,lca,dis[u]+dis[v]+edge[i].w);
if(!mark[i^]) List_change(u,lca,dis[u]+dis[v]+edge[i].w);
}
for(int i=;i<=n;i++)
{
int tmp=Tree_query(,id[i]);
if(tmp==INF) puts("-1");
else printf("%d\n",tmp-dis[i]);
}
return ;
}
唉、
洛谷—— P2934 [USACO09JAN]安全出行Safe Travel || COGS ——279|| BZOJ——1576的更多相关文章
- ●洛谷P2934 [USACO09JAN]安全出行Safe Travel
题链: https://www.luogu.org/problemnew/show/P2934 题解: 最短路(树),可并堆(左偏堆),并查集. 个人感觉很好的一个题. 由于题目已经明确说明:从1点到 ...
- luogu P2934 [USACO09JAN]安全出行Safe Travel
题目链接 luogu P2934 [USACO09JAN]安全出行Safe Travel 题解 对于不在最短路树上的边(x, y) 1 | | t / \ / \ x-----y 考虑这样一种形态的图 ...
- P2934 [USACO09JAN]安全出行Safe Travel
P2934 [USACO09JAN]安全出行Safe Travel https://www.luogu.org/problemnew/show/P2934 分析: 建出最短路树,然后考虑一条非树边u, ...
- [USACO09JAN]安全出行Safe Travel 最短路,并查集
题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each ...
- 「BZOJ1576」[Usaco2009 Jan] 安全路经Travel------------------------P2934 [USACO09JAN]安全出行Safe Travel
原题地址 题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as ...
- [USACO09JAN]安全出行Safe Travel
题目 什么神仙题啊,我怎么只会\(dsu\)啊 我们考虑一个非常暴力的操作,我们利用\(dsu\ on \ tree\)把一棵子树内部的非树边都搞出来,用一个堆来存储 我们从堆顶开始暴力所有的边,如果 ...
- 洛谷——P2935 [USACO09JAN]最好的地方Best Spot
P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that ...
- 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...
- 洛谷 P2935 [USACO09JAN]最好的地方Best Spot
题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 ...
随机推荐
- Pyinstaller 1 使用PyInstaller
使用PyInstaller pyinstaller命令的语法是: pyinstaller[ options ] script [ script ...] | spec文件 在最简单的情况下,将当前目录 ...
- Generating SSH Keys for github
由于最近电脑重装了Windows 8.1, 想用github维护一些代码.故不得不重新生成一下ssh key. 按https://help.github.com/articles/generating ...
- 压缩图片C#算法
转载自 http://www.open-open.com/lib/view/open1391348644910.html using System.IO; using System.Drawing; ...
- poj1170 - 转换成背包
题目链接 有5种物品,给出每个物品的单价. 给出几个这些物品的组合和这个组合的价格.买组合要比一件件的买便宜. 问给定的购买计划最少花多少钱. ---------------------------- ...
- C语言-实现字符串倒序输出
方法1: Action(){//倒序输出 char *src="abcdefgh123"; char *desc; desc=(char *)malloc(100*sizeof(c ...
- php 基础------数组过滤
array_filter();过滤数组 语法: array_filter(array,callbackfunction); array 必写,规定要过滤的数组 callbackfunction 必写, ...
- 登录生成令牌token存于redis
package com.medic.rest.province.base.home; import java.util.HashMap;import java.util.List;import jav ...
- Remember the Word UVALive - 3942 DP_字典树
每个小单词的长度都是小于等于100的,这是个重要的突破口. Code: #include <cstdio> #include <algorithm> #include < ...
- Fastlane基础介绍
Fastlane是什么 Git地址: Fastlane 文档地址:Fastlane Document Fastlane是一整套的客户端CICD工具集合.Fastlane可以非常快速简单的搭建一个自动化 ...
- 压缩和还原压缩的JS代码
压缩JS代码:packer – 最好用的 javascript 压缩工具地址: http://dean.edwards.name/packer/ http://kan.willin.org/?page ...