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.

输入输出样例

输入样例#1:

4 5
1 2 2
1 3 2
3 4 4
3 2 1
2 4 3
输出样例#1:

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的更多相关文章

  1. ●洛谷P2934 [USACO09JAN]安全出行Safe Travel

    题链: https://www.luogu.org/problemnew/show/P2934 题解: 最短路(树),可并堆(左偏堆),并查集. 个人感觉很好的一个题. 由于题目已经明确说明:从1点到 ...

  2. luogu P2934 [USACO09JAN]安全出行Safe Travel

    题目链接 luogu P2934 [USACO09JAN]安全出行Safe Travel 题解 对于不在最短路树上的边(x, y) 1 | | t / \ / \ x-----y 考虑这样一种形态的图 ...

  3. P2934 [USACO09JAN]安全出行Safe Travel

    P2934 [USACO09JAN]安全出行Safe Travel https://www.luogu.org/problemnew/show/P2934 分析: 建出最短路树,然后考虑一条非树边u, ...

  4. [USACO09JAN]安全出行Safe Travel 最短路,并查集

    题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each ...

  5. 「BZOJ1576」[Usaco2009 Jan] 安全路经Travel------------------------P2934 [USACO09JAN]安全出行Safe Travel

    原题地址 题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as ...

  6. [USACO09JAN]安全出行Safe Travel

    题目 什么神仙题啊,我怎么只会\(dsu\)啊 我们考虑一个非常暴力的操作,我们利用\(dsu\ on \ tree\)把一棵子树内部的非树边都搞出来,用一个堆来存储 我们从堆顶开始暴力所有的边,如果 ...

  7. 洛谷——P2935 [USACO09JAN]最好的地方Best Spot

    P2935 [USACO09JAN]最好的地方Best Spot 题目描述 Bessie, always wishing to optimize her life, has realized that ...

  8. 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)

    P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...

  9. 洛谷 P2935 [USACO09JAN]最好的地方Best Spot

    题目描述 Bessie, always wishing to optimize her life, has realized that she really enjoys visiting F (1 ...

随机推荐

  1. MySQL数据库学习记录

    SELECT子句顺序

  2. 基本数据类型(int,bool,str)

    1.int bit_lenth() 计算整数在内存中占用的二进制码的长度 十进制 二进制 长度(bit_lenth()) 1 1 1 2 10 2 4 100 3 8 1000 4 16 10000 ...

  3. ES 新增字符串方法

    话不多说,直接开鲁 1. startsWith() 作用: 检测字符串以什么开头 实例: let str = "www.qjzzj.top"; console.log(str.st ...

  4. LightOJ-1236 Pairs Forming LCM 唯一分解定理

    题目链接:https://cn.vjudge.net/problem/LightOJ-1236 题意 给一整数n,求有多少对a和b(a<=b),使lcm(a, b)=n 注意数据范围n<= ...

  5. Linux 重启防火墙失败

    CentOS 7 执行service iptables start出现redirecting to systemctl ...Failed to ...not loaded. 如果出现以下错误,好像说 ...

  6. Linux ping 不通 域名 添加DNS

    修改路由配置文件 vi /etc/resolv.conf # Generated by NetworkManager #NDS nameserver 192.168.32.2 redhat7 系统优化 ...

  7. 【CS Round 34】Max Or Subarray

    [题目链接]:https://csacademy.com/contest/round-34/summary/ [题意] 让你找一个最短的连续子串; 使得这个子串里面所有数字or起来最大; [题解] 对 ...

  8. Linux的中断和系统调用 & esp、eip等寄存器

    http://www.linuxidc.com/Linux/2012-11/74486.htm 一共三篇 中断一般分为三类: 1.由计算机硬件异常或故障引起的中断,称为内部异常中断: 2.由程序中执行 ...

  9. hdoj--1312--Red and Black(dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  10. hpuoj--校赛--特殊的比赛日期(素数判断+模拟)

    问题 B: 感恩节KK专场--特殊的比赛日期 时间限制: 1 Sec  内存限制: 128 MB 提交: 392  解决: 99 [提交][状态][讨论版] 题目描述 KK今天参加河南理工大学ACM程 ...