Untitled

31
mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.5 kB
2
Indexable
Never
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const long long INF = 200000000;

struct WD
{
    long long next;
    long long water;
    long long dist;
};

struct cmp{
    bool operator()(const WD &a, const WD &b){
        if(a.dist!=b.dist){
            return a.dist>b.dist;
        }
            return a.water>b.water;
    }
};

int waters[100001];
long long dp[100001];
long long dp_water[100001];
bool vis[100001];
vector<WD> graph[100001];
int town, road, start, arrive, dis, target;

void dijstra(int start, int target){
    // 거리의 최소 with 물의 max 
    fill(dp, dp+100001, INF);
    dp[start] = 0; // 
    dp_water[start] = waters[start];
    priority_queue<WD, vector<WD>, cmp> pq;
    pq.push({start,dp_water[start],dp[start]});
    while(!pq.empty()){
        WD cur = pq.top();
        long long cur_idx = cur.next;
        long long cur_dist = cur.dist;
        long long cur_water = cur.water;
        pq.pop();
        for(WD next_node : graph[cur_idx]){
            long long next_idx = next_node.next;
            long long next_dis = cur_dist + next_node.dist;
            long long next_wat = cur_water + next_node.water;
            cout << "NOW : " << dp[next_idx] << " " << next_dis << " " << cur_water << " " << dp_water[target]<< endl;
            if(next_dis <= dp[next_idx]){
                dp[next_idx] = next_dis;
                if(next_dis == dp[next_idx] && dp_water[next_idx]< next_wat){
                    dp_water[next_idx] = next_wat;
                    pq.push({next_idx,dp_water[next_idx],next_dis});
                    cout << "!313 " << next_dis << " " << cur_water << " " << dp_water[target]<< endl;
                    continue;
                }
                dp_water[next_idx] = next_wat;
                pq.push({next_idx,next_wat,dp[next_idx]});
                // vis[next_idx] = true;
            }
        }
    }
    cout << dp_water[target] << endl;
    if(dp[target]!=INF){
        cout << dp[target] << " " << dp_water[target] << endl;
        return;
    }
    cout << -1 << endl;
    return;
}

int main(void){
    ios::sync_with_stdio(false);cin.tie(0);
    cin >> town;
    for(int i = 1;i<=town;i++){
        cin >> waters[i];
    }
    cin >> road;
    while(road--){
        cin >> start >>  arrive  >> dis;
        graph[start].push_back({arrive,waters[start],dis});
        graph[arrive].push_back({start,waters[arrive], dis});
    }
    cin >> start >> target;
    dijstra(start,target);
}