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. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fa5519] was not registered for synchronization because synchronization is not active

    Creating a new SqlSessionSqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1fa5519] w ...

  2. webpack-webpackConfig-plugin 配置

    ProvidePlugin 语法: module.export = { plugins: [ new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jqu ...

  3. h5:WebSocket

    实时 Web 应用的窘境 Web 应用的信息交互过程通常是客户端通过浏览器发出一个请求,服务器端接收和审核完请求后进行处理并返回结果给客户端,然后客户端浏览器将信息呈现出来,这种机制对于信息变化不是特 ...

  4. <Android开源库 ~ 1> GitHub Android Libraries Top 100 简介

    转载自GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitH ...

  5. Android - 开发页面需了解的dip,sp,px知识,以及它们的转换

    工作中,时常会有任务要求开发新页面,这时一般的流程是产品经理确定要开发的页面和功能,然后设计师提供设计稿,之后由我们开发人员完成开发工作. 通常,设计师提供的设计稿尺寸标注会很详细,例如涉及到字时,字 ...

  6. 微软RPC技术学习小结

    RPC,即Remote Procedure Call,远程过程调用,是进程间通信(IPC, Inter Process Communication)技术的一种.由于这项技术在自己所在项目(Window ...

  7. selenium 服务器端运行命令

    cd C:\Users\kfa_wangchao\Downloadsjava -jar selenium-server-standalone-2.37.0.jarcmd=getNewBrowserSe ...

  8. 笨办法学Python(三十一)

    习题 31: 作出决定 这本书的上半部分你打印了一些东西,而且调用了函数,不过一切都是直线式进行的.你的脚本从最上面一行开始,一路运行到结束,但其中并没有决定程序流向的分支点.现在你已经学了 if,  ...

  9. CToolTipCtrl使用详细解说

    很多的界面设计都需要有Tip提示,下面描述一下Tip的简单使用方法: 1. 首先要New一个CToolTipCtrl的对象m_pContentTip 2. 调用CToolTipCtrl的create函 ...

  10. Jmeter入门8 连接microsoft sql server数据库取数据

    本文以Jmeter 连接microsoft sql server为例. 1 从微软官网下载Microsoft SQL Server JDBC Driver 地址:http://www.microsof ...