最短路变形题目 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 题意: 判断一个简化版的算术表达式是否 ...
随机推荐
- 51nod 1380"夹克老爷的逢三抽一"(贪心+set)
传送门 •参考资料 [1]:51Nod-1380-夹克老爷的逢三抽一 •题意 从长度为 n 的数组中抽取 $\frac{n}{3}$ 个不相邻的值使得加和最大(首尾也不能同时取) •题解 贪心选择当前 ...
- H3C 单区域OSPF配置示例二
- 怎样在RxJS Observable中使用Async-Await
怎样在RxJS Observable中使用Async-Await 一般情况下 async-await 和 Observables 并不能“在一起使用”.但RxJS 从一开始就具备与 Promises ...
- javascript异步编程 Async/await
Async/await Async/await 在学习他之前应当补充一定的 promise 知识 它是一种与 promise 相配合的特殊语法,目前被认为是异步编程的终级解决方案 值得我们每一个人学习 ...
- Python3使用过程中需要注意的点
命名规则 变量 变量名只能是数字.字母或下划线的任意组合 变量名的第一个字符不能是数字 不能使用关键字作为变量名 变量的定义要具有可描述性 变量名不宜过长.不宜使用中文.拼音 常量(常用在配置文件中) ...
- 一个vue管理系统的初步搭建总结
ps:目前这个项目只是有一个大致的框架,并没有做完 前期准备工作 前端构建工具:Visual Studio Code后端构建工具:IDEA数据库和服务器构建工具:WampServer (使用的是2.4 ...
- CodeForces 1096D(线性dp)
传送门 •题意 给出一个长度为n的字符串s,对于每个$s_{i}$有$a_{i}$的价值 让你删除最小的价值,使得字符串中不存在$hard$这个子序列 •思路 设dp[1]是不存在以$h$为前缀的最小 ...
- SPOJ - REPEATS Repeats (后缀数组)
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...
- wpf passwobox 添加水印
之前有做过wpf texbox添加水印,这个并不难 重写一下样式就可以了,今天用到了passwordbox 添加水印的时候 发现还是有点难度的. 这个难度就在于如何去取password的长度来控制水印 ...
- 【Kubernetes】容器集群管理常用命令笔记
一.集群部署-查询集群状态 ①查询k8s master各组件健康状态: kubectl get componentstatus ②查询k8s node健康状态: kubectl get node 二. ...