제출 #701652

#제출 시각아이디문제언어결과실행 시간메모리
701652jennierubyjaneRelay Marathon (NOI20_relaymarathon)C++14
17 / 100
228 ms9160 KiB
#include<bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define REP(i, b) for (int i = 0, _b = (b); i < _b; i++)
#define ii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define next ____next
#define prev ____prev
#define left ____left
#define right ___right
#define lcm ___lcm
#define index ____index
using namespace std;

template<class M> M myabs(M x) {
        return x < 0 ? -x : x;
}
template<class M1, class M2> bool minimize(M1 &x, const M2 &y) {
        if (x > y) {x = y; return true;} return false;
}
template<class M1, class M2> bool maximize(M1 &x, const M2 &y) {
        if (x < y) {x = y; return true;} return false;
}

const int INF = (int)1e9 + 7;
const ll INFLL = (ll)1e18 + 7LL;

const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

mt19937 rd(time(0));
/**
ii = pair<int, int>
FOR(i, a, b): a, b == int
**/

#define MAX 5050

int n, m, num_special;
vector<ii> adj[MAX];
int a[MAX];
ll d[MAX][MAX];

void dijkstra(int s, ll d[]) {
        FOR(i, 1, n) d[i] = INFLL;
        d[s] = 0;
        priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
        q.push(mp(d[s], s));
        while (!q.empty()) {
            int u = q.top().se; ll w = q.top().fi; q.pop();
            if (w != d[u]) continue;
            for (auto p : adj[u]) {
                int v = p.se, c = p.fi;
                if (minimize(d[v], d[u] + c)) q.push(mp(d[v], v));
            }
        }

}

bool check(const ii& A, const ii& B) {
        return A.fi != B.fi && A.fi != B.se && A.se != B.fi && A.se != B.se;
}

void solve() {
        cin >> n >> m >> num_special;
        FOR(i, 1, m) {
            int u, v, c; cin >> u >> v >> c;
            adj[u].push_back(mp(c, v));
            adj[v].push_back(mp(c, u));
        }
        FOR(i, 1, num_special) cin >> a[i];
        sort(a + 1, a + 1 + num_special);
        FOR(i, 1, num_special) dijkstra(a[i], d[a[i]]);
        vector<pair<ll, ii>> vec;
        FOR(i, 1, num_special) FOR(j, i + 1, num_special) if (d[a[i]][a[j]] < INFLL)
            vec.push_back(mp(d[a[i]][a[j]], mp(a[i], a[j])));
        sort(ALL(vec)); vec.erase(unique(ALL(vec)), vec.end());
        ll ans = INFLL;
        //for (auto p : vec) cout << p.fi << " " << p.se.fi << " " << p.se.se << "\n";
        REP(i, sz(vec)) FOR(j, i + 1, sz(vec) - 1) if (check(vec[i].se, vec[j].se)) {
            minimize(ans, vec[i].fi + vec[j].fi);
            //cout << vec[i].fi << " " << vec[j].fi << "\n";
            break;
        }
        cout << ans;
}

int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
       
        int t = 1; //cin >> t;
        while (t--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...