2018.09.19 atcoder Snuke's Subway Trip(最短路)
传送门
就是一个另类最短路啊。
利用颜色判断当前节点的最小花费的前驱边中有没有跟当前的边颜色相同的。
如果有这条边费用为0,否则费用为1.
这样跑出来就能ac了。
代码:
#include<bits/stdc++.h>
#define N 500005
#define M 500005
using namespace std;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
int n,m,first[N],cnt=0,d[N];
bool vis[N];
struct edge{int v,next,c;}e[M<<1];
inline void add(int u,int v,int c){e[++cnt].v=v,e[cnt].next=first[u],e[cnt].c=c,first[u]=cnt;}
struct heap{int u,v;};
inline bool operator<(heap a,heap b){return a.v==b.v?a.u<b.u:a.v<b.v;}
priority_queue<heap>q;
set<int>col[N*10];
inline void dijkstra(){
for(int i=2;i<=n;++i)d[i]=0x3f3f3f3f,vis[i]=false;
q.push((heap){1,d[1]});
while(!q.empty()){
heap x=q.top();
q.pop();
if(vis[x.u])continue;
vis[x.u]=1;
for(int i=first[x.u];i;i=e[i].next){
int v=e[i].v,w=col[x.u].count(e[i].c)?0:1;
if(d[v]>d[x.u]+w)d[v]=d[x.u]+w,q.push((heap){v,-d[v]}),col[v].clear(),col[v].insert(e[i].c);
else if(d[v]==d[x.u]+w)col[v].insert(e[i].c);
}
}
}
int main(){
n=read(),m=read();
for(int i=1;i<=m;++i){
int u=read(),v=read(),c=read();
add(u,v,c),add(v,u,c);
}
dijkstra();
printf("%d",d[n]==0x3f3f3f3f?-1:d[n]);
return 0;
}
2018.09.19 atcoder Snuke's Subway Trip(最短路)的更多相关文章
- 2018.09.19 atcoder Snuke's Coloring(思维题)
传送门 谁能想到这道题会写这么久. 本来是一道很sb的题啊. 就是每次选一个点只会影响到周围的九个方格,随便1e9进制就可以hash了,但是我非要作死用stl写. 结果由于技术不够高超,一直调不出来. ...
- 2018.09.22 atcoder Snuke's Coloring 2(线段树+单调栈)
传送门 就是给出一个矩形,上面有一些点,让你找出一个周长最大的矩形,满足没有一个点在矩形中. 这个题很有意思. 考虑到答案一定会穿过中线. 于是我们可以把点分到中线两边. 先想想暴力如何解决. 显然就 ...
- 2018.09.19 atcoder AtCoDeer and Rock-Paper(贪心)
传送门 sb贪心啊. 显然能选帕子就选帕子. 首先假设第一个人全出石头. 考虑把一些石头修改成帕子. 这样贡献只增不减,加起来就是答案. 代码: #include<bits/stdc++.h&g ...
- 2018.09.19 atcoder AtCoDeer and Election Report(贪心)
传送门 很有意思的一道贪心. 就是每次翻最小的倍数来满足条件. 代码: #include<bits/stdc++.h> #define ll long long using namespa ...
- 2018.09.19 atcoder Card Game for Three(组合数学)
传送门 简单组合数学想优化想了半天啊233. 我们只需考虑翻开n张A,b张B,c张C且最后一张为A的选法数. 显然还剩下m+k−b−cm+k-b-cm+k−b−c张牌没有选. 这样的话无论前n+b+c ...
- AtCoder ARC061E Snuke's Subway Trip 最短路
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门 Portal 原题目描述在最下面. \(n(1 ...
- 【例题收藏】◇例题·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 ...
- AtCoder arc061C Snuke's Subway Trip
大意: 给你一张无向图,边有种类. 当你第一次/重新进入某种边时费用 + 1 在同一种边之间行走无费用. 求 1 到 n 的最小费用. 嗯...乍一看有一个很直观的想法:记录每个点的最短路的上一条边的 ...
随机推荐
- leetcode29
class Solution { public int divide(int dividend, int divisor) { if (dividend == Integer.MIN_VALUE &a ...
- JDK1.7之后switch支持string
转自:https://blog.csdn.net/tjcyjd/article/details/9666035 在Java7之前,switch只能支持 byte.short.char.int或者其对应 ...
- VBA 获取文件夹内的文件列表
Private strFiles(20) Private Sub GetFiles(ByVal strFolder As String, ByVal ty As String) ' 获取文件列表 If ...
- json decimal and datetime
python json模块默认不能序列化decimal和datetime数据,可以通过自定义一个序列化的类实现: link: http://www.cnblogs.com/buxizhizhoum/p ...
- 更新日志(建议升级到2017.1.18a) && 更新程序的方法
更新程序的步骤: 1,在控制面板里点击备份当前数据库文件到磁盘,把当天获取的信息从内存写到磁盘/存储卡.2,下载最新版的源码 wget -O "infopi.zip" " ...
- spark UDF函数
Spark(Hive) SQL中UDF的使用(Python):http://www.tuicool.com/articles/3yMBNb7
- python文件的只读,只写操作
只读:r rb(bytes类型数据) 只写:w wb(bytes类型数据) 在文件最后追加: f = open('log',mode='a',encoding='utf-8') f.write('这里 ...
- linux 杂
set -e表示一旦脚本中有命令的返回值为非0,则脚本立即退出,后续命令不再执行; set -o pipefail表示在管道连接的命令序列中,只要有任何一个命令返回非0值,则整个管道返回非0值,即使最 ...
- select语法图
- Hibernate实体映射转换列值
@Column(name="ADDTIME", insertable=false, updatable=false)@ColumnTransformer(read="CA ...