Snuke's Subway Trip
すぬけ君の地下鉄旅行 / Snuke's Subway Trip
Time limit : 3sec / Memory limit : 256MB
Score : 600 points
Problem Statement
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≤i≤M ) 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≤pi≤N (1≤i≤M)
- 1≤qi≤N (1≤i≤M)
- 1≤ci≤106 (1≤i≤M)
- pi≠qi (1≤i≤M)
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.
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.
分析:根据贪心思想,从一个点到另一个点必然是从之前点最小花费转移过来的,否则不会更优;
所以每个点维护最小花费和到达前的公司id,用最短路拓展即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,h[maxn],tot,ans[maxn];
set<int>comp[maxn];
struct node
{
int to,nxt,com;
}e[maxn<<];
void add(int x,int y,int z)
{
tot++;
e[tot].to=y;
e[tot].com=z;
e[tot].nxt=h[x];
h[x]=tot;
}
priority_queue<pair<int,int> >p;
int main()
{
int i,j;
memset(ans,inf,sizeof ans);
scanf("%d%d",&n,&m);
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
p.push({,});ans[]=;
while(!p.empty())
{
int now=p.top().se,ca=-p.top().fi;
p.pop();
if(ans[now]<ca)continue;
for(i=h[now];i;i=e[i].nxt)
{
int to=e[i].to,to_com=e[i].com;
int to_ca=ca+(!comp[now].count(to_com));
if(ans[to]>to_ca)
{
ans[to]=to_ca;
p.push({-to_ca,to});
comp[to].clear();
comp[to].insert(to_com);
}
else if(ans[to]==to_ca)
{
comp[to].insert(to_com);
}
}
}
printf("%d\n",ans[n]==inf?-:ans[n]);
//system("Pause");
return ;
}
Snuke's Subway Trip的更多相关文章
- 【例题收藏】◇例题·I◇ Snuke's Subway Trip
◇例题·I◇ Snuke's Subway Trip 题目来源:Atcoder Regular 061 E题(beta版) +传送门+ 一.解析 (1)最短路实现 由于在同一家公司的铁路上移动是不花费 ...
- [ARC061E]すぬけ君の地下鉄旅行 / Snuke's Subway Trip
题目大意:Snuke的城镇有地铁行驶,地铁线路图包括$N$个站点和$M$个地铁线.站点被从$1$到$N$的整数所标记,每条线路被一个公司所拥有,并且每个公司用彼此不同的整数来表示. 第$i$条线路($ ...
- AtCoder arc061C Snuke's Subway Trip
大意: 给你一张无向图,边有种类. 当你第一次/重新进入某种边时费用 + 1 在同一种边之间行走无费用. 求 1 到 n 的最小费用. 嗯...乍一看有一个很直观的想法:记录每个点的最短路的上一条边的 ...
- 2018.09.19 atcoder Snuke's Subway Trip(最短路)
传送门 就是一个另类最短路啊. 利用颜色判断当前节点的最小花费的前驱边中有没有跟当前的边颜色相同的. 如果有这条边费用为0,否则费用为1. 这样跑出来就能ac了. 代码: #include<bi ...
- ARC061E Snuke's Subway Trip
传送门 题目大意 已知某城市的地铁网由一些地铁线路构成,每一条地铁线路由某一个公司运营,该城市规定:若乘坐同一公司的地铁,从开始到换乘只需要一块钱,换乘其他公司的价格也是一块钱,问从1号地铁站到n号地 ...
- AtCoder ARC061E Snuke's Subway Trip 最短路
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门 Portal 原题目描述在最下面. \(n(1 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- atcoder题目合集(持续更新中)
Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...
- AtCoder Regular Contest
一句话题解 因为上篇AGC的写的有点长……估计这篇也短不了所以放个一句话题解方便查阅啥的吧QwQ 具体的题意代码题解还是往下翻…… ARC 058 D:简单容斥计数. E:用二进制表示放的数字,然后状 ...
随机推荐
- js解析php返回的json数据无法获取length的问题分析
1.问题出现的过程,js解析php json_encode 的数据,无法获取长度信息,提示undefined debug: 首先打印查看了php encode后的数据,返现最外层是一个 ...
- Entity Framework技巧系列之八 - Tip 29 – 34
提示29. 怎样避免延迟加载或Load()阅读器问题 如果你有如下这样的代码: 1 var results = from c in ctx.Customers 2 where c.SalesPerso ...
- isPostBack原理
从 到输入用户名,点击提交按钮 这个过程就叫做postback(是两个不同的状态) 利用ispostback原理,实现是否第一次进入处理程序(上一个用用户名判断的不好,会导致在用户名空的情况下 ...
- css(非表格变成表格用)
父元素:display:table: 子元素:display:table-cell:vertical-align:middle:
- 【dp】 比较经典的dp poj 1160
转自http://blog.sina.com.cn/s/blog_5dd8fece0100rq7d.html [题目大意]:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择 ...
- Facade ——为子系统的一组接口提供一致界面
Façade模式提供了子系统一组接口的一致封装特性,如下图所示: 如图所示,OperationWrapper的实现依赖SubSystem1,2等的Operation操作.但用户调用OperationW ...
- 闭包 -> map / floatMap / filter / reduce 浅析
原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...
- android之相机开发
http://blog.csdn.net/jason0539/article/details/10125017 android之相机开发 分类: android 基础知识2013-08-20 22: ...
- 为Android系统内置C可执行程序测试Linux内核驱动程序
在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中, 创建三个不同的文件节点来供用户空间访问,分别是传统的设备 ...
- js数组总结
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...