← Back to List

1753번: 최단경로 ↗

Solutions

C++14
2.0 KB | 2020 chars
#include <bits/stdc++.h>

#define for1(s,n) for(int i = s; i < n; i++)
#define for1j(s,n) for(int j = s; j < n; j++)
#define foreach(k) for(auto i : k)
#define foreachj(k) for(auto j : k)
#define pb(a) push_back(a)
#define sz(a) a.size()

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef vector <int> iv1;
typedef vector <vector<int>> iv2;
typedef vector <ll> llv1;
typedef unsigned int uint;
typedef vector <ull> ullv1;
typedef vector <vector <ull>> ullv2;

#define MAX 100000
#define INF (ll)1e18

struct edge {
	ll node;
	ll cost;
	bool operator<(const edge &to) const {
		return cost > to.cost;
	}
};

struct WGraph {	
	ll n; 
	vector<vector<edge>> adj;
	vector<ll> prev;
	WGraph(ll n) : n{n}, adj(n+1) {}
	void addEdge(ll s, ll e, ll cost) {
		adj[s].push_back({e, cost});
	}

	void input(ll m) { // 단방향
		ll a, b, c;
		while(m--) {
			cin >> a >> b >> c;
			addEdge(a,b,c);
		}
	}

	void inputD(ll m) { // 양방향
		ll a, b, c;
		while(m--){
			cin >> a >> b >> c;
			addEdge(a,b,c);
			addEdge(b,a,c);
		}
	}

	vector <ll> dijkstra(ll s) {
		vector <ll> dist(n+1, INF);
		prev.resize(n+1, -1);
		priority_queue<edge> pq;
		pq.push({ s, 0ll });
		dist[s] = 0;
		while (!pq.empty()) {
			edge cur = pq.top();
			pq.pop();
			if (cur.cost > dist[cur.node]) continue;
			for (auto &nxt : adj[cur.node])
				if (dist[cur.node] + nxt.cost < dist[nxt.node]) {
					prev[nxt.node] = cur.node;
					dist[nxt.node] = dist[cur.node] + nxt.cost;
					pq.push({ nxt.node, dist[nxt.node] });
				}
		}
		return dist;
	}

	vector<ll> getPath(ll s, ll e) {
		vector<ll> ret;
		ll current = e;
		while(current != -1) {
			ret.push_back(current);
			current = prev[current];
		}
		reverse(ret.begin(), ret.end());
		return ret;
	}

};

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	ll v, e, start;
	cin >> v >> e;
	cin >> start;
	WGraph w(v);
	w.input(e);

	vector<ll> ret = w.dijkstra(start);
	for1(1, v+1) {
		if(ret[i] == INF) cout <<"INF\n";
		else cout << ret[i] <<"\n";
	}
}