任意门: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

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

题意概括:

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 + 优先队列优化】的更多相关文章

  1. HDU - 6386 Age of Moyu (双端队列+bfs)

    题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...

  2. HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)

    题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...

  3. hdu 6386 Age of Moyu (重边判断)

    本来用一个map判重边结果T了, 实际上可以直接给边上打标记即可 int n, m; struct _ {int to,w,vis;}; vector<_> g[N]; int dis[N ...

  4. HDU 6386 Age of Moyu

    Problem Description Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting ...

  5. HDU 6386 Age of Moyu (最短路+set)

    <题目链接> 题目大意:给定一张无向图,有n个点m条边,从一条边到另一条边,如果两边的指不同 花费就要+1,如果相同就不需要花费. 先从1走到n问最小花费是多少.(第一条边的花费都是1) ...

  6. hdu - 1180 诡异的楼梯 (bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...

  7. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  8. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

随机推荐

  1. SQL 文件以及文件组

    1.SQL Server根据分区表名查找所在的文件及文件组实现脚本 --SQL Server根据分区表名查找所在的文件及文件组实现脚本 SELECT fg.name AS FileGroupName ...

  2. java 并发(五)---AbstractQueuedSynchronizer

    文章部分图片和代码来自参考文章. LockSupport 和 CLH 和 ConditionObject 阅读源码首先看一下注解 ,知道了大概的意思后,再进行分析.注释一开始就进行了概括.AQS的实现 ...

  3. 基数排序——Java实现

    一.基数排序思想 相比其它排序,主要是利用比较和交换,而基数排序则是利用分配和收集两种基本操作.基数 排序是一种按记录关键字的各位值逐步进行排序的方法.此种排序一般适用于记录的关键字为整数类型的情况. ...

  4. java 用Graphics制作模糊验证码

    这篇随笔主要是java中制作验证码的效果,由于是在国庆前做的,现在也找不到原载了.我对自己整理的发表一份 生成的验证码效果如下: 一.建立一个工具类,用来生成验证码 package com.dkt.u ...

  5. Codeforces183D T-shirt

    传送门 这题好神啊……(然而我连每种物品贡献独立都没看出来…… 首先$O(n^2 m)$的DP肯定都会写,然后可以发现每种物品一定是选得越多再选一个的收益就越低,因此可以用一个堆维护当前收益最高的物品 ...

  6. 理解JS表达式

    表达式:是由运算元和运算符(可选)构成,并产生运算结果的语法结构. 基本表达式 以下在ES5中被称为基本表达式(Primary Expression) this.null.arguments等内置的关 ...

  7. 《Visual C++ 2010入门教程》系列一:关于Visual Studio、VC和C++的那些事

    原文:http://www.cnblogs.com/Mrt-02/archive/2011/07/24/2115606.html 作者:董波 日期:2010.6.15 写在前面 在我还在上学的时候,我 ...

  8. lianxi

    package dududu; public class qiqiqi { public static void main(String[] args) { // TODO 自动生成的方法存根 ; ; ...

  9. 利用Grahics 进行图片裁剪

    这两天做了一个图片对比工具,里面要处理两张大的图片,所以要对图片先进行裁剪最开始用了 /// <summary>        /// 裁剪图片        /// </summa ...

  10. arcgis silverlight api Query接口

    Query.text 是 根据发布图层的 Display Field 字段进行模糊查询的 Query.where 是输入查询语句的 如果要进行模糊查询where = F_AREA like'12312 ...