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做肯定出 ...
随机推荐
- VMWARE 12安装Tools
准备条件 1.yum install perl 2.yum install gcc 接着就是挂载安装 新建cdrom挂载目录mkdir /mnt/cdrom挂载光驱mount -t auto /dev ...
- WES7 定制界面完整过程(去除所有windows标识)
转载但有改动 红色字体记录 目的:实验从启动开始到出现桌面,不出现任何windows图标或标识.重大提示:在某些虚拟机上面操作和真实机器是不一样的,主机会容易很多;所以在虚拟机无法实现效果的时候使用主 ...
- sql 递归 STUFF
select distinct fm_id, ,,'') AS SO_Nums from [dbo].[t_BADItems] its 表内一对多 的关系查询
- CXF - JAX-WS入门
相关dependency,我使用的版本是2.7.11: <dependency> <groupId>org.apache.cxf</groupId> <art ...
- Error -4075: File not found. An error occurred merging module <MODULENAME> for feature <FEATURENAME>.
利用Install Shield2010制作安装包的时候一直报这样的错误,原以为是我自己安装包制作流程的问题,又重新做了2个,但是还是出现问题. 解决办法: 查找资料发现是Install Shield ...
- unity3d之技能栏冷却
绑定在按钮上的脚本 using UnityEngine; using System.Collections; using UnityEngine.UI; public class CdCover : ...
- hibernate5的一些坑
SessionFactory创建的修改 如果你是刚刚从hibernate4升级到hibernate5,这时候你的项目肯定就要出错了,什么错呢? org.hibernate.MappingExcepti ...
- csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C#.
/// <summary> ///參考: http://james.newtonking.com/json/help/index.html# /// 塗聚文(Geovin Du) 2014 ...
- Python入门-再谈编码
一.小数据池 在说小数据池之前. 我们先看一个概念. 什么是代码块: 根据提示我们从官方文档找到了这样的说法: A Python program is constructed from code bl ...
- hive中的bucket table
前言 bucket table(桶表)是对数据进行哈希取值,然后放到不同文件中存储 应用场景 当数据量比较大,我们需要更快的完成任务,多个map和reduce进程是唯一的选择.但是如果输入文件是一个的 ...