2018 Multi-University Training Contest 7 - Age of Moyu
dijkstra
应该是签到题了。。最短路裸题,优先队列维护就行了
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){int X = 0, w = 0; char ch = 0;while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){A ans = 1;for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;return ans;
}
const int N = 100005;
int n, m, cnt, head[N], dist[N];
bool vis[N];
struct Edge { int v, next, id;} edge[N<<2];
struct Node{int s, dis, id;Node(int s, int dis, int id): s(s), dis(dis), id(id){}bool operator < (const Node &rhs) const {return dis > rhs.dis;}
};void addEdge(int a, int b, int c){edge[cnt].v = b, edge[cnt].next = head[a], edge[cnt].id = c, head[a] = cnt ++;
}int dijkstra(){full(dist, INF), full(vis, false);priority_queue<Node> pq;dist[1] = 0, pq.push(Node(1, dist[1], 0));while(!pq.empty()){Node cur = pq.top(); pq.pop();if(vis[cur.s]) continue;vis[cur.s] = true;for(int i = head[cur.s]; i != -1; i = edge[i].next){int u = edge[i].v;if(dist[u] > cur.dis + (cur.id != edge[i].id)){dist[u] = cur.dis + (cur.id != edge[i].id);pq.push(Node(u, dist[u], edge[i].id));}}}return dist[n];
}int main(){FAST_IO;while(cin >> n >> m){full(head, -1), cnt = 0;for(int i = 0; i < m; i ++){int u, v, id;cin >> u >> v >> id;addEdge(u, v, id), addEdge(v, u, id);}int ans = dijkstra();if(ans == INF) cout << "-1" << endl;else cout << ans << endl;}return 0;
}
转载于:https://www.cnblogs.com/onionQAQ/p/10943235.html
2018 Multi-University Training Contest 7 - Age of Moyu相关推荐
- 2018 Multi-University Training Contest 7 Age of Moyu
Age of Moyu 来追梦 Problem Description Mr.Quin love fishes so much and Mr.Quin's city has a nautical sy ...
- HDU 6386 Age of Moyu 2018 Multi-University Training Contest 7(最短路径dijkstra)
Age of Moyu 题意:第一行给出n,m,接下来有m条路,每一行给出 a b c ,从a到b 是c掌控. 若下一条路与上一条路不属于一个c,需要缴费1. 输出从1到N的最小花费,不通则输出-1 ...
- Sichuan University Programming Contest 2018 Preliminary
嗯为了防止大家AK,所以这次的A题和K题我们就当做不存在好了! 经历了昨天写了两个多小时的博客没保存的心态炸裂,今天终于下了个Markdown.所以我猜这篇的格式应该会更好看一点! 好吧废话不多说 题 ...
- 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...
- 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...
- 2021 HZNU Winter Training Day 17 (2018 German Collegiate Programming Contest (GCPC 18))
2021 HZNU Winter Training Day 17 (2018 German Collegiate Programming Contest (GCPC 18)) 题目 A B C D E ...
- 2018 Multi-University Training Contest 7
1001 Age of Moyu (HDU 6386) 思路: 把Dijkstra的vis数组改成set判一判就好了.... //By SiriusRen #include <bits/st ...
- HDU 6091 - Rikka with Match | 2017 Multi-University Training Contest 5
思路来自 某FXXL 不过复杂度咋算的.. /* HDU 6091 - Rikka with Match [ 树形DP ] | 2017 Multi-University Training Conte ...
- HDU 6051 - If the starlight never fade | 2017 Multi-University Training Contest 2
/* HDU 6051 - If the starlight never fade [ 原根,欧拉函数 ] | 2017 Multi-University Training Contest 2 题意: ...
最新文章
- 解决Android的ListView控件滚动时背景变黑
- C++ const 学习
- 08 ORA系列:ORA-01861 文字与格式字符串不匹配
- 时频分析:短时傅立叶变换实现(5)
- sql 数据库中只靠一个数据,查询到所在表和列名
- 重庆市计算机二级证丢失,重庆计算机二级证书丢了怎么办
- 学习笔记----linux下编译samba
- flutter 自定义Tabbar高度和背景色
- C++读取Json生成随机名字
- youphp学习整理
- win10 升级出现0x80004002
- 解决:115网盘下载路径失败
- 帝国cms后台登录系统限制次数,60分钟过后重新登录解决办法
- python 密码库_(01)Python密码库Cryptography探究学习---简介和入门
- 华为免费培养2000名大数据开发者!
- 论文项目复现笔记----Emotion Driven Monocular Face Capture and Animation
- 福尔摩斯基本演绎法第一季/全集Elementary迅雷下载
- 华米科技举办首届AI创新大会 发布“黄山2号”可穿戴芯片
- 项目管理还只会用甘特图?这7种软件让你拉开差距
- 用公式π/4≈1/1-1/3+1/5-1/7+...求π的近似值,直到发现某一项的绝对值小于10的-6次方为止。