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 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
随机推荐
- BZOJ 4399 魔法少女LJJ(线段树合并)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=4399 思路 码农题,需要一定代码功底.方法很暴力,先将权值离散,表示在线段树里储存的位置,每 ...
- JavaScript(2)
JavScript在页面上显示时间,首先我们先来了解关于时间的一些简单方法: getFullYear() 获取当前年份,getMonth() 0-n(一月到十二月),getDate() 1-31(月 ...
- s*s*r备用
遇见的问题 突然打不开 ip被墙 能ping 但是不能ssh 参考https://www.vultrcn.com/6.html
- 火狐对 min-height 的支持
代码: <!DOCTYPE html> <style> .com-center-banner { background: #f00; } .com-center-banner ...
- 【译】第41节---EF6-事务
原文:http://www.entityframeworktutorial.net/entityframework6/transaction-in-entity-framework.aspx EF默认 ...
- C++通过jsoncpp类库读写JSON文件-json用法详解
介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...
- 学习笔记47—PhotoShop技巧
1.photoshop里怎么给画布画对角线? photoshop里给画布画对角线有二种方法: 1) 选直线工具 从一角拉向另一对角 就OK了 非常简单: 2) 选钢笔工具 鼠标先点击某一角 然后再点击 ...
- CentOS7.6 如何设置静态ip
[root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts] ...
- JAVA基础知识总结:十三
测试用一个字符串常量创建一个字符串对象 1.获取这个字符串的长度 2.获取任意长度的子串 3.判断这个字符串是否为空 4.将这个字符串中的小写字母转化为大写字母 5.获取某个字符在字符串中第一次出现的 ...
- 大数据 - spark-sql 常用命令
--spark启动 spark-sql --退出 spark-sql> quit; --退出spark-sql or spark-sql> exit; 1.查看已有的database sh ...