제출 #1357792

#제출 시각아이디문제언어결과실행 시간메모리
1357792nathako9nEvacuation plan (IZhO18_plan)C++20
22 / 100
4093 ms17560 KiB
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int N = 1e5+5;
int n,m,k,q;
vector<char> ans;
vector<pair<ll,ll>>vec[N+3];
vector<int>np;
ll val[N+3];
ll sol(int s,int e){
    vector<ll>dp(n+2,-1e18);
    dp[s]=val[s];
    priority_queue<tuple<ll,ll>,vector<tuple<ll,ll>>,greater<tuple<ll,ll>>>pq;
    pq.push({val[s],s});
    while(!pq.empty()){
        auto[cw,cur]=pq.top();
        pq.pop();
        if(dp[cur]>cw)continue;
        for(auto [nx,nw]:vec[cur]){
            ll tw=min(cw,val[nx]);
            if(dp[nx]<tw){
                dp[nx]=tw;
                pq.push({tw,nx}); 
            }
        }
    }    
    return dp[e];
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y,w;cin>>x>>y>>w;
        vec[x].emplace_back(y,w);
        vec[y].emplace_back(x,w);
    }
    for(int i=1;i<=n;i++){
        val[i]=1e18;
    }
    priority_queue<tuple<ll,ll>,vector<tuple<ll,ll>>,greater<tuple<ll,ll>>>pq;
    cin>>k;
    for(int i=1;i<=k;i++){
        int x;
        cin>>x;
        np.emplace_back(x);
        val[x]=0;
        pq.push({0,x});
    }
    while(!pq.empty()){
        auto[cw,cur]=pq.top();
        pq.pop();
        if(val[cur]<cw)continue;
        for(auto [nx,nw]:vec[cur]){
            ll tw=cw+nw;
            if(val[nx]>tw){
                val[nx]=tw;
                pq.push({tw,nx}); 
            }
        }
    }
    cin>>q;
    while(q--){
        int s,e;cin>>s>>e;
        cout<<sol(s,e)<<endl;
    }
    return 0;
}
/*

9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…