Coding Test

BOJ1238_파티 (C++)

Deviloper😈 2022. 9. 21. 16:00
#include <iostream>
#include <queue>
#include <vector>
#include <string.h>
#define INF int(1e9)

using namespace std;

struct Edge {
    int to;
    int cost;
};

struct cmp {
    bool operator() (Edge a, Edge b) {
        if (a.cost <= b.cost) return false;
        else return true;
    }
};

int n, m, x;
vector<Edge> graph[1001];
int d[1001];
int total_d[1001];

void dijkstra(int start) {
    priority_queue<Edge, vector<Edge>, cmp> pq;
    pq.push({start, 0});
    d[start] = 0;

    while(!pq.empty()) {
        Edge cur = pq.top();
        pq.pop();

        if (d[cur.to] < cur.cost) continue;
        
        for(int i = 0; i < graph[cur.to].size(); i++) {
            Edge next = graph[cur.to][i];
            if (d[next.to] <= cur.cost + next.cost) continue;
            d[next.to] = cur.cost + next.cost;
            pq.push({next.to, d[next.to]});
        }
    }
}

int main(void) 
{
    cin >> n >> m >> x;     // 정점 갯수, 간선 갯수, 도착지점
    int s, e, t;
    for (int i = 0; i < m; i++) {
        cin >> s >> e >> t;
        graph[s].push_back({e, t}); // to, cost
    }

    memset(total_d, 0, sizeof(total_d));

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            d[j] = INF;
        }
        dijkstra(i);
        total_d[i] += d[x];
    }
    for (int j = 1; j <= n; j++) {
        d[j] = INF;
    }

    dijkstra(x);

    int res = 0;
    for (int i = 1; i <= n; i++) {
        total_d[i] += d[i];
        if (res < total_d[i]) res = total_d[i];
    }

    cout << res << endl;
}