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 ...
随机推荐
- 第三百零五节,Django框架,Views(视图函数),也就是逻辑处理函数里的各种方法与属性
Django框架,Views(视图函数),也就是逻辑处理函数里的各种方法与属性 Views(视图函数)逻辑处理,最终是围绕着两个对象实现的 http请求中产生两个核心对象: http请求:HttpRe ...
- web开发之微信公众号---微信公众好开发
--------------------------------------time:2015/11/5 ----------------------------------------------- ...
- 浅谈cookie测试
Cookie 提供了一种在Web 应用程序中存储用户特定信息的方法,例如存储用户的上次 访问时间等信息.假如不进行cookie存储一个网站的用户行为,那么可能会造成以下问题:用户进行购买几件商品转到结 ...
- Visual Basic的未来之路
Green首先列出了当时使用VB进行开发的四个基础指导原则: 1.VB和C#共享的通用IDE和平台构建块. 2.共享的“多范式.面向对象.命令式.强类型等”语言 ...
- js表单计算金额问题
<table width="600" border="1" align="center" style="text-align ...
- web.xml 中的listener、filter、servlet 加载顺序及其【配置详解】
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- Razor------引入css文件的方法
MVC3.0之前版本: <link href=@Url.Content("~/Content/Styles.css") rel="stylesheet" ...
- 如何在单片机上使用printf函数(printf)(avr)(stm)(lpc)(单片机)(转)
摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上,来判断我们的程序是否按预期的运行,printf函数很好的做到了这一点,它能直接以字符的方式输出变量名和变量的值,printf ...
- 当div没有设置宽度,使用width的fit-content和margin:auto实现元素的水平居中
当我们做水平居中的时候,会有许多方法,margin:0 auto,或者test-align:center,以及flex布局.当元素的width不固定的时候,我们如何实现水平居中呢,代码如下: < ...
- PyQt4重写事件处理方法
PyQt中的事件处理主要以来重写事件处理函数来实现. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...