hdu6386 Age of Moyu【最短路】
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【最短路】的更多相关文章
- HDU 6386 Age of Moyu (最短路+set)
<题目链接> 题目大意:给定一张无向图,有n个点m条边,从一条边到另一条边,如果两边的指不同 花费就要+1,如果相同就不需要花费. 先从1走到n问最小花费是多少.(第一条边的花费都是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 ...
- Age of Moyu HDU - 6386 (杭电多校7A)
给出n和点,m条边,每条边有各自的标号,进入第一个标号需要消耗1的费用,此后转换标号需要1费用,在同一个标号上走不需要费用.问你从1到n最少需要多少费用. 最短路变形,把第一个点看成不存在的标号,然后 ...
- HDU 6386 Age of Moyu
Problem Description Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting ...
- hdu 6386 Age of Moyu (重边判断)
本来用一个map判重边结果T了, 实际上可以直接给边上打标记即可 int n, m; struct _ {int to,w,vis;}; vector<_> g[N]; int dis[N ...
- HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)
题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...
- Age of Moyu (2018 Multi-University Training Contest 7)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ ,f= ...
- HDU - 6386 Age of Moyu (双端队列+bfs)
题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...
- HDU-6386-最短路
Age of Moyu Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
随机推荐
- 在windows下编译ffmpeg
编译ffmpeg,我在网上找了很多相关的方法,但最后都没编译成功. 所以下面就记录下自己的编译方法吧,留着以后编译的时候做参考. 1.首先,下载编译工具MinGW+Msys,搭建编译环境.工具下载地址 ...
- 其它系统与domino系统单点登录的实现方式
其它系统与domino系统单点登录的实现方式 [背景] 随着企业中业务不断增多,用户处理不同的业务则须要频繁的切换不同的系统进行操作.而用户则须要记住各个系统的username.password ...
- c语言的fopen
c语言fopen函数 fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式); 其中, “文件指针名”必须是被说明为FILE 类型的指针变量: “文件名” ...
- Extjs 下拉框
刚刚熟练了easyui控件的使用,又開始了如今的这个项目. 这个项目是个半成品.前端使用的是Extjs控件,jsp中没有代码.就引用了非常多的js...于是乎有种不知所措了呀. . . 说实话特别的不 ...
- Linux /proc/loadavg(平均负载)
from : http://hi.baidu.com/mengyun8/blog/item/bd424531451b98e71a4cffc0.html 一.什么是系统平均负载(Load average ...
- 提高Web性能的前端优化技巧总结
- plsql developer中,清除登录历史
需求描述: 在使用plsql developer的时候,发现登录的时候,有太多的历史,想要把这些登录历史清除掉, 在此记录下. 操作过程: 1.登录plsql developer(或者在登录时取消也会 ...
- spring配置文件中bean标签
<bean id="beanId"(1) name="beanName"(2) class="beanClass"(3) parent ...
- php开n次方
php有开平方函数 sqrt,但没开n次方的函数 网上用根据什么数字原理,可用次方(pow)弄开方,格式为:pow(number, 1/ 开方数) 例如: 4的开平方,可以写成 pow(4, 1/2) ...
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
[文章作者:张宴 本文版本:v1.0 最后修改:2011.08.05 转载请注明原文链接:http://blog.s135.com/file_get_contents/] 有时候,运行 Nginx.P ...