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

instead)

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;

思路分析 : 就是一个正常的最短路变形题目,但是好卡时限啊, 2449ms ,
    基本就是正常的最短路更新,从一个点更新到另一个点时,同时用set记录一下有哪些状态到达了这个点,一旦可以更新,就清空此集合中的全部元素
代码示例 :
  dij
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的更多相关文章

  1. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  2. POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...

  3. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  4. 2015 HDU 多校联赛 5363 Key Set

    2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...

  5. 2015 HDU 多校联赛 5317 RGCDQ 筛法求解

    2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...

  6. HDOJ find the safest road 1596【最短路变形】

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. HN0I2000最优乘车 (最短路变形)

    HN0I2000最优乘车 (最短路变形) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! [试题]为了简化城市公共汽车收费系 ...

  8. 洛谷P1144-最短路计数-最短路变形

    洛谷P1144-最短路计数 题目描述: 给出一个\(N\)个顶点\(M\)条边的无向无权图,顶点编号为\(1-N\).问从顶点\(1\)开始,到其他每个点的最短路有几条. 思路: \(Dijkstra ...

  9. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

随机推荐

  1. vue-cil 打包爬坑(解决)

    1.打包成功不报错,但是打开页面啥都没有?  解决:文件位置:config目录下index.js文件更改assetsPublicPath属性:文件里面有两个assetsPublicPath属性,也就是 ...

  2. Redux 认识之后进阶

    两个东西 action  状态 路由 以及嵌套路由 完整结构   进阶+源代码 源代码在我的 gitHub  存储库里面  https://github.com/Haisenan/Redux2.0

  3. java 反射和泛型-反射来获取泛型信息

    通过指定对应的Class对象,程序可以获得该类里面所有的Field,不管该Field使用private 方法public.获得Field对象后都可以使用getType()来获取其类型. Class&l ...

  4. java 利用TCP上传文件

    从客户端上传到服务器端,其实本质上也就是复制! package july76net; //上传文件(文本) import java.io.BufferedReader; import java.io. ...

  5. P1077 旅行

    题目描述 你要进行一个行程为7000KM的旅行,现在沿途有些汽车旅馆,为了安全起见,每天晚上都不开车,住在汽车旅馆,你手里现在已经有一个旅馆列表,用离起点的距离来标识,如下: 0, 990, 1010 ...

  6. H3C 配置路由器作为FTP客户端

  7. linux虚拟机设置固定IP并实现联网,主机与虚拟机实现互ping

    ifconfig eth0 up 启用第一块网卡 onboot=yes 自动启动 service network restart 重启网络服务 使用虚拟机添加一块桥接网卡 cp eth0 eth1 复 ...

  8. VScode快捷键(最全)

    按 Press 功能 Function Ctrl + Shift + P,F1 显示命令面板 Show Command Palette Ctrl + P 快速打开 Quick Open Ctrl + ...

  9. ES6必须 知道的小知识

    1.函数的默认参数 一般 我们给函数设置默认参数的时候  会在函数里用 || 运算符 比如 function show(width,height ....){ var height = height ...

  10. CSS 高度居中方案

    实现高度自适应并且上下居中 <div id="wrap"> <div class="box">DemoSeat</div> ...