最短路变形题目 HDU多校7
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 −1
InputThere 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.OutputFor 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
题意 : 给你 n 个点,m 条边,在给你一些两点间的路径值,让你求1 - n的最小花费,当你改变航线以后所消耗的权值就会 + 1;
using namespace std;
#define ll long long
const int maxn = 1e5+5;
const int inf = 0x3f3f3f3f; int n, m;
struct node
{
int to, pt, cost, fa;
bool operator< (const node &v)const{
return cost > v.cost;
}
};
priority_queue<node>que;
vector<node>ve[maxn];
bool vis[maxn];
int dis[maxn];
set<int>s[maxn]; void solve(){
while(!que.empty()) que.pop();
for(int i = 1; i <= 100000; i++) s[i].clear();
memset(vis, false, sizeof(vis));
memset(dis, inf, sizeof(dis));
que.push({1, 0, 0, 0});
dis[1] = 0; while(!que.empty()){
node v = que.top(); que.pop(); int x = v.to;
if (v.cost > dis[x]) continue;
for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i].to;
int pt = ve[x][i].pt;
if (to == v.fa) continue;
int cost = 0;
if (s[x].count(pt) == 0) cost++; if (dis[x]+cost < dis[to]) {
dis[to] = dis[x]+cost;
s[to].clear();
s[to].insert(pt);
que.push({to, pt, dis[to], x});
}
else if (dis[x]+cost == dis[to] && s[to].count(pt) == 0){
s[to].insert(pt);
que.push({to, pt, dis[to], x});
}
}
}
if (dis[n] == inf) puts("-1");
else
printf("%d\n", dis[n]);
} int main() {
int u, v, w; while(~scanf("%d%d", &n, &m)){
for(int i = 1; i <= 100000; i++) ve[i].clear();
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &u, &v, &w);
ve[u].push_back({v, w, 0, 0});
ve[v].push_back({u, w, 0, 0});
}
solve();
}
return 0;
}
spfa
using namespace std;
#define ll long long
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f; int n, m;
struct node
{
int to, pt, fa;
//bool operator< (const node& v)const{
//return cost > v.cost;
//}
};
queue<node>que;
vector<node>ve[maxn];
bool vis[maxn];
int dis[maxn];
set<int>s[maxn]; void solve(){
while(!que.empty()) que.pop();
for(int i = 1; i <= 100000; i++) s[i].clear();
memset(vis, false, sizeof(vis));
memset(dis, inf, sizeof(dis));
que.push({1, 0, 0});
dis[1] = 0; vis[1] = 1; while(!que.empty()){
node v = que.front(); que.pop(); int x = v.to;
vis[x] = 0;
for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i].to;
int pt = ve[x][i].pt;
if (to == v.fa) continue;
int cost = 0;
if (s[x].count(pt) == 0) cost++; if (dis[x]+cost < dis[to]) {
dis[to] = dis[x]+cost;
s[to].clear();
s[to].insert(pt);
if (!vis[to]) {
que.push({to, pt, x});
vis[to] = 1;
}
}
else if (dis[x]+cost == dis[to] && s[to].count(pt) == 0){
s[to].insert(pt);
//que.push({to, pt, dis[to], x});
if (!vis[to]) {
que.push({to, pt, x});
vis[to] = 1;
}
}
}
}
if (dis[n] == inf) puts("-1");
else
printf("%d\n", dis[n]);
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int u, v, w; while(~scanf("%d%d", &n, &m)){
for(int i = 1; i <= 100000; i++) ve[i].clear();
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &u, &v, &w);
ve[u].push_back({v, w, 0});
ve[v].push_back({u, w, 0});
}
solve();
}
return 0;
}
最短路变形题目 HDU多校7的更多相关文章
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- 2015 HDU 多校联赛 5363 Key Set
2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...
- 2015 HDU 多校联赛 5317 RGCDQ 筛法求解
2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目 http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...
- HDOJ find the safest road 1596【最短路变形】
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HN0I2000最优乘车 (最短路变形)
HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...
- 洛谷P1144-最短路计数-最短路变形
洛谷P1144-最短路计数 题目描述: 给出一个\(N\)个顶点\(M\)条边的无向无权图,顶点编号为\(1-N\).问从顶点\(1\)开始,到其他每个点的最短路有几条. 思路: \(Dijkstra ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
随机推荐
- python模块之hashlib模块
hashlib模块:提供摘要算法 格式: hashlib格式: obj = hashlib.算法(md5,sha....) obj.update(摘要内容:bytes类型) result = obj. ...
- java 递归(Recursion)
现在要求输出一个给定目录中的全部文件的路径. 本程序肯定只能依靠递归的操作完成,因为在一个给定的路径下有可能还是文件夹,那么如果是文件夹的话则肯定要继续列出,重复判断. 递归:程序调用自身的编程技巧 ...
- 2018-10-22-win10-uwp-自定义控件入门
title author date CreateTime categories win10 uwp 自定义控件入门 lindexi 2018-10-22 09:47:54 +0800 2018-10- ...
- 一图理解vue生命周期
博客园上传图不太清晰,可以查看我的CSDN https://blog.csdn.net/jiaoshuaiai/article/details/90046736 感谢: https://segment ...
- vue权限控制菜单显示
对于不同角色显示不同的菜单 思路1: 本地放一份完整的菜单数据,通过后台返回角色的菜单列表两者对比,筛选需要显示的菜单数据绑定,这里有个问题就是路由vue实例初始化就生成了,加载的全部,人为输入地址是 ...
- 【t088】倒水
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一天辰辰买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着辰辰发现瓶子实在太多了,于是 ...
- koa2--07.cookies的设置和使用
cookies本身在koa中即可直接设置和使用,不需要在安装中间件 //cookies的使用 const koa = require('koa'); var router = require('koa ...
- Linux网络文件共享服务之FTP
一.FTP介绍 ftp(File Transfer Protocol)是早期的三个应用级协议之一,基于C/S结构,双通道协议,数据和命令连接,数据传输格式默认是二进制,当然也支持文件方式传输.默认情况 ...
- CS224n: Natural Language Processing学习准备
cs224n 斯坦福网址,里面包含讲课视频,ppt,代码,学习完后做一个问答系统 http://web.stanford.edu/class/cs224n/index.html 下载anaconda, ...
- 复盘:错误理解zuul路径匹配,无法使用zuul
场景: 项目中用到zuul时,配置url总是有问题,无法路由到对应微服务. 配置如下: zuul: routes: m2-member: path: /member/* serviceId: m2-m ...