제출 #1173919

#제출 시각아이디문제언어결과실행 시간메모리
1173919InvMODRelay Marathon (NOI20_relaymarathon)C++20
0 / 100
226 ms28212 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll INF = 1e15;

struct Edge{
    int u,v; ll w;
    Edge(int u = 0, int v = 0, ll w = 0): u(u), v(v), w(w) {}

    bool operator < (const Edge& q) const{
        return w < q.w;
    }
};


int n, m, k, cnt_comp, comp_id[N], near_spec[N];

ll dist[2][N], dist_comp[N];

bool special[N]; pi first_small[N];

Edge E[N]; vector<int> adj[N];

void find_comp(){
    vector<bool> vis(n + 1, false);

    auto do_bfs = [&](int x) -> void{
        queue<int> q;
        q.push(x); vis[x] = true;
        while(!q.empty()){
            int cur = q.front(); q.pop();

            comp_id[cur] = cnt_comp;
            for(int id : adj[cur]){
                int v = cur ^ E[id].u ^ E[id].v;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    };

    FOR(i, 1, n){
        if(!vis[i]) ++cnt_comp, do_bfs(i);
    }

    FOR(i, 1, cnt_comp) dist_comp[i] = INF;
    FOR(i, 1, m){
        int u = E[i].u, v = E[i].v; ll w = E[i].w;
        if(near_spec[u] == near_spec[v]) continue;

        if(ckmn(dist_comp[comp_id[u]], dist[0][u] + w + dist[0][v])){
            first_small[comp_id[u]] = make_pair(near_spec[u], near_spec[v]);
        }
    }
}

void dijkstra(int cid){
    priority_queue<pair<int,ll>, vector<pair<int,ll>>, greater<pair<int,ll>>> pq;

    FOR(i, 1, n){
        dist[cid][i] = INF, dist[cid][i] = INF;
        near_spec[i] = -1;

        if(special[i]){
            dist[cid][i] = 0;
            near_spec[i] = i;
            pq.push(make_pair(i, 0ll));
        }
    }

    while(!pq.empty()){
        pair<int,ll> e = pq.top(); pq.pop();

        if(e.se > dist[cid][e.fi]) continue;

        int x = e.fi;
        for(int id : adj[x]){
            int v = E[id].u ^ E[id].v ^ x;
            if(ckmn(dist[cid][v], dist[cid][x] + E[id].w)){
                pq.push({v, dist[cid][v]});
                near_spec[v] = near_spec[x];
            }
        }
    }
}

void proc_find(){
    FOR(i, 1, cnt_comp){
        int x = first_small[i].fi;
        int y = first_small[i].se;

        if(min(x, y) > 0){
            special[x] = false;
            special[y] = false;
        }
    }

    dijkstra(1);

    FOR(i, 1, cnt_comp){
        int x = first_small[i].fi;
        int y = first_small[i].se;

        if(min(x, y) > 0){
            special[x] = true;
            special[y] = true;
        }
    }
    return;
}

ll get_allcomp(){
    ll answer = INF;
    if(cnt_comp > 1){
        vector<ll> comp_dist;
        FOR(i, 1, cnt_comp){
            comp_dist.pb(dist_comp[i]);
        }

        sort(all(comp_dist));
        answer = comp_dist[0] + comp_dist[1];
    }
    return answer;
}

void solve()
{
    cin >> n >> m >> k;
    FOR(i, 1, m){
        int u,v,w; cin >> u >> v >> w;
        E[i] = Edge(u, v, w);

        adj[u].pb(i), adj[v].pb(i);
    }

    FOR(i, 1, k){
        int x; cin >> x;
        special[x] = true;
    }
    dijkstra(0), find_comp(), proc_find();

    ll answer = get_allcomp();
    FOR(i, 1, m){
        int u = E[i].u, v = E[i].v; ll w = E[i].w;

        if(near_spec[u] == near_spec[v]) continue;
        answer = min(answer, dist[1][u] + dist[1][v] + w + dist_comp[comp_id[u]]);
    }

    cout << answer << "\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:184:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  184 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  185 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...