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) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3821 Accepted Submission(s): 1214
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)
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.
题意概括:
N个点,M条无向边,每条边都有编号,经过相同的编号的边不会改变花费,经过不同的编号的边花费加一。
解题思路:
BFS 求最短路,用边替换点进队,按照花费升序排序。
可能数据偏水,可以卡过去。
AC code:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 1e5+;
bool vis[MAXN];
int N, M; struct Edge
{
int nxt, no, v;
}edge[MAXN<<];
int head[MAXN], tot;
void init()
{
memset(vis, , sizeof(vis));
memset(head, -, sizeof(head));
tot = ;
} void add(int a, int b, int no)
{
edge[tot].v = b;
edge[tot].no = no;
edge[tot].nxt = head[a];
head[a] = tot++;
} struct data
{
int u, v, cnt, type;
bool operator < (const data &a) const {
return cnt > a.cnt;
}
}; int solve()
{
data x, y;
int from, to, t, vv, sum;
priority_queue<data>quq;
for(int i = head[]; i != -; i = edge[i].nxt){
x.u = ;
x.v = edge[i].v;
x.cnt = ;
x.type = edge[i].no;
quq.push(x);
}
vis[] = true;
while(!quq.empty()){
x = quq.top();
quq.pop();
to = x.v;
t = x.type;
sum = x.cnt;
if(to == N) return sum;
vis[to] = true;
for(int i = head[to]; i != -; i = edge[i].nxt){
vv = edge[i].v;
if(vis[vv]) continue;
if(edge[i].no != t) y.cnt = sum+;
else y.cnt = sum;
y.v = vv;
y.type = edge[i].no;
quq.push(y);
}
}
return -;
} int main()
{
int u, v, no;
while(~scanf("%d %d", &N, &M)){
init();
for(int i = ; i <= M; i++){
scanf("%d %d %d", &u, &v, &no);
add(u, v, no);
add(v, u, no);
} int ans = solve();
printf("%d\n", ans);
}
return ;
}
HDU 6386 Age of Moyu 【BFS + 优先队列优化】的更多相关文章
- HDU - 6386 Age of Moyu (双端队列+bfs)
题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...
- HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)
题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...
- 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
Problem Description Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting ...
- HDU 6386 Age of Moyu (最短路+set)
<题目链接> 题目大意:给定一张无向图,有n个点m条边,从一条边到另一条边,如果两边的指不同 花费就要+1,如果相同就不需要花费. 先从1走到n问最小花费是多少.(第一条边的花费都是1) ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- 【idea快捷键】
IntelliJ Idea 常用快捷键列表 idea也是可以切换到eclipse风格的快捷键方式的 在keymap中切换即可 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表 ...
- fabu dao fenleizhong
IsAggregated IsAggregatedIsAggregated IsAggregatedIsAggregated IsAggregatedIsAggregated IsAggregated ...
- CSS基础知识---浮动,定位和盒模型
转载请注明出处! 需要掌握的三个最重要的CSS概念是浮动,定位和盒模型. 盒模型概述: 页面上的每个元素都被看做一个矩形框(元素框or盒模型),这个框由元素内容,内边距,边框和外边距组成. 内边距出现 ...
- Hibernate 注解(Annotations 二)一对一双向注解
注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法.局部变量.方法参数等的前面,用来 ...
- java 输出菱形
package com.demo01; public class Triangle { /** * @param args */ /* * 第一步:规定输出的行数 * 第二步:输出空格 再输出一个星, ...
- csharp:.net 3.5 using System.Runtime.Serialization.Json read json
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Python基础-socket编程
一.网络编程 自从互联网诞生以来,现在基本上所有的程序都是网络程序,很少有单机版的程序了. 计算机网络就是把各个计算机连接到一起,让网络中的计算机可以互相通信.网络编程就是如何在程序中实现两台计算机的 ...
- [小北De编程手记] : Lesson 03 - Selenium For C# 之 元素定位
无论哪一种自动化测试的驱动框架(基于B/S,桌面应用,还是手机App).都应当具有一套优秀的元素定位技术.通常的自动化测试流程也可以简单的归结为是一个从被测试程序中识别或是定位元素以及执行操作和验证元 ...
- 寒假来了,阿里游戏云6000、20000元新春大礼,游戏开发的骚年们r u ready?
寒假来了,游戏开发的骚年们,r u ready? 亿元云计算基金.游戏云计算解决方案.尊享VIP服务,为你“三羊开泰”! 现在参与游戏云认证,即享6000元.2万元… 最高100万云基金!走你> ...
- Android GridView设置行数
普通的做法是设置一个高度,然后里面能显示出来几行就是几行,如果里面的内容高度变了,就需要重新调整高度来适配. 观察了一下它的onMeasure @Override protected void onM ...