洛谷—— 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 ...
随机推荐
- N!,斯特林近似
题目链接 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 1000) 第2 - ...
- win10关闭更新
计算机--管理: 找到windows update 服务关闭:
- mongodb报错:connection refused because too many open connections: 819
问题: 发现mongodb无法连接,查看mongodb日志,出现大量的如下报错: [initandlisten] connection refused because too many open co ...
- 转载:跟我一起写 Makefile
陈皓(http://my.csdn.net/haoel) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得 ...
- jQuery 完整 ajax示例
$(function(){ //请求参数 var list = {}; // $.ajax({ //请求方式 type : "POST", //请求的媒体类型 contentTyp ...
- 关于python return 和 print 的区别
概念上一个是 返回值 一个是打印输出 区别一:return是结束语一般放在函数的最后,当你在return 结束后面再写一些东西是不执行的如 下 def renshu(x,y): h=x+y pri ...
- c++PrimerChap7类
仅仅记录贴,按书上的做完了一边,想把private分离出来已经很难了.因为is用到的成员变量都是直接当做public使用的,如果要改的话可以考虑存储输入,让后用构造函数对类进行初始化. #includ ...
- 虚拟机VM安装Linux系统CentOS7
第一步:安装一个VM虚拟机: 百度VM,使用普通下载,一路Next即可 如果需要输入序列号,可以网上随意找一个,目前是个人可以随意激活,但如果做商业用途的话,还是最好买一个序列号,我在网上搜到的:5A ...
- 集团公司(嵌入ETL工具)財务报表系统解决方式
集团公司(嵌入ETL工具)財务报表系统解决方式 一.项目背景: 某集团公司是一家拥有100多家子公司的大型集团公司,旗下子公司涉及各行各业,包含:金矿.铜矿.房产.化纤等.因为子公司在业务上的差异.子 ...
- hadoop-03-安装java
hadoop-03-安装java 1,su root 2, rpm -qa|grep jdk #查看已经安装的jdk 3,rpm -e --nodeps `rpm -qa|grep jdk ` #删除 ...