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

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  
    
    题面贼鸡儿难理解,其实就是求最小换乘次数,第一次上路时也算一次换乘。用set维护当前最短路的状态对应的边权,然后跑最短路就好了。用spfa,不断更新,看讨论说bfs也可以做。dij的话也ok,只要不断更新所有的最短路状态就好了。
  

 #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 ;
}
 
Source
  
 

HDU-6386-最短路的更多相关文章

  1. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  2. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

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

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

  6. 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 ...

  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. HDU2112 HDU Today 最短路+字符串哈希

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 2544 最短路

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...

  10. hdu 2544 最短路 Dijkstra

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...

随机推荐

  1. Python实现机器学习算法:感知机

    ''' 数据集:Mnist 训练集数量:60000 测试集数量:10000 ------------------------------ 运行结果: 正确率:81.72%(二分类) ''' impor ...

  2. x=x+1,x+=1,及x++的效率哪个最高,为什么?

    x=x+1: 1.     读取右X的地址 2.     X+1 3.     读取左X的地址 4.     将右值传给左边的X x+=1: 1.     读取右边的x的地址 2.     X+1 3 ...

  3. EPPlus实战篇——Excel写入

    .net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...

  4. Async、Await

    Async.Await:net4.x新增的异步编程方式: 目的:为了简化异步程序编写 Async方式, 使用Async标记Async1为异步方法, 用Await标记GetRequestStreamAs ...

  5. 页面中,禁止html内容被选择

    1.通过css的方式 *{ moz-user-select: -moz-none; -moz-user-select: none; -o-user-select:none; -khtml-user-s ...

  6. MySql查询功能梳理

    CREATE DATABASE CristinMysql Create table employee( eId int(9) not null auto_increment, eName varcha ...

  7. python基础-PyYaml操作yaml文件

    yaml语法 格式 它的基本语法规则如下 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 YAML 支持的数据结构有 ...

  8. RN酷炫组件圆形加载

    地址:https://js.coach/react-native/react-native-circular-progress?search=react-native 别谢我 点个赞就行 ## Use ...

  9. python 安装pip

    https://pip.pypa.io/en/stable/installing/

  10. unbuntu安装Node.js

    在官网https://nodejs.org/en/下载 手动创建链接的话,新安装的angular的ng   typescript的tsc都要自己手动建立软链接,要不就每个工程里npm install一 ...