(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦

Catalog

Problem:传送门

Portal

 原题目描述在最下面。

 \(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 最短路的更多相关文章

  1. AtCoder arc061C Snuke's Subway Trip

    大意: 给你一张无向图,边有种类. 当你第一次/重新进入某种边时费用 + 1 在同一种边之间行走无费用. 求 1 到 n 的最小费用. 嗯...乍一看有一个很直观的想法:记录每个点的最短路的上一条边的 ...

  2. ARC061E Snuke's Subway Trip

    传送门 题目大意 已知某城市的地铁网由一些地铁线路构成,每一条地铁线路由某一个公司运营,该城市规定:若乘坐同一公司的地铁,从开始到换乘只需要一块钱,换乘其他公司的价格也是一块钱,问从1号地铁站到n号地 ...

  3. 【例题收藏】◇例题·I◇ Snuke's Subway Trip

    ◇例题·I◇ Snuke's Subway Trip 题目来源:Atcoder Regular 061 E题(beta版) +传送门+ 一.解析 (1)最短路实现 由于在同一家公司的铁路上移动是不花费 ...

  4. Snuke's Subway Trip

    すぬけ君の地下鉄旅行 / Snuke's Subway Trip Time limit : 3sec / Memory limit : 256MB Score : 600 points Problem ...

  5. 2018.09.19 atcoder Snuke's Subway Trip(最短路)

    传送门 就是一个另类最短路啊. 利用颜色判断当前节点的最小花费的前驱边中有没有跟当前的边颜色相同的. 如果有这条边费用为0,否则费用为1. 这样跑出来就能ac了. 代码: #include<bi ...

  6. [ARC061E]すぬけ君の地下鉄旅行 / Snuke's Subway Trip

    题目大意:Snuke的城镇有地铁行驶,地铁线路图包括$N$个站点和$M$个地铁线.站点被从$1$到$N$的整数所标记,每条线路被一个公司所拥有,并且每个公司用彼此不同的整数来表示. 第$i$条线路($ ...

  7. POJ 2502 Subway (最短路)

    Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...

  8. POJ2502:Subway(最短路)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14634   Accepted: 4718 题目链接:http ...

  9. atcoder C - Snuke and Spells(模拟+思维)

    题目链接:http://agc017.contest.atcoder.jp/tasks/agc017_c 题解:就是简单的模拟一下就行.看一下代码就能理解 #include <iostream& ...

随机推荐

  1. MySQL - 两种存储引擎 (MyISAM PK InnoDB) + MVCC

    总结 1.两种存储引擎各有各的有点,MyISAM专注性能,InnoDB专注事务.两者最大的区别就是InnoDB支持事务,和行锁. 2.InnoDB采用MVCC(Multi-Version Concur ...

  2. table响应式设计

    table不可用flex布局和td宽度的自适应. table外层加div.mml-table设置overflow-x:auto可以添加横向滚动条.

  3. Delphi 方法:overload、override、virtual、dynamic、abstract

    1.overload 在Pascal语法规则中,同一个UNIT里是不能存在两个同名的函数的,例如: function func(): Boolean; function func(const x: C ...

  4. CF1016F 【Road Projects】

    思路 可以考虑另一种想法:因为我们发现,答案是肯定不会大于在原来的树上的最短路径的.所以原来的最短路是(有可能的)最大值! 我们把树变成这样,提取出1~n的路径,方便观看撕烤: (它有个我起的名字,叫 ...

  5. C#调用Mail发送QQ邮件

    需要用到: 1.System.Net.Mail; 2.QQ邮箱的POP3/SMTP服务码 QQ邮箱的POP3/SMTP服务码获取方法: 1.打开qq邮箱: 2.进入设置页面-->账户:(往下翻) ...

  6. date和string转换用joda包

    import org.joda.time.DateTime;import org.joda.time.format.DateTimeFormat;import org.joda.time.format ...

  7. C++——指针与引用

    1.指针本身为对象,引用只是对象的别名.故有指针的引用,没有引用的引用,没有引用的指针.指针必须指向一个实际的对象.引用也必须是实际对象的别名. 2.允许指针赋值和拷贝,指针可指向不同的对象 3.指针 ...

  8. 2019牛客多校第三场F-Planting Trees(单调队列)

    Planting Trees 题目传送门 解题思路 枚举每一个下边界,再枚举其对应的所有上边界,求出其对应区间内的最大最小值,当下边界一样的时候,其最大最小值可以随着上边界减小逐步更新.然后将这些最大 ...

  9. shell 检查文件夹是否包含文件,或者只是空文件

    empty_dir_check(){ check_dir=$ if [ -d $check_dir ];then file_list=` -maxdepth -type f` if [ $file_l ...

  10. 剑指offer——66翻转字符串

    题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“student ...