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. PHP 初学之登录查询小case

    说明:如误入本文,请忽略即可,内容仅为记录. 功能:登录(不验证),查询所有列表,删除记录.--很简单,仅为熟悉代码. // MySQL,新建数据库data,导入如下sql ; -- -------- ...

  2. perl 脚本将phred33 转换为phred64

    今天用fastx_tookit 时遇到问题, 我的fastq 文件的碱基质量值格式为phred33, 而fastq_tookit 默认碱基质量值的格式为phred64, 所以报错了,提示我的fastq ...

  3. 手动安装OpenStack Mistral

    Prepare packages: $ sudo apt-get install python-dev python-setuptools python-pip libffi-dev libxslt1 ...

  4. C语言中,为什么字符串可以赋值给字符指针变量

    转载于:http://www.cnblogs.com/KingOfFreedom/archive/2012/12/07/2807223.html 本文是通过几篇转帖的文章整理而成的,内容稍有修改: 一 ...

  5. TTCN中PTC的执行流程

    一些概念 Component(測试组件或者測试成分),TTCN接触下来最频繁的就是MTC(Main Test Component,主測试组件),在执行測试用例前,须要首先创建一个MTC.在testca ...

  6. Oracle会话及连接数优化

    一.改动Oracle会话及最大连接数 1.查看最大连接数 SQL> show parameter processes; NAME                                 ...

  7. 用命令行执行ROBOT FRAMEWORK用例

    转自:http://www.51testing.com/html/86/n-1430786.html 除了在ride中执行用例,我们也可以通过命令行的形式执行用例. 1.执行一整个项目 pybot+项 ...

  8. Solr4.0+IKAnalyzer中文分词安装

    1.依赖: JDK1.6,Tomcat 5.5,Solr 4.0.0,IKAnalyzer 2012FF Tomcat虽然不是必须,但觉得上生产环境的话,还是得用Tomcat,便于统一管理和监控. T ...

  9. sphinx的配置和管理.No2

    网上配置文档众多,但是对着他们的文档来做老是出问题,于是花了点时间研究了一下,写成总结,方便以后查阅.也希望学习sphinx的朋友能少走弯路.Coreseek的安装请参考:http://blog.ch ...

  10. cocos2d-x游戏引擎核心之九——跨平台

    一.cocos2d-x跨平台 cocos2d-x到底是怎样实现跨平台的呢?这里以Win32和Android为例. 1. 跨平台项目目录结构 先看一下一个项目创建后的目录结构吧!这还是以HelloCpp ...