From 9a9fa0e3170729cccff6cbc78896437d134829d8 Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Sun, 24 Feb 2019 17:41:59 -0300 Subject: [PATCH] Add ENGARRAF Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- algorithms/graph/dijkstra.cpp | 2 +- contests/Cadernaveis/ENGARRAF.cpp | 76 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 contests/Cadernaveis/ENGARRAF.cpp diff --git a/algorithms/graph/dijkstra.cpp b/algorithms/graph/dijkstra.cpp index 3328a27..8b86c90 100644 --- a/algorithms/graph/dijkstra.cpp +++ b/algorithms/graph/dijkstra.cpp @@ -27,7 +27,7 @@ int dijkstra(int o, int d) { wt = i.second; if (dist[v] > dist[u] + wt) { - if (dist[v] != oo) + if (dist[v] != inf) pq.erase(pq.find(ii(dist[v], v))); dist[v] = dist[u] + wt; diff --git a/contests/Cadernaveis/ENGARRAF.cpp b/contests/Cadernaveis/ENGARRAF.cpp new file mode 100644 index 0000000..cb45af8 --- /dev/null +++ b/contests/Cadernaveis/ENGARRAF.cpp @@ -0,0 +1,76 @@ +#include <bits/stdc++.h> + +#define MAX 200 +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define sz size() +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +typedef long long ll; +typedef pair<int,int> ii; + +int dist[MAX]; +vector<ii> graph[MAX]; + +int dijkstra(int o, int d) { + set<ii> pq; + int u, v, wt; + + mset(dist, inf); + + dist[o] = 0; + pq.insert(ii(0, o)); + + while (pq.size() != 0) { + u = pq.begin()->second; + pq.erase(pq.begin()); + + for (auto i : graph[u]) { + v = i.first; + wt = i.second; + + if (dist[v] > dist[u] + wt) { + if (dist[v] != inf) + pq.erase(pq.find(ii(dist[v], v))); + + dist[v] = dist[u] + wt; + pq.insert(ii(dist[v], v)); + } + } + } + + return dist[d]; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int n, m; + while (cin >> n >> m && (n || m)) { + for (int i = 1; i <= n; ++i) graph[i].clear(); + + for (int i = 0; i < m; ++i) { + int o, d, t; cin >> o >> d >> t; + graph[o].pb(ii(d, t)); + } + + int s, t; cin >> s >> t; + int ans = dijkstra(s, t); + cout << ((ans == inf) ? -1 : ans) << ende; + } + + return 0; +} -- GitLab