HDU-6386-最短路
Age of Moyu
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 119 Accepted Submission(s): 23
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.
1 2 1
1 3 2
2 3 1
2 0
3 2
1 2 1
2 3 2
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
int N,M;
struct Edge{
int v,w,next;
}e[];
int tot,first[],d[];
bool vis[];
set<int>S[];
void add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
} int spfa(){
memset(vis,,sizeof(vis));
memset(d,inf,sizeof(d));
for(int i=;i<=N;++i)S[i].clear();
queue<int>q;
q.push();
d[]=;
vis[]=;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=;
for(int i=first[u];~i;i=e[i].next){
int w;
if(S[u].count(e[i].w)) w=d[u];
else w=d[u]+;
if(d[e[i].v]>w){
d[e[i].v]=w;
S[e[i].v].clear();
S[e[i].v].insert(e[i].w);
if(!vis[e[i].v]){
q.push(e[i].v);
vis[e[i].v]=;
}
}
else if(d[e[i].v]==w&&S[e[i].v].count(e[i].w)==){
S[e[i].v].insert(e[i].w);
if(!vis[e[i].v]){
q.push(e[i].v);
vis[e[i].v]=;
}
}
}
}
if(d[N]==inf) return -;
return d[N];
}
int main(){
int i,j,u,v,w;
while(scanf("%d%d",&N,&M)!=EOF){
memset(first,-,sizeof(first));
tot=;
for(i=;i<=M;++i){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
cout<<spfa()<<endl;
}
return ;
}
HDU-6386-最短路的更多相关文章
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- 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 ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- HDU2112 HDU Today 最短路+字符串哈希
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2544 最短路
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
随机推荐
- [Err] 1449 - The user specified as a definer ('rybhe'@'%') does not exist
转载: 最近在做一个项目,由于服务器切换,所以需要将原有服务器的mysql数据表以及存储过程导入到另一个服务器的mysql数据库中.导入完成之后以为一切是那么的简单,却没有想到总还是出现了一些莫名其妙 ...
- Tomcat的manager app管理web项目
1.在浏览器地址栏输入http://localhost:8080/进入,如下图所示: 2.在点击Manager App 前,首次使用则需要对tomcat目录下的conf/tomcat-users.xm ...
- 基于 Python 和 Pandas 的数据分析(2) --- Pandas 基础
在这个用 Python 和 Pandas 实现数据分析的教程中, 我们将明确一些 Pandas 基础知识. 加载到 Pandas Dataframe 的数据形式可以很多, 但是通常需要能形成行和列的数 ...
- _faction
一.自定义阵营独立于联盟,部落,联盟和部落玩家可以加入同一阵营 二._function_menu表可以配置自定义阵营开启 二.配合_pvp表,可以实现区域的自定义阵营PVP 三.配合_req表fact ...
- centos nginx 中安装ssl证书 以及在项目中的使用
今天阿里云的证书到期了,重新申请了一个,下面是从申请到安装以及结合项目使用的过程: 1.登录阿里云 2.在左侧找到SSL证书 3.申请免费的证书 4.下载证书 5.根据说明配置nginx 6.在项 ...
- 百万并发中间件系统的内核设计看Java并发性能优化
“ 这篇文章,给大家聊聊一个百万级并发的中间件系统的内核代码里的锁性能优化. 很多同学都对Java并发编程很感兴趣,学习了很多相关的技术和知识.比如volatile.Atomic.synchroniz ...
- windows版 Java调用人脸识别离线sdk
最近因工作需求在java-web服务中调用人脸识别离线sdk,主要通过JNA及JNI技术,但均未调试通过,JNA调用时出现以下异常,一直未解决,求大佬指点,导常信息如下: in BaiduFaceAp ...
- Java离线人脸识别SDK 支持arcface 2.0 最新版
虹软人脸识别SDK之Java版,支持SDK 1.1+,以及当前最新版本2.0,滴滴,抓紧上车! JDK SDK Win release license status 前言 由于业务需求,最近跟人脸识别 ...
- 学习笔记54—均方误差(MSE)和均方根误差(RMSE)和平均绝对误差(MAE)
https://blog.csdn.net/reallocing1/article/details/56292877 MSE: Mean Squared Error 均方误差是指参数估计值与参数真值 ...
- 关于datatables自适应以及自定义列宽度的总结
table-layout:fixed;可以自定义列的宽度 <div id="bizhi" style="width:100%;height: 85%;overflo ...