Problem Description

Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number.

The i-th ( 1≤iM ) line connects station pi and qi bidirectionally. There is no intermediate station. This line is operated by company ci.

You can change trains at a station where multiple lines are available.

The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again.

Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare.

Constraints

  • 2≤N≤105
  • 0≤M≤2×105
  • 1≤piN (1≤iM)
  • 1≤qiN (1≤iM)
  • 1≤ci≤106 (1≤iM)
  • piqi (1≤iM)

Input

The input is given from Standard Input in the following format:

N M
p1 q1 c1
:
pM qM cM

Output

Print the minimum required fare. If it is impossible to get to station N by subway, print -1 instead.

Example

Sample Input 1

3 3
1 2 1
2 3 1
3 1 2

Sample Output 1

1
Use company 1's lines: 1 → 2 → 3. The fare is 1 yen.

Sample Input 2

8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

Sample Output 2

2
First, use company 1's lines: 1 → 3 → 2 → 5 → 6. Then, use company 5's lines: 6 → 7 → 8. The fare is 2 yen.

Sample Input 3

2 0

Sample Output 3

-1

题意:n 个点 m 条边,每条边给出邻接点的同时给出一个价值,代表的是该边所属的公司,现在要从 1 号点到 n 号点,已知在同一个公司上行走时,无需付出任何代价,但当走到令一公司的边上时,需要花费 1 元,求最小花费

思路:

这个题的问题在于让相同公司的道路之间行走花费为 0,不同公司之间的道路行走花费为 1,于是整张图的每个公司可以看做一个虚点,其连接着其所属的边的两点

由于在同一个公司的路上行走时,不耗费任何价值,那么可以对同一公司的路上进行拆点建图,即对于所属公司为 val 的边 x-y,将其拆成 x-newX、newX-newY、newY-y 三条边,其价值分别为 1、0、1,然后对整个图跑 SPFA 求最短路即可

跑完 SPFA 时,由于在起点时需要一个初始代价,中间行走时不需要代价,离开时也需要一个代价,因此 dis[n]/2 就是答案

此外,重点在于如何拆点能保证点不重复,考虑到 map 的特性,我们令 mp[a][b] 的值从 n 号点开始递增,从而保证点的唯一性

需要注意的是,由于拆完点后点和边的数目会很多,因此数组范围要开的大一点

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
struct Edge {int to,next,val;Edge(){}Edge(int to,int next,int val):to(to),next(next),val(val){}bool operator < (const Edge& rhs)const{return val>rhs.val;}
}edge[N*2];
int tot,head[N];
void addEdge(int from,int to,int val) {edge[tot].next=head[from];edge[tot].to=to;edge[tot].val=val;head[from]=tot++;
}
bool vis[N];
int dis[N];
int SPFA(int s,int e) {memset(vis,false,sizeof(vis));memset(dis,INF,sizeof(dis));dis[s]=0;priority_queue<Edge> Q;Q.push(Edge(s,0,0));while (!Q.empty()) {Edge temp=Q.top();Q.pop();if(vis[temp.to])continue;vis[temp.to]=true;for(int i=head[temp.to]; i!=-1; i=edge[i].next) {if(dis[edge[i].to]>dis[temp.to]+edge[i].val) {dis[edge[i].to]=dis[temp.to]+edge[i].val;Q.push(Edge(edge[i].to,0,dis[edge[i].to]));}}}if(dis[e]!=INF)return dis[e]/2;return -1;
}
map<int,int> mp[N];
int allPoint;
int getPoint(int x,int y) {if (!mp[x][y])mp[x][y]=++allPoint;return mp[x][y];
}
int main() {int n,m;scanf("%d%d",&n,&m);memset(head,-1,sizeof(head));allPoint=n;for (int i=1; i<=m; i++) {int x,y,w;scanf("%d%d%d",&x,&y,&w);int newX=getPoint(x,w);int newY=getPoint(y,w);addEdge(x,newX,1);addEdge(newX,x,1);addEdge(newX,newY,0);addEdge(newY,newX,0);addEdge(y,newY,1);addEdge(newY,y,1);}int res=SPFA(1,n);printf("%d\n",res);return 0;
}

すぬけ君の地下鉄旅行 / Snuke's Subway Trip(AtCoder-2069)相关推荐

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

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

  2. すぬけ君の地下鉄旅行

    题目链接:すぬけ君の地下鉄旅行 显然我们可以记录每个状态前一个状态通过的铁路.然后判断这一次边权值. 但是我们不能只用一个状态来记录,因为可能两个状态的dis相等,但是具体用哪个点更新是不确定的. 所 ...

  3. AtCoder Regular Contest 061 E - Snuke‘s Subway Trip(建图 + dijkstra最短路 / 0/1bfs / 并查集)

    AtCoder Regular Contest 061 E - Snuke's Subway Trip problem 洛谷翻译 my idea 最近一直在做网络流,所以一读这题后,我就想到了最小费用 ...

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

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

  5. 【AtCoder 2069】Snuke's Subway Trip(构图方法)

    题目链接:[AtCoder 2069]Snuke's Subway Trip 题目大意:有 n n n个节点,m" role="presentation" style=& ...

  6. すぬけ君の塗り絵 2 イージー / Snuke's Coloring 2 (ABC Edit) AtCoder - 2145

    这个题就是典型的想出轮廓后,然后再具体分类,弄出各种情况 其中发现了一个新的地方,之前是要记得每次循环更新数据,这次更新数据是要判断是否更新后,然后再更新(其实就可以用max与min函数) #incl ...

  7. android绘制矢量图_Android矢量可绘制对象

    android绘制矢量图 Everything about Vector Assets in Android 关于Android中的矢量资产的一切 介绍 (Introduction) In the b ...

  8. atcoder题目合集(持续更新中)

    Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...

  9. AtCoder Regular Contest 061

    文章目录 C - Many Formulas D - Snuke's Coloring E - Snuke's Subway Trip F - Card Game for Three 传送门 C - ...

最新文章

  1. 读取Assets中的文件数据
  2. ERICA:提升预训练语言模型实体与关系理解的统一框架
  3. 沙龙预告 | 新书分享《崛起的超级智能:互联网大脑如何影响科技未来》
  4. oracle rac重建grid,oracle 11g rac安装之grid报错解决
  5. keil 函数 默认 外部 内部 博客_5.9 C++内部函数与外部函数
  6. CygWin / 获取 root 权限的方法
  7. tornado 学习笔记17 HTTPServerRequest分析
  8. MyBatis_1 简介
  9. RTOS原理与实现09:事件标志组实现
  10. hive报错(1)MoveTask
  11. 华为手机android版本升级失败怎么办,华为手机系统更新好吗 华为手机系统更新方法...
  12. [转载] numpy.take()从数组中取指定的行或列
  13. 创建ServerSocket出错Permission denied
  14. 7.深入分布式缓存:从原理到实践 --- Redis探秘
  15. 查阅我们JavaScript学习新指南
  16. java游戏下载怎么玩_jar的手机游戏怎么玩?java手机游戏的玩法
  17. 学计算机能把照片还原吗,要在计算机上恢复的1英寸照片的图像大小是多少? -恢复照片图像尺寸恢复...
  18. Vue入门(10)axios
  19. 【操作系统 - 1】先来先服务FCFS和短作业优先SJF进程调度算法
  20. 【持续更新】pip install报错解决汇总

热门文章

  1. 初学c#读书笔记(一)--c#和.NET Framework
  2. 数字化转型最致命的5个误区
  3. 中台生态的形成:全面解读技术、研发、移动中台建设
  4. Linux调试时常见问题,C程序在linux下调试时经常出现的问题
  5. 今年面试,光靠技术肯定不行了!
  6. pick王菊?作为“菊外人”的程序员能做点什么?
  7. SpringBoot2 整合 ClickHouse数据库,实现高性能数据查询分析
  8. Servlet - 基础
  9. TCP/IP入门(1) --链路层
  10. Search API