Age of Moyu

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 3125    Accepted Submission(s): 981

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

2018 Multi-University Training Contest 7

Recommend

chendu   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404

其实当时没怎么认真思考这道题要怎么写

补题时候看的题解

有个题解说用set 其实没怎么搞清楚要怎么写 但是这种方法也可以实现

用优先队列优化dijkstra 队列维护的是边而不是点

根据当前边的编号决定cost是否增加

因为是优先队列所以可以保证最先到达n的线路是cost最小的

好像比赛的时候他们写T了好多次....不知道他们是怎么写的


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std; int n, m;
const int maxn = 100005;
const int maxm = 200005;
int head[maxn], cnt;
struct edge{
int v, f, w;
int nxt;
bool operator < (edge a) const{
return w > a.w;
}
}e[maxm * 2]; void addedge(int u, int v, int f)
{
e[cnt].v = v;
e[cnt].f = f;
e[cnt].nxt = head[u];
head[u] = cnt;
cnt++;
} int dis[maxn], vis[maxn];
void init()
{
cnt = 0;
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis));
memset(head, -1, sizeof(head));
} void dijkstra()
{
dis[1] = 0;
priority_queue <edge> q;
while(!q.empty()){
q.pop();
}
edge now;
now.v = 1;
now.f = 0;
now.w = 0;
q.push(now);
while(!q.empty()){
now = q.top();
q.pop();
int u = now.v;
if(vis[u]){
continue;
}
vis[u] = true;
dis[u] = now.w;
for(int i = head[u]; ~i; i = e[i].nxt){
int v = e[i].v;
if(!vis[v]){
edge nex;
nex.v = v;
nex.f = e[i].f;
nex.w = dis[u] + (e[i].f != now.f);
q.push(nex);
}
}
}
} int main()
{
while(scanf("%d%d", &n, &m) != EOF){
init(); for(int i = 0; i < m; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c);
addedge(b, a, c);
} dijkstra();
if(dis[n] == inf){
printf("-1\n");
}
else{
printf("%d\n", dis[n]);
}
}
return 0;
}

hdu6386 Age of Moyu【最短路】的更多相关文章

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

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

  2. 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 ...

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

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

  4. HDU 6386 Age of Moyu

    Problem Description Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting ...

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

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

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

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

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

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

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

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

  9. HDU-6386-最短路

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

随机推荐

  1. 第二百八十五节,MySQL数据库-MySQL函数

    MySQL数据库-MySQL函数 1.MySQL内置函数 SELECT执行函数,后面跟要执行的函数 CHAR_LENGTH(str)函数:返回字符串的字符长度 -- CHAR_LENGTH(str)函 ...

  2. 使用定时器判断确保某个标签有值才执行方法, 控制js代码执行先后顺序

    使用定时器判断确保某个标签有值才执行方法: var wait = setInterval(function(){ var diqu = $("#diqu").val(); //确保 ...

  3. 中文路径-接口路径url不能传输中文解决方案

    服务端:

  4. Spring-MVC案例:Spitter的笔记

    源码地址:https://github.com/Young4Dream/yan/tree/master/Maven_spittr 笔记: 1.当DispatcherServlet启动时,会创建Spri ...

  5. ThinkPHP Mongo驱动update方法支持upsert参数

    Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...

  6. Ajax在ASP.NET MVC中上传

    HomeController.cs using System; using System.Collections.Generic; using System.Linq; using System.We ...

  7. 第四章 Spring.Net 如何管理您的类___对象的生命周期链

    各位,实在不好意思,因为有事,博客几天没更新了.前面有童鞋提到,配置 Objects.xml 路径的相关问题,这些东西是 IResource 接口的一些内容.在下一章会详细介绍. 传统的Net应用中, ...

  8. SQLServer------如何快速插入几万条测试数据

    方法一: 1.建表 if OBJECT_ID('test') is not null drop table test go create table test (id ,),vid ), constr ...

  9. 单行dp复习hdu1087

    我写的想法是每个dp[i]都是前dp[i]的最大值 dp[i]就等于前全部dp[0...i-1]的最大值加上dp[i] 最大值是一个中间变量 最大值得选取条件就是序列的值大小都是递增的,也就是a[i] ...

  10. 超全面的JavaWeb笔记day03<JS对象&函数>

    1.js的String对象(****) 2.js的Array对象 (****) 3.js的Date对象 (****) 获取当前的月 0-11,想要得到准确的月 +1 获取星期时候,星期日是 0 4.j ...