HDU 6386 Age of Moyu
Problem Description
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
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
1 2 1
1 3 2
2 3 1
2 0
3 2
1 2 1
2 3 2
-1
2
Source
题解:
最短路的写法;始终去找最小费用,保存上一次的边权和这一次比较,然后查看应当+1还是+0
我是根据https://blog.csdn.net/m0_37611893/article/details/81636688 这个链接而写,
#include <bits/stdc++.h>
using namespace std;
const int MAXN=100010;
const int INF=0x3f3f3f3f;
int n,m;
struct qnode{
int v; //下一个点
int c; //当前花费
int w; //当前这条边的价值
qnode(int _v = 0, int _c = 0, int _w = 0):v(_v),c(_c),w(_w){}
bool operator < (const qnode &r) const{ //按照费用小的在前
return c>r.c;
}
};
struct edge{
int v,cost;
edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<edge>E[MAXN];
int dis[MAXN];
int vis[MAXN];
void dij()
{
for (int i = 0; i <=n ; ++i) {
dis[i]=INF;
}
memset(vis, false, sizeof(vis));
priority_queue<qnode>q;
while(!q.empty()) q.pop();
dis[1]=0;
q.push(qnode(1,0,0));
qnode tmp;
while (!q.empty())
{
tmp=q.top();q.pop();
int u=tmp.v;
if(vis[u]) continue;
vis[u]=true;
dis[u]=tmp.c;
for (int i = 0; i <E[u].size() ; ++i) {
int v=E[u][i].v;
int w1=E[u][i].cost;
if(!vis[v])
{
int temp;
if(tmp.w!=w1) temp=tmp.c+1;//如果这个边的值和上一条边值不一样,费用+1;
else temp=tmp.c;
q.push(qnode(v,temp,w1));
}
}
} }
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i = 0; i <=n ; ++i) {
E[i].clear();
}
int a,b,c;
for (int i = 0; i <m ; ++i) {
scanf("%d%d%d",&a,&b,&c);
E[a].push_back(edge(b,c));
E[b].push_back(edge(a,c));
}
dij();
if(dis[n]==INF) printf("-1\n");
else printf("%d\n",dis[n]);
}
return 0;
}
HDU 6386 Age of Moyu的更多相关文章
- 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 6386 Age of Moyu (重边判断)
本来用一个map判重边结果T了, 实际上可以直接给边上打标记即可 int n, m; struct _ {int to,w,vis;}; vector<_> g[N]; int dis[N ...
- HDU - 6386 Age of Moyu 2018 Multi-University Training Contest 7 (Dijkstra变型)
题意:N个点M条边的无向图,每条边都有属于自己的编号,如果一条路径上的边编号都相同,那么花费仅为1:改变至不同编号的路径,花费加1,无论这个编号之前是否走过. 分析:记录每个点的最小花费,再用set维 ...
- HDU - 6386 Age of Moyu (双端队列+bfs)
题目链接 双端队列跑边,颜色相同的边之间的花费为0,放进队首:不同的花费为1,放进队尾. 用Dijkstra+常数优化也能过 #include<bits/stdc++.h> using n ...
- HDU 6386 Age of Moyu (最短路+set)
<题目链接> 题目大意:给定一张无向图,有n个点m条边,从一条边到另一条边,如果两边的指不同 花费就要+1,如果相同就不需要花费. 先从1走到n问最小花费是多少.(第一条边的花费都是1) ...
- HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij
http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others) Me ...
- hdu6386 Age of Moyu【最短路】
Age of Moyu Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) To ...
- Age of Moyu HDU - 6386 (杭电多校7A)
给出n和点,m条边,每条边有各自的标号,进入第一个标号需要消耗1的费用,此后转换标号需要1费用,在同一个标号上走不需要费用.问你从1到n最少需要多少费用. 最短路变形,把第一个点看成不存在的标号,然后 ...
- Age of Moyu (2018 Multi-University Training Contest 7)
题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ ,f= ...
随机推荐
- Node.js-Webstorm2018配置nodejs
网上都是webstorm老版本的设置方法!根本就找不到以下配置项: 下面介绍2018版的配置方式.功能:使webstrom支持node.js语法检测及语法提示! 例如:配置前,没有任何提示 配置后 配 ...
- 开源框架 epics,开源画面编辑软件 edm
epics Experimental Physics and Industrial Control System 一套开源软件框架,实验物理和工业控制系统 http://www.aps.anl.gov ...
- 调用外部EXE文件
实现效果: 知识运用: Process类的Start方法 实现代码: private void button1_Click(object sender, EventArgs e) { OpenFile ...
- 使用正则表达式验证IP地址
实现效果: 知识运用: 实现代码: public bool validate(string str_IP) { string regex = @"(25[0-5]|2[0-4]\d|[0-1 ...
- python 最简单的web应用(一)
对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. server.py文件 #!/usr/bin/env python # -*- coding: ...
- 检测pycaffe安装好没
进入python然后import caffe,如果没报错就表示安装好了
- 利用python的numpy创建矩阵并对其赋值
创建一个3X3的矩阵并对其赋值: x = numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print x print x.shape 运行结果: [[ ] [ ] [ ] ...
- ceph-块存储客户端
ceph块存储 ceph块设备,以前称为RADOS块设备,为客户机提供可靠性.分布式和高性能的块存储磁盘.RADOS块设备利用librbd库并以顺序的形式在ceph集群的多个osd上存储数据块.RBD ...
- 简单使用hibernate(idea中使用)
首先创建一个maven项目 创建成功后,进行创建数据库的表 CREATE TABLE BOOK( ID INT AUTO_INCREMENT PRIMARY KEY, NAME ), NUMBER i ...
- C#面向对象的编程语言具三个特性
C#面向对象的编程语言具三个特性:有封装性.继承性.多态性 .