最短路变形题目 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 题意: 判断一个简化版的算术表达式是否 ...
随机推荐
- CodeForces 1216C(假的计算几何+扫描线)
传送门 •题意 给你三个矩形,依次编号为 1,2,3: 判断 矩形1 是否被 矩形2 和 矩形3 完全覆盖: 如果没有完全覆盖,输出 "YES",反之,输出 "NO&qu ...
- HDU 6709“Fishing Master”(贪心+优先级队列)
传送门 •参考资料 [1]:2019CCPC网络选拔赛 H.Fishing Master(思维+贪心) •题意 池塘里有 n 条鱼,捕捉一条鱼需要花费固定的 k 时间: 你有一个锅,每次只能煮一条鱼, ...
- java Scanner(简单文本扫描器)
Scanner(File source) 构造一个新的 Scanner,它生成的值是从指定文件扫描的. 备注:实现了Iterable接口 package june6D; import java. ...
- 2018-9-20-断点调试-Windows-源代码
title author date CreateTime categories 断点调试 Windows 源代码 lindexi 2018-09-20 17:37:55 +0800 2018-05-1 ...
- linux 使用 jiffies 计数器
这个计数器和来读取它的实用函数位于 <linux/jiffies.h>, 尽管你会常常只是包含 <linux/sched.h>, 它会自动地将 jiffies.h 拉进来. 不 ...
- IDEA + Spring boot 单元测试
1. 创建测试类 打开IDEA,在任意类名,任意接口名上,按ctrl+shift+t选择Create New Test image 然后根据提示操作(默认即可),点击确认,就在项目的/test/jav ...
- 如何用python“优雅的”调用有道翻译?
前言 其实在以前就盯上有道翻译了的,但是由于时间问题一直没有研究(我的骚操作还在后面,记得关注),本文主要讲解如何用python调用有道翻译,讲解这个爬虫与有道翻译的js“斗争”的过程! 当然,本文仅 ...
- 2018-8-10-win10-uwp-Window.Current.Dispatcher中Current为null
title author date CreateTime categories win10 uwp Window.Current.Dispatcher中Current为null lindexi 201 ...
- FreeSql配合仓储实现软删除
该篇内容由个人博客点击跳转同步更新!转载请注明出处! 前段时间使用FreeSql作为ORM,写了一个简单的CMS,在这里总结一下其中的使用心得. 仓储配合全局过滤器 1. 统一的删除标志 如:数据库字 ...
- Qt、Vc下用fopen打开中文名字的文件(转换成Unicode后,使用_wfopen函数)
在做一个Qt项目的时候,完成上传文件时,通过fopen打开文件用来读时发现fopen不能打开中文的文件名,自己在网查找一下,解决方法如下 参考:http://weidaohang.org/wanglu ...