最短路变形题目 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 题意: 判断一个简化版的算术表达式是否 ...
随机推荐
- 蝶式套利(butterfly spread)
多头蝶式套利.预期市场价格趋于稳定,希望在这个价格区间内能获利,可选用多头蝶式套利,以较低的议定价格买进一个看涨期权,又以较高的议定价格买进一个看涨期权,同时又以介于上述2个议定价格之间的中等的议定价 ...
- 浅谈Python Django框架
1.Django简介 Python下有多款不同的 Web 框架,Django是最有代表性的一种.许多成功的网站和APP都基于Django. Django是一个开源的Web应用框架,由Python写成. ...
- Vue的第一个实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- async和await的执行顺序问题
说明 : 要了解执行顺序,所需要的知识是了解浏览器js运行机制,以及微任务和宏任务的先后顺序.如果你明白了宏任务.微任务,请往下看: async function async1 () { consol ...
- UVA 12563 "Jin Ge Jin Qu hao" (背包)
传送门 debug了好一会,突然发现,输出错了,emmm......... 明天再写debug历程: (PS:ipad debug是真的繁琐) 题意: 题解: 尽管题干中给的 t 的范围很大,但是 t ...
- P1019 聪聪理扑克
题目描述 聪聪的两个小伙伴灵灵和豪豪喜欢打扑克,什么斗地主.德州.牛牛,他们都玩的有模有样. 但是每次玩好扑克他们都不整理一下,所以整理扑克的任务就交到了聪聪的手上. 已知现在桌面上有 n 张扑克牌, ...
- 解决html2canvas图片跨域合成失败的问题
/** * 将图片转换为base64 * 解决html2canvas跨域合成失败的问题 */ var getBase64Image = function(src, cb) { var img = do ...
- python数据分析经常使用的库
这个列表包含数据分析经常使用的Python库,供大家使用.1. 网络通用urllib -网络库(stdlib).requests -网络库.grab – 网络库(基于pycurl).pycurl – ...
- 一个APP从启动到主页面显示经历了哪些过程?
①点击桌面App图标,Launcher进程采用Binder IPC向system_server进程发起startActivity请求: ②system_server进程接收到请求后,向zygote进程 ...
- int32 无符号范围 -2147483648~2147483647
int32 无符号范围 -2147483648~2147483647