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做肯定出 ...
随机推荐
- MySQL触发器基本介绍
基本简介: 1.触发器可以让你在执行insert,update,delete语句的时候,执行一些特定的操作.并且可以在MySQL中指定是在sql语句执行前触发还是执行后触发. 2.触发器没有返回值. ...
- 自动收缩数据库T-SQL
alter database 数据库名 set auto_update_statistics off alter database 数据库名 set auto_update_statistics on
- Struts2 知识点梳理
一.Struts2简介 1.概念:轻量级的MVC框架,主要解决了请求分发的问题,重心在控制层和表现层.低侵入性,与业务代码的耦合度很低.Struts2实现了MVC,并提供了一系列API,采用模式化方式 ...
- 三:Mybatis知识整理(3)
一:mybatis中模糊查询的方法: 1.直接传参法:在java传参时进行拼接 -- %keyword% 2.mysql内置函数:concart('%',#{keyword},'%') -- 拼接sq ...
- 固态硬盘SSD与闪存(Flash Memory)
转自:http://qiaodahai.com/solid-state-drives-ssd-and-flash-memory.html 固态硬盘SSD(Solid State Drive)泛指使用N ...
- How to limit Dialog's max height?
1. We can make it to play trick in code. At Dialog's show function, after app has set contentView, w ...
- Thrift笔记(七)--回调源码分析
网上找了写代码,东拼西凑写了个demo.开始server用的是阻塞io,不行,换成非阻塞的io就可以.这里可能需要注意下 thrift文件 namespace java com.gxf.thrift ...
- Java测试工具使用(1)--Junit
在进行测试之前需要导入junit的两个包,分别是 junit:4.12;hamcrest-core:1.1 1.基本测试标签 @Test.@Before.@After 2.组测试 有时候多个测试文件, ...
- jQuery Text-to-Speech 谷歌在线语音
<!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" ...
- 2018.10.26NOIP模拟赛解题报告
心路历程 预计得分:\(100 + 100 + 70\) 实际得分:\(40 + 100 + 70\) 妈妈我又挂分了qwq..T1过了大样例就没管,直到临考试结束前\(10min\)才发现大样例是假 ...