AtCoder ARC061E Snuke's Subway Trip 最短路
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
Problem:传送门
原题目描述在最下面。
\(n(1e5)\)个点, \(m(2e5)\)条边, 每条边有一个属性值。经过一条同一属性值的连续路径花费为1。问从1到n的最小花费。
Solution:
我的解法
直接边最短路搞,然后t的飞起。仔细一想,这样写的话有\(2e5\)个点,边数更是多到飞起,拿命跑啊,不过代码我还是放下面。
正解:拆点
把一条\(u->v\)属性为\(c\)的路径,拆成\(u->uc, uc->vc, vc->v\)三条路径,边权分别为\(1, 0, 1\)。
然后跑最裸的最短路就行,答案除\(2\)输出。
why?
为什么这样是对的呢?
对于一个点连接的许多路径,从一条走向另一条,如果属性相同就不需要额外花费。这点怎么做到的呢?
比如\(x->y,y->z\)属性均为\(c\):实际路径是\(x->yc->z\),经过了yc这个中间点,而且没有额外的花费。
答案除\(2\)是因为出发和结束都算了一遍花费。
AC_Code:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define fi first
#define se second
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MXN = 1e6 + 5;
const int MXT = 2e7 + 6;
const uLL base = 99959;
unordered_map<uLL, int> mp;
int n, m, tn;
int head[MXN], tot;
struct lp {
int v, c, nex;
}cw[MXT];
int dis[MXT], vis[MXT], fa[MXN];
void add_edge(int u,int v,int w) {
cw[++tot].v = v;cw[tot].c = w;cw[tot].nex = head[u];
head[u] = tot;
cw[++tot].v = u;cw[tot].c = w;cw[tot].nex = head[v];
head[v] = tot;
}
void dij() {
priority_queue<pii,vector<pii>,greater<pii> >Q;
for(int i = 1; i <= n; ++i) dis[i] = INF, vis[i] = 0;
dis[1] = 0;
Q.push({dis[1], 1});
while(!Q.empty()) {
pii now = Q.top();Q.pop();
int u = now.se;
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; ~i; i = cw[i].nex) {
int v = cw[i].v;
//if(vis[v]) continue;
if(dis[v] > dis[u] + cw[i].c) {
dis[v] = dis[u] + cw[i].c;
Q.push({dis[v], v});
}
}
}
int ans = dis[tn];
while(!Q.empty()) Q.pop();
if(ans == INF) ans = -2;
printf("%d\n", ans/2);
}
int Fi(int x) {
return fa[x] == x? x: fa[x] = Fi(fa[x]);
}
int get(int x, int y) {
uLL tmp = x;
tmp = tmp * base * base + y * base + x ^ y;
if(mp[tmp]) return mp[tmp];
mp[tmp] = ++n;
return n;
}
int main(int argc, char const *argv[]) {
while(~scanf("%d%d", &n, &m)) {
memset(head, -1, sizeof(head));
tot = -1;
mp.clear();
tn = n;
for(int i = 1; i <= n; ++i) fa[i] = i;
for(int i = 0, u, v, c, pa, pb, uc, vc; i < m; ++i) {
scanf("%d%d%d", &u, &v, &c);
pa = Fi(u), pb = Fi(v);
fa[pa] = pb;
uc = get(u, c); vc = get(v, c);
add_edge(u,uc,1);add_edge(uc,vc,0);add_edge(vc,v,1);
add_edge(v,vc,1);add_edge(vc,uc,0);add_edge(uc,u,1);
}
if(Fi(tn) != Fi(1)){
printf("-1\n");
continue;
}
dij();
}
return 0;
}
TLE_code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define fi first
#define se second
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MXN = 1e5 + 5;
const int MXT = 4e5 + 6;
int n, m;
int head[MXN], tot;
struct lp {
int v, c, nex;
}cw[MXT];
vector<pii> mp[MXN];
int dis[MXT], vis[MXT], fa[MXN];
void add_edge(int u,int v,int w) {
cw[++tot].v = v;cw[tot].c = w;cw[tot].nex = head[u];
head[u] = tot;
cw[++tot].v = u;cw[tot].c = w;cw[tot].nex = head[v];
head[v] = tot;
}
void dij() {
priority_queue<pii,vector<pii>,greater<pii> >Q;
memset(dis, 0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
for(int i = head[1]; ~i; i = cw[i].nex) {
dis[i] = 1;
Q.push({dis[i], i});
}
int ans = INF;
while(!Q.empty()) {
pii now = Q.top();Q.pop();
if(vis[now.se]) continue;
vis[now.se] = 1;
int u = now.se, a = cw[u].v;
if(a == n) {
ans = min(ans, dis[u]);
break;
}
for(int i = head[a]; ~i; i = cw[i].nex) {
if(vis[i]) continue;
if(dis[i]>dis[u]+(cw[i].c!=cw[u].c)) {
dis[i] = dis[u]+(cw[i].c!=cw[u].c);
Q.push({dis[i], i});
}
}
}
while(!Q.empty()) Q.pop();
if(ans == INF) ans = -1;
printf("%d\n", ans);
}
int Fi(int x) {
return fa[x] == x? x: fa[x] = Fi(fa[x]);
}
int main(int argc, char const *argv[]) {
while(~scanf("%d%d", &n, &m)) {
memset(head, -1, sizeof(head));
tot = -1;
for(int i = 1; i <= n; ++i) fa[i] = i;
for(int i = 0, u, v, c, pa, pb; i < m; ++i) {
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
pa = Fi(u), pb = Fi(v);
fa[pa] = pb;
}
if(Fi(n) != Fi(1)){
printf("-1\n");
continue;
}
dij();
}
return 0;
}
Problem Description:
AtCoder ARC061E Snuke's Subway Trip 最短路的更多相关文章
- AtCoder arc061C Snuke's Subway Trip
大意: 给你一张无向图,边有种类. 当你第一次/重新进入某种边时费用 + 1 在同一种边之间行走无费用. 求 1 到 n 的最小费用. 嗯...乍一看有一个很直观的想法:记录每个点的最短路的上一条边的 ...
- ARC061E Snuke's Subway Trip
传送门 题目大意 已知某城市的地铁网由一些地铁线路构成,每一条地铁线路由某一个公司运营,该城市规定:若乘坐同一公司的地铁,从开始到换乘只需要一块钱,换乘其他公司的价格也是一块钱,问从1号地铁站到n号地 ...
- 【例题收藏】◇例题·I◇ Snuke's Subway Trip
◇例题·I◇ Snuke's Subway Trip 题目来源:Atcoder Regular 061 E题(beta版) +传送门+ 一.解析 (1)最短路实现 由于在同一家公司的铁路上移动是不花费 ...
- Snuke's Subway Trip
すぬけ君の地下鉄旅行 / Snuke's Subway Trip Time limit : 3sec / Memory limit : 256MB Score : 600 points Problem ...
- 2018.09.19 atcoder Snuke's Subway Trip(最短路)
传送门 就是一个另类最短路啊. 利用颜色判断当前节点的最小花费的前驱边中有没有跟当前的边颜色相同的. 如果有这条边费用为0,否则费用为1. 这样跑出来就能ac了. 代码: #include<bi ...
- [ARC061E]すぬけ君の地下鉄旅行 / Snuke's Subway Trip
题目大意:Snuke的城镇有地铁行驶,地铁线路图包括$N$个站点和$M$个地铁线.站点被从$1$到$N$的整数所标记,每条线路被一个公司所拥有,并且每个公司用彼此不同的整数来表示. 第$i$条线路($ ...
- POJ 2502 Subway (最短路)
Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...
- POJ2502:Subway(最短路)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14634 Accepted: 4718 题目链接:http ...
- atcoder C - Snuke and Spells(模拟+思维)
题目链接:http://agc017.contest.atcoder.jp/tasks/agc017_c 题解:就是简单的模拟一下就行.看一下代码就能理解 #include <iostream& ...
随机推荐
- CSS3 object-position/object-fit
object-position和object-fit只针对替换元素有作用,也就是form表单家族控件系列,老牌劲旅img图片,HTML5新贵video视频等元素(一般有src属性的). 一.objec ...
- 【转】让应用程序支持emoji字符
什么是emoji?就是这些表情和符号:
- Python每日一题 002
做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...
- position:fixed 造成页面抖动解决办法
今天做项目遇到一个问题.鼠标滚动到指定长度后,理想状况是菜单吸附到顶部.但是实际上在一些页面上会造成抖动.定位之后发现.window.pageYoffset值会变成0 搜索一些资料后发现.因为fixe ...
- [CQOI2014]数三角形 题解(找规律乱搞)
题面 其实这道题不用组合数!不用容斥! 只需要一个gcd和无脑找规律(滑稽 乍一看题目,如果单纯求合法三角形的话情况太多太复杂,我们可以从局部入手,最终扩展到整体. 首先考虑这样的情况: 类似地,我们 ...
- Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class
ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶 ...
- This inspection detects shadowing names defined in outer scopes.
错误信息:This inspection detects shadowing names defined in outer scopes. 检查到波浪处的单词已在函数外部定义. 解决:使用global ...
- python使用threading获取线程函数返回值的实现方法
python使用threading获取线程函数返回值的实现方法 这篇文章主要介绍了python使用threading获取线程函数返回值的实现方法,需要的朋友可以参考下 threading用于提供线程相 ...
- Retrofit全攻略——进阶篇
最近事比较多,距离上次写文章已经过去了一个月了.上一篇文章Retrofit全攻略——基础篇 介绍了Retrofit的基础用法,这篇文章介绍点进阶的用法. 打印网络日志 在开发阶段,为了方便调试,我们需 ...
- Error: EBUSY: resource busy or locked, symlink "xxx" 的解决方法
上面是报错信息. 解决方法:当我们在npm i 安装依赖的时候,会遇到这个Error: EBUSY: resource busy or locked, symlink....的问题.解决的 办法就是关 ...