2018 Multi-University Training Contest 7 - Age of Moyu
Posted onionqaq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}
以上是关于2018 Multi-University Training Contest 7 - Age of Moyu的主要内容,如果未能解决你的问题,请参考以下文章
2018 Multi-University Training Contest 2
2018 Multi-University Training Contest 9
2018 Multi-University Training Contest 4
2018 Multi-University Training Contest 4