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 ...
随机推荐
- 单精度浮点数(float)加法计算出错
场景: 一个float型的变量赋值1170601,加上19000000,结果出现错误. 原因: float占用4个字节(32位)存储空间,包括符号位1位,阶码位8位,尾数23位.浮点数精度与它的尾数有 ...
- C# HttpClientHelper请求
public class HttpClientHelper { /// <summary> /// get请求 /// </summary> /// <param nam ...
- Spring-注入外部值
Spring注入需要初始化,但前面均使用硬编码注入,如: JavaConfig配置: package soundSystem; import org.springframework.stereotyp ...
- 创建SQL语句_面试
创建一个表:create table if not exists Teachaers(tea_id integer primary key autoincrement,tea_name text,t ...
- myEclipse svn 插件安装
MyEclipse6.0 安装svn插件 博客分类: 技术 只说一种在线安装流程: 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Ins ...
- cesium图形上加载图片
<!DOCTYPE html> <html> <head> <!-- Use correct character set. --> <meta c ...
- TCP处理主要开销
快速的网络TCP 通常受限 发送主机 与 接收主机. 而不是网络设备或协议本身的实现. TCP的处理的主要开销 分为中断操作.数据复制和协议处理. 1:中断操作 2:数据复制 3:协议处理 TCP的处 ...
- Android DOM解析XML示例程序
DOM方式解析xml是先把xml文档都读到内存中,然后再用DOM API来访问树形结构,并获取数据的.DOM比较符合人的思维模式,但是其对内存的消耗比较大. activity_main.xml < ...
- NUC970设备驱动
安装完WinUSB4NuVCOM_NUC970.exe后 USB0要配置成DEVICE 才可以在设备管理器中显示.
- Apache nutch1.5 & Apache solr3.6
第1章引言 1.1nutch和solr Nutch 是一个开源的.Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具. Solr 拥有像 web-services API 的独立的 ...