「BZOJ1576」[Usaco2009 Jan] 安全路经Travel------------------------P2934 [USACO09JAN]安全出行Safe Travel
题目描述
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
打题的艰辛历程
- 这题,只要是个想我一样这么弱的人第一反应就是次短路,把断的边给连上跑一次次短路就行了,然后隔壁大佬给了一组数据,他有可能次短路也要走这条边,然后这个思路就死了。
- 过了一会又瞎YY出来了一个算法,找出终点的父节点,然后从这个父节点开始跑最短路再加上根节点到父节点的距离就可以了,就这个算法一开始还没看出来是错的,还瞎搞了一个什么算法,还一本正经的试了样例,发现错了,然后才发现他可以从父节点之前就绕道走。。。。
- 最后去看了题解,又瞎搞了什么最短路树,调了一上午,发现有一个字母打错了。。。。
- 最后终于过了
- 总结这题就是折磨人的。。。
思路:
- 这题就是一个最短路树,最短路树,就是一棵树,根节点到每个节点的长度就是原图上起点到这个点的最短路(就是1到每个节点的最短路径路过的边所构成的集合)
- 我们要找一个点对(x,y)x,y之间有连边,但不在最短路树上,y在节点i的子树中,而x则不在,x同样也不是i,y的祖先,dis是最短路径,w是路径长度。
- 所以我们要找的节点路径就是dis(x)+dis(y)-dis(i)+w(x,y);这个东西比较抽象,可以动手画图看一下。
- 由于dis(i)是一定的,所以我们就要维护dis(x)+dis(y)+w(x,y)的最小值,因为太懒,就直接排个序算了,慢也慢不到哪去,STL里的sort好像有什么搞不懂的优化,反正排序用他就可以了。
- 这样我们就选出了不在树上的边,排下序。更新好答案。
- 在一个点被更新之后,那这个点的答案就一定最优,所以对上面那一堆式子,第一次更新就一定会是他的最优解。
- 然后就可以用冰茶姬去维护每一个只搞了一次。
- 然后又可以发现每一次更新都更新了x->lca(x,y)和y->lca(x,y)的所有点。因为如果更新到了LCA(x,y),那x,y就在同一棵子树里了,而我们可以更新的前提就是X,Y不在一棵子树中,题目说了x-y不会是i的父边,所以就一个一个像LCA那样去跳,一边跳一遍更新,知道LCA,然后改一下冰茶姬,最后完美结束。
最后放一下代码,千万别用SPFA,会T掉很多。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
const int maxm = ; inline int read()
{
int res = ;
bool flag = ;
char c = getchar();
while (c < '' or c > '')
{
if (c == '-') flag = ;
c = getchar();
}
while (c >= '' and c <= '')
{
res = res * + c - '';
c = getchar();
}
return flag ? -res : res;
} int n, m;
int a [maxm], b[maxm], c[maxm];
int what[maxm];
int ins[maxm];
bool iiis[maxm];
int father[maxn];
int fa[maxn];
int ans[maxn]; int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
} struct edge
{
int next;
int to;
int val;
}way[maxm]; int head[maxn], tot;
inline void add (int x , int y , int z , int GG)
{
way[++tot].next = head[x];
way[tot].to = y;
way[tot].val = z;
what[tot] = GG;
head[x] = tot;
}
inline void add (int x , int y)
{
way[++tot].next = head[x];
way[tot].to=y;
head[x]=tot;
}
struct date
{
int x;
int y;
int w;
}usaco[maxm]; bool cmp(date a, date b)
{
return a.w < b.w;
} int dfs(int x)
{
for(int i=head[x];i;i=way[i].next)
{
int to=way[i].to;
if(to==father[x])
{
continue;
}
father[to]=x;
dfs(to);
}
} struct dij{int x, w;}; bool operator <(const dij &a,const dij &b)
{
return a.w > b.w;
} int dis[maxn];
bool vis[maxn]; int dijkstra(int qa)
{
memset(dis,0x3f,sizeof(dis));
priority_queue < dij > q;
dis[qa]=;
q.push((dij){qa,});
while(!q.empty())
{
dij t=q.top();
q.pop();
int x=t.x;
if(vis[x])
{
continue;
}
vis[x]=;
for(int i=head[x];i;i=way[i].next)
{
int to=way[i].to;
if(!vis[to]&&dis[to]>dis[x]+way[i].val)
{
ins[to]=what[i];
dis[to]=dis[x]+way[i].val;
q.push((dij){to,dis[to]});
}
}
}
}
int main()
{
//freopen("testdata.in","r",stdin);
n=read();
m=read();
for(int i=;i<=m;i++)
{
a[i]=read();
b[i]=read();
c[i]=read();
add(a[i],b[i],c[i],i);
add(b[i],a[i],c[i],i);
}
dijkstra();
tot=;
memset(head,,sizeof(head));
for(int i=;i<=n;i++)
{
int x=ins[i];
add(a[x],b[x]);
add(b[x],a[x]);
iiis[x]=;
}
dfs();
int sum=;
for(int i=;i<=m;i++)
{
if(iiis[i])
{
continue;
}
usaco[++sum]=(date){a[i],b[i],dis[a[i]]+dis[b[i]]+c[i]};
}
sort(usaco+,usaco++sum,cmp);
for(int i=;i<=n;i++)
{
fa[i]=i;
ans[i]=-;
}
for(int i=;i<=sum;i++)
{
int x=usaco[i].x;
int y=usaco[i].y;
x=find(x);
y=find(y);
while(x!=y)
{
if(dis[x]<dis[y])
{
swap(x,y);
}
ans[x]=usaco[i].w-dis[x];
fa[x]=father[x];
x=find(x);
}
}
for(int i=;i<=n;i++)
{
printf("%d\n",ans[i]);
// cout<<ans[i]<<endl;
}
return ;
}
「BZOJ1576」[Usaco2009 Jan] 安全路经Travel------------------------P2934 [USACO09JAN]安全出行Safe Travel的更多相关文章
- 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, ...
- 洛谷—— P2934 [USACO09JAN]安全出行Safe Travel || COGS ——279|| BZOJ——1576
https://www.luogu.org/problem/show?pid=2934 题目描述 Gremlins have infested the farm. These nasty, ugly ...
- ●洛谷P2934 [USACO09JAN]安全出行Safe Travel
题链: https://www.luogu.org/problemnew/show/P2934 题解: 最短路(树),可并堆(左偏堆),并查集. 个人感觉很好的一个题. 由于题目已经明确说明:从1点到 ...
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...
- [USACO09JAN]安全出行Safe Travel 最短路,并查集
题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each ...
- [USACO09JAN]安全出行Safe Travel
题目 什么神仙题啊,我怎么只会\(dsu\)啊 我们考虑一个非常暴力的操作,我们利用\(dsu\ on \ tree\)把一棵子树内部的非树边都搞出来,用一个堆来存储 我们从堆顶开始暴力所有的边,如果 ...
- 【思维题 并查集 图论】bzoj1576: [Usaco2009 Jan]安全路经Travel
有趣的思考题 Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第 ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MB Submit: 665 Solved: 227[Sub ...
随机推荐
- [go设计模式]简单工厂模式
优点 工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体类的对象.通过使用工厂类,外界可以从直接创建具体产品对象的尴尬局面摆脱出来,仅仅需要负责“消费”对象就可 ...
- springboot结合jpa
idea中新建springboot项目,引入spring-boot-starter-data-jpa依赖 application.yml中配置数据库连接,示例如下: spring: datasourc ...
- HTML5 + WebGL 实现的垃圾分类系统
前言 垃圾分类,一般是指按一定规定或标准将垃圾分类储存.分类投放和分类搬运,从而转变成公共资源的一系列活动的总称.分类的目的是提高垃圾的资源价值和经济价值,力争物尽其用.垃圾在分类储存阶段属于公众的私 ...
- postman全局变量设置
1.点击小齿轮进入到变量添加页面,点击Globals添加全局变量 2.输入变量名称和变量值 3.接口中设置变量
- 内网转发之reGeorg+proxifier
先将reGeorg的对应脚本上传到服务器端,reGeorg提供了PHP.ASPX.JSP脚本,直接访问显示“Georg says, 'All seems fine'”,表示脚本运行正常. 运行 pyt ...
- Head First设计模式——装饰者模式
前言:对于设计模式我们有时候在想是否有必要,因为实际开发中我们没有那么多闲工夫去套用这么多设计模式,也没有必要为了模式而模式. 通常这些模式会引入新的抽象层,增加代码的复杂度,但是当我们掌握了这些设计 ...
- python深拷贝与浅拷贝的区别
可变对象:一个对象在不改变其所指向的地址的前提下,可以修改其所指向的地址中的值 不可变对象:一个对象所指向的地址上值是不能修改的,如果你修改了这个对象的值,那么它指向的地址就改变了,相当于你把这个对象 ...
- django2-创建项目
方式一:cmd或linux命令行下创建django项目(不常用,此处不做详细介绍) django-admin.py startproject autotest 方式二:使用pycharm专业版创建dj ...
- 玩转OneNET物联网平台之HTTP服务③ —— OneNet智能灯 HTTP版本
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- 07 python学习笔记-写一个清理日志的小程序(七)
#删掉三天前的日志 #1.获取到所有的日志文件, os.walk #2.获取文件时间 android 2019-09-27 log,并转成时间戳 #3.获取3天前的时间 time.time() - 6 ...