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 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
随机推荐
- skype for business 无法共享桌面、无法传输图片
以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 #以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 Sto ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project ppcloud-common: Compilation failure
本文为博主原创,未经允许不得转载: [INFO] Finished at: 2018-09-19T20:26:05+08:00[INFO] ------------------------------ ...
- Excel 导出通用类
public class ExportToExcelHelper { public static void ExportExcel(DataTable dt) { try { //创建一个工作簿 IW ...
- leetcode 7-> Reverse Integer(32-bit signed integer)
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int "&quo ...
- tkinter 打包成exe可执行文件
1.安装pyinstaller pip install pyinstaller 2.打包 打开cmd,切换到需要打包的文件(demo.py)目录.执行 pyinstaller -F -w demo.p ...
- django Admin文档生成器
Django的admindocs应用可以从模型.视图.模板标签等地方获得文档内容. 一.概览 要激活admindocs,请按下面的步骤操作: 在INSTALLED_APPS内添加django.cont ...
- C#6.0 语法
属性表达式 属性值初始化 public string name {get;set;} = "张三"; 函数表达式 NULL检查运算符 var aa = Created?.Date; ...
- 人脸识别demo使用教程
最近在研究虹软家的arcface 人脸识别 demo,现在就给大家分享一下官方的demo**工程如何使用? **1.下载代码:git clone https://github.com/asdfqwra ...
- Pychram - 使用介绍
Pychram - 使用介绍 PyCharm中Directory与Python package的区别 对于Python而言,有一点是要认识明确的,python作为一个相对而言轻量级的,易用的脚本语言( ...
- 全栈性能测试修炼宝典--Jmeter实战(二)
性能测试初体验 1.测试分类 从图中可以看出,性能测试在整个软件测试环节中占了50%的内容,比如负载测试.压力测试.性能测试.大数据量测试.恢复测试.内容泄露测试.竞品测试(比较测试)和可靠性测试. ...