大意:

给你一张无向图,边有种类。

当你第一次/重新进入某种边时费用 + 1

在同一种边之间行走无费用。

求 1 到 n 的最小费用。

嗯...乍一看有一个很直观的想法:记录每个点的最短路的上一条边的种类。

但是这个算法是个错的......有些数据能够hack掉。

由于边权只有0 & 1,考虑01BFS。

事实上我们还要记录每个点来之前的边,然后就要写结构体/pair

然后还要判重...

正解:考虑按照每个点的边的type拆点。
在不同点的同一type分点之间边权为0
原点与拆出来的点之间边权为1
然后跑01BFS即可。

开n个map来存点的分点编号。
map 的 count 用法(虽然没啥卵用)

我照着正解写了个过了,然后写了自己的想法就WA.....

不知道为什么。对拍可能生成的数据太弱了,全是符合的。

 #include <cstdio>
#include <map>
#include <deque> const int N = ; int cnt; std::map<int, int> mp[N]; struct Edge {
int v, nex, len;
}edge[N << ]; int top; int e[N], dis[N], vis[N]; inline int get(int x, int type, int &f) {
if(mp[x][type]) {
f = ;
return mp[x][type];
}
f = ;
return mp[x][type] = ++cnt;
} inline void add(int x, int y, int z) {
++top;
edge[top].v = y;
edge[top].len = z;
edge[top].nex = e[x];
e[x] = top++;
edge[top].v = x;
edge[top].len = z;
edge[top].nex = e[y];
e[y] = top;
return;
} inline int BFS(int s, int t) {
vis[s] = ;
dis[s] = ;
std::deque<int> Q;
Q.push_back(s);
while(!Q.empty()) {
int x = Q.front();
Q.pop_front();
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(vis[y]) {
continue;
}
dis[y] = dis[x] + edge[i].len;
if(y == t) { /// error : y == s
return dis[y];
}
vis[y] = ;
if(edge[i].len) {
Q.push_back(y);
}
else {
Q.push_front(y);
}
}
}
return -;
} int main() {
freopen("in.in", "r", stdin);
freopen("my.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
cnt = n;
for(int i = , x, y, z, f; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
int x_type = get(x, z, f);
if(f) {
add(x, x_type, );
}
int y_type = get(y, z, f);
if(f) {
add(y, y_type, );
}
add(x_type, y_type, );
} int ans = BFS(, n);
printf("%d", ans >> ); return ;
}

AC代码

 #include <cstdio>
#include <map>
#include <deque> const int N = ; std::map<int, int> mp[N]; struct Edge {
int v, nex, type;
}edge[N << ]; int top; int e[N]; inline void add(int x, int y, int z) {
top++;
edge[top].v = y;
edge[top].type = z;
edge[top].nex = e[x];
e[x] = top;
return;
} struct Poi {
int id, dis, pre;
Poi(int id, int dis, int pre) {
this->id = id;
this->dis = dis;
this->pre = pre;
}
}; inline int BFS(int s, int t) {
std::deque<Poi> Q;
Q.push_back(Poi(s, , ));
while(!Q.empty()) {
Poi x = Q.front();
Q.pop_front();
for(int i = e[x.id]; i; i = edge[i].nex) {
int y = edge[i].v;
int len = edge[i].type == x.pre ? : ;
if(y == t) {
return x.dis + len;
}
if(mp[y][edge[i].type]) {
continue;
}
mp[y][edge[i].type] = ;
if(len) {
Q.push_back(Poi(y, x.dis + , edge[i].type));
}
else {
Q.push_front(Poi(y, x.dis, x.pre));
}
}
}
return -;
} int main() {
int m, n;
scanf("%d%d", &n, &m);
for(int i = , x, y, z; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
add(y, x, z);
} int t = BFS(, n);
printf("%d", t); return ;
}

WA代码

AtCoder arc061C Snuke's Subway Trip的更多相关文章

  1. AtCoder ARC061E Snuke's Subway Trip 最短路

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门  Portal  原题目描述在最下面.  \(n(1 ...

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

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

  3. Snuke's Subway Trip

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

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

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

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

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

  6. ARC061E Snuke's Subway Trip

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

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

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

  8. AtCoder AGC017C Snuke and Spells

    题目链接 https://atcoder.jp/contests/agc017/tasks/agc017_c 题解 很久前不会做看了题解,现在又看了一下,只想说,这种智商题真的杀我... 转化成如果现 ...

  9. AtCoder 杂题训练

    前言: 因为要普及了,今年没一等就可以退役去学文化课了,所以暑假把历年noip普及组都刷了一遍,离noip还有50+天,想弄点强化训练什么的. 想了想,就这些天学文化课之余有空就把AtCoder之前那 ...

随机推荐

  1. bootstrap模态框关闭后清除模态框的数据

    https://segmentfault.com/q/1010000008789123 bootstrap模态框第二次打开时如何清除之前的数据? 我用了bootstrap模态框的remote功能,在弹 ...

  2. .Net在操作mysql查询的时候出现“: Unknown column 'UserName' in 'where clause'”错误

    今天使用.Net操作mysql查询的时候,如果加上条件查询的时候就会出现 Unknown column 'UserName' in 'where clause'这个错,不加条件直接select * f ...

  3. Spring-Boot Banner

    下载Spring-Boot源码,目录结构spring-boot-2.1.0.M2\spring-boot-2.1.0.M2\spring-boot-project\spring-boot\src\ma ...

  4. 使用PHP进行SOCKET编程

    一.SOCKET原理图 二.SOCKET常用函数 1.创建socket函数: resource socket_create ( int $domain , int $type , int $proto ...

  5. linux 安装python 和pip

    下载文件 python官网:https://www.python.org/downloads/ 百度网盘http://pan.baidu.com/s/1mixGB12     密码   9nzu [r ...

  6. SpringMVC中对多部件类型解析---文件(图片)上传

    加入上传图片jar包 commons-io-2.4.jar commons-fileupload-1.3.jar 在页面form中提交enctype="multipart/form-data ...

  7. How to RAMDISK on macOS

    diskutil erasevolume HFS+ 'RAM Disk' `hdiutil attach -nomount ram://8388608`

  8. 学习 Spring (十六) AOP API

    Spring入门篇 学习笔记 Spring AOP API 是 Spring 1.2 历史用法,现在仍然支持 这是 Spring AOP 基础,现在的用法也是基于历史的,只是更简便了 Pointcut ...

  9. linux利用CMakeLists编译cuda程序

    文件目录: cudaTest |--utils.cu |--utils.h |--squaresum.cu |--squaresum.h |--test.cpp |--CMakeLists.txt 编 ...

  10. Idea中JavaWeb项目部署

    1. 添加应用服务器tomcat 2. 将tomcat配置添加到项目中 artifacts配置:添加deploy, 添加artifacts,选择Web Application: Exploded &g ...