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. (生产)vue-router:路由

    参考:https://router.vuejs.org/zh-cn/ 安装 直接下载 / CDN https://unpkg.com/vue-router/dist/vue-router.js 使用: ...

  2. Dynamics CRM 4.0升级Dynamics CRM 2013后全局Ribbon的修改

    最近在为一个客户在Dynamics CRM 4.0到Dynamics CRM 2013的升级,升级之后发现原来在Dynamics CRM 4.0中定义的全局Ribbon按钮像牛皮癣一样,在每个实体页面 ...

  3. (C#) 线程之 AutoResetEvent, EventHandle.

    AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号.如果 AutoRese ...

  4. 【起航计划 022】2015 起航计划 Android APIDemo的魔鬼步伐 21 App->Launcher Shortcuts 为某个非主Activity在Home Screen上建立一个快捷方式

    Android 操作系统对于<intent-filter>含有下列属性的Activity会在应用程序管理器(Launcher)显示一项,一般这个Activity对应于某个应用的主Activ ...

  5. Java1.7新特性

    1.switch语句支持字符串变量 public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) { String typeOf ...

  6. IOS开发入门你们准备好了吗?

    我们对于IOS的了解最多应该就是苹果手机独有的IOS系统吧,也可以说是单任务管理器,这可以说是一个优势,但是随着技术提升IOS慢慢有被超越的趋势,但是很多大公司还是需要这方面的开发人才,那么今天我们来 ...

  7. 3dsmax2014的下载、安装与注册激活教程详解

    3dsmax2014的下载.安装与注册激活教程,虽然网上类似的教程文章不胜枚举,但大多比较粗枝大叶,没有详细的步骤,尤其对于电脑小白来说,更是不易参考,今天我就教大家如何注册破解3dsmax2014吧 ...

  8. leetcode:查找

    1.  word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...

  9. 使用ABAP Push Channel(APC)开发的乒乓球游戏,可双打

    url: https://:/sap/bc/apc_test/ping_pong/game 或者事务码SICF, 输入ping_pong, 按F8: 选中搜索结果,点右键选择Test,即可打开url. ...

  10. EF问题集合

    1. 在使用数据迁移的过程中,如果手工删除了本地数据库之后,再次尝试连接被删除的数据库,会有以下提示: System.Data.SqlClient.SqlException (0x80131904): ...