Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Graph Theory 2
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define ui unsigned int #define f(i,n) for(int i = 0; i < n; i++) #define ff(i, l, r) for(int i = l; i < (int)r; i++) #define rf(i, r, l) for(int i = r; i > (int)l; i--) #define pii pair<int, int> #define pll pair<ll, ll> #define F first #define S second #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define vvi vector<vi> #define vvl vector<vl> #define vvpii vector<vii> #define vvpll vector<vll> #define si set<int> #define sl set<ll> #define sii set<pii> #define sll set<pll> #define mii map<int, int> #define mll map<ll, ll> #define mipii map<int, pii> #define mlpii map<int, pll> #define mipll map<ll, pii> #define mlpll map<ll, pll> #define mivi map<int, vi> #define mlvi map<ll, vi> #define mlvl map<ll, vl> #define mci map<char, int> #define mcvi map<char, vi> #define mcvl map<char, vl> #define max_heap_i priority_queue<int> #define max_heap_pii priority_queue<pii> #define min_heap_i priority_queue<int, vector<int>, greater<int>> #define min_heap_pii priority_queue<pii, vector<pii>, greater<pii>> #define pb push_back #define all a.begin(), a.end() #define sz size() #define inf INT_MAX #define llinf LONG_LONG_MAX #define int ll #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); void dfs(vvi graph, int s, bool visited[]) { visited[s] = true; cout << s + 1 << " "; for(int i = 0; i < graph[s].size(); i++) { if(!visited[graph[s][i]]) { dfs(graph, graph[s][i], visited); } } } void bfs(vvi graph, int s, int n) { queue<int> q; q.push(s); bool visited[n]; for(int i = 0; i < n; i++) { visited[i] = false; } visited[s] = true; while(!q.empty()) { int curr = q.front(); q.pop(); cout << curr + 1 << " "; for(int i = 0; i < graph[curr].size(); i++) { if(!visited[graph[curr][i]]) { visited[graph[curr][i]] = true; q.push(graph[curr][i]); } } } } int dijkstra(vvpii graph, int s, int d) { int n = graph.size(); int dist[n]; for(int i = 0; i < n; i++) { dist[i] = 100000; } dist[s] = 0; bool visited[n]; for(int i = 0; i < n; i++) { visited[i] = false; } priority_queue<pii, vii, greater<pii>> q; q.push({dist[s], s}); while(!q.empty()) { pii node = q.top(); q.pop(); int d = node.F; int x = node.S; visited[x] = true; for(int i = 0; i < graph[x].size(); i++) { if(!visited[graph[x][i].F]) { dist[graph[x][i].F] = min(dist[graph[x][i].F], dist[x] + graph[x][i].S); q.push({dist[graph[x][i].F], graph[x][i].F}); } } } /*for(int i = 0; i < n; i++) { cout << dist[i] << " "; }*/ return dist[d]; } void solve() { int n, m; cin >> n >> m; vvpii graph(n); for(int i = 0; i < m; i++) { char u, v; int w; cin >> u >> v >> w; graph[(int)(u - 'A')].pb({(int)(v - 'A'), w}); graph[(int)(v - 'A')].pb({(int)(u - 'A'), w}); } cout << dijkstra(graph, (int)('A' - 'A'), (int)('E' - 'A')); } signed main() { fast; int t = 1; // cin >> t; while(t--) { solve(); } return 0; }
g++
15 19 A B 2 A C 1 A D 5 A F 1 A K 1 C E 3 C D 8 E D 4 E B 1 B F 5 F H 2 F J 7 F G 3 G J 6 G I 4 K M 3 K L 4 K N 2 N O 2
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 2.34 sec, absolute running time: 0.55 sec, cpu time: 0.01 sec, memory peak: 5 Mb, absolute service time: 2,95 sec
edit mode
|
history
|
discussion
3