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& ...
随机推荐
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 电子商务B2B的发展趋势
B2B的发展趋势 尽管B2B市场发展势头良好,但B2B市场还是存在发育不成熟的一面.这种不成熟表现在B2B交易的许多先天性交易优势,比如在线价格协商和在线协作等还没有充分发挥出来. 报告指出,到200 ...
- C之输入输出函数(2) -- gets()
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rtref/gets.htm#gets #include <stdio.h ...
- Frost & Sullivan权威报告:阿里云再次领跑云WAF大中华区市场
近日,国际权威分析机构Frost & Sullivan 针对Web应用防火墙(简称“WAF”)领域发布了<2017年亚太区Web应用防火墙市场报告>,阿里云以市场占有率45.8%的 ...
- python bs4解析网页时 bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to inst(转)
Python小白,学习时候用到bs4解析网站,报错 bs4.FeatureNotFound: Couldn't find a tree builder with the features you re ...
- Ext 面板(Panel)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux知识总结(更新中)
Linux知识总结(更新中) 如何查找特定的文件 find find path [options] params 作用:在指定目录下查找文件 检索文件内容 grep grep [options] pa ...
- [资料]C#操作SQL Server数据库
1.概述 ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据库: 第二,建立SqlCommand对象,负责SQL语句的执行和存储过程的调 ...
- 剑指offer第二版面试题4:替换空格(JAVA版)
题目:请实现一个函数,把字符串中的每个空格替换成“%20”.例如输入“We are happy”,则输出”We%20are%20happy”. 原因:在网络编程中,如果URL参数中含有特殊字符,如:空 ...
- 网络编程(四)——基于udp协议的套接字socket、socketserver模块的使用
基于udp协议的套接字.socketserver模块 一.UDP协议(数据报协议) 1.何为udp协议 不可靠传输,”报头”部分一共只有8个字节,总长度不超过65,535字节,正好放进一个IP数据包. ...