Problem Description

Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M shipping lines. The ports are numbered 1 to N. Each line is occupied by a Weitian. Each Weitian has an identification number.

The i-th (1≤i≤M) line connects port Ai and Bi (Ai≠Bi) bidirectionally, and occupied by Ci Weitian (At most one line between two ports).

When Mr.Quin only uses lines that are occupied by the same Weitian, the cost is 1 XiangXiangJi. Whenever Mr.Quin changes to a line that is occupied by a different Weitian from the current line, Mr.Quin is charged an additional cost of 1 XiangXiangJi. In a case where Mr.Quin changed from some Weitian A's line to another Weitian's line changes to Weitian A's line again, the additional cost is incurred again.

Mr.Quin is now at port 1 and wants to travel to port N where live many fishes. Find the minimum required XiangXiangJi (If Mr.Quin can’t travel to port N, print −1instead)

 

Input

There might be multiple test cases, no more than 20. You need to read till the end of input.

For each test case,In the first line, two integers N (2≤N≤100000) and M (0≤M≤200000), representing the number of ports and shipping lines in the city.

In the following m lines, each contain three integers, the first and second representing two ends Ai and Bi of a shipping line (1≤Ai,Bi≤N) and the third representing the identification number Ci (1≤Ci≤1000000) of Weitian who occupies this shipping line.

 

Output

For each test case output the minimum required cost. If Mr.Quin can’t travel to port N, output −1 instead.
 
Sample Input
3 3
1 2 1
1 3 2
2 3 1
2 0
3 2
1 2 1
2 3 2
 
Sample Output
1
-1
2
 

Source

 

题解:

最短路的写法;始终去找最小费用,保存上一次的边权和这一次比较,然后查看应当+1还是+0

我是根据https://blog.csdn.net/m0_37611893/article/details/81636688 这个链接而写,

#include <bits/stdc++.h>
using namespace std;
const int MAXN=100010;
const int INF=0x3f3f3f3f;
int n,m;
struct qnode{
int v; //下一个点
int c; //当前花费
int w; //当前这条边的价值
qnode(int _v = 0, int _c = 0, int _w = 0):v(_v),c(_c),w(_w){}
bool operator < (const qnode &r) const{ //按照费用小的在前
return c>r.c;
}
};
struct edge{
int v,cost;
edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<edge>E[MAXN];
int dis[MAXN];
int vis[MAXN];
void dij()
{
for (int i = 0; i <=n ; ++i) {
dis[i]=INF;
}
memset(vis, false, sizeof(vis));
priority_queue<qnode>q;
while(!q.empty()) q.pop();
dis[1]=0;
q.push(qnode(1,0,0));
qnode tmp;
while (!q.empty())
{
tmp=q.top();q.pop();
int u=tmp.v;
if(vis[u]) continue;
vis[u]=true;
dis[u]=tmp.c;
for (int i = 0; i <E[u].size() ; ++i) {
int v=E[u][i].v;
int w1=E[u][i].cost;
if(!vis[v])
{
int temp;
if(tmp.w!=w1) temp=tmp.c+1;//如果这个边的值和上一条边值不一样,费用+1;
else temp=tmp.c;
q.push(qnode(v,temp,w1));
}
}
} }
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i = 0; i <=n ; ++i) {
E[i].clear();
}
int a,b,c;
for (int i = 0; i <m ; ++i) {
scanf("%d%d%d",&a,&b,&c);
E[a].push_back(edge(b,c));
E[b].push_back(edge(a,c));
}
dij();
if(dis[n]==INF) printf("-1\n");
else printf("%d\n",dis[n]);
}
return 0;
}

  

HDU 6386 Age of Moyu的更多相关文章

  1. HDU 6386 Age of Moyu 【BFS + 优先队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...

  2. hdu 6386 Age of Moyu (重边判断)

    本来用一个map判重边结果T了, 实际上可以直接给边上打标记即可 int n, m; struct _ {int to,w,vis;}; vector<_> g[N]; int dis[N ...

  3. HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)

    题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...

  4. HDU - 6386 Age of Moyu (双端队列+bfs)

    题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...

  5. HDU 6386 Age of Moyu (最短路+set)

    <题目链接> 题目大意:给定一张无向图,有n个点m条边,从一条边到另一条边,如果两边的指不同 花费就要+1,如果相同就不需要花费. 先从1走到n问最小花费是多少.(第一条边的花费都是1) ...

  6. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  7. hdu6386 Age of Moyu【最短路】

    Age of Moyu Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) To ...

  8. Age of Moyu HDU - 6386 (杭电多校7A)

    给出n和点,m条边,每条边有各自的标号,进入第一个标号需要消耗1的费用,此后转换标号需要1费用,在同一个标号上走不需要费用.问你从1到n最少需要多少费用. 最短路变形,把第一个点看成不存在的标号,然后 ...

  9. Age of Moyu (2018 Multi-University Training Contest 7)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ ,f= ...

随机推荐

  1. 构建跨平台APP开发的两本书,这里重点推荐下

    第一本是<构建跨平台:jquery Mobile移动应用实战> 是目前jqm开发写的比较入门的一本书,上手很快,但是高手我觉得就没有必要学习了,因为写的比较浅显. 第二本是<构建跨平 ...

  2. js 判断浏览器类型

    前言 工作中需要用到判断浏览器类型,网上找到的内容不怎么全,故在此进行一下总结. 一.不同浏览器及版本下User-Agent信息 待续.....欢迎补充 二.根据User-Agent信息进行判断 参考 ...

  3. 夜色的 cocos2d-x 开发笔记 03

    本章添加敌人,首先我们在.h文件里添加新的方法 之后进入.cpp文件,写出方法内容 当然还要调用一次,我把这个方法添加在了这里,也就是和发子弹是同步的,当然想要多久调用一次大家可以自己调整 运行一下 ...

  4. Struts2_Action

    具体视图的返回可以由用户自己定义的Action来决定:具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容:具体Action的实现可以是一个普通的java类,里面有public String ...

  5. 用iSee图片专家制作淘宝店标教程

    普通的淘宝店铺都会有店标.店标都显示在店铺首页的显现位置,买家在逛淘宝店的时候,一眼都会瞄到店标.因此,如果可以制作一个专属于自己店铺的店标,可以吸引买家的眼光,也更好地宣传了店铺. 下面就用iSee ...

  6. MYSQL导入excel

    MYSQL使用navicat导入excel 第一步:首先需要准备好有数据的excel 第二步:选择"文件"->"另存为",保存为"CSV(逗号分 ...

  7. 计算后缀表达式的过程(C#)

    计算后缀表达式的过程是一个很好玩的过程,而且很简单哦!这里呢,有个计算的技巧,就是:遇到数字直接入栈,遇到运算符就计算! 后缀表达式也叫逆波兰表达式,求值过程可以用到栈来辅助存储: 假定待求值的后缀表 ...

  8. 显示C++数据的数据类型

    #include <typeinfo> using namespace std; ... cout << typeid(d).name() << endl; 其中, ...

  9. Second last week for the second last semester!

    This week, I focused more on the final project, such as H335(Computer structure, still confused with ...

  10. dd-wrt ddns更新失败由于电信提供的ip不是公网ip

    由于电信提供的ip地址原来是公网的ip,后来电信通过nat提供一个内网ip,导致ddns更新失败.电话给电信客服10000号,让他们修改回来之后就可以了. 如果ddns更新失败,尤其是原本是正常的,后 ...