Untitled

 avatar
unknown
plain_text
a year ago
2.5 kB
8
Indexable
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
#define ll  long long
#define sz(x) (int)x.size()

int dx8[] = {1, -1, 0, 0, 1, -1, -1, 1};
int dy8[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
string dir = "RLDU";
const ll N = 1e5 + 2, M = 900 + 2, MOD = 1e9 + 7, inf = 3e18, inf2 = 1e9;
int n, m, q;
vector<vector<pair<int, ll>>> adj1, adj2;
vector<int> to_sort1, to_sort2;
vector<bool> vis1, vis2;

void dfs(int node, int parent, vector<vector<pair<int, ll>>> &adj, vector<int> &to_sort, vector<bool> &vis) {
    vis[node] = 1;
    for (auto it: adj[node]) {
        if (it.first == parent or vis[it.first])continue;
        dfs(it.first, node, adj, to_sort, vis);
    }
    to_sort.push_back(node);
}

void code() {
    cin >> n >> m >> q;
    adj1 = vector<vector<pair<int, ll>>>(n + 5);
    adj2 = vector<vector<pair<int, ll>>>(n + 5);
    vis1 = vector<bool>(n + 5, 0);
    vis2 = vector<bool>(n + 5, 0);
    for (int i = 0; i < m; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        adj1[u].push_back({v, w});
        adj2[v].push_back({u, w});
    }
    vector<vector<ll>> dp(2, vector<ll>(n + 5, -1));
    dfs(1, 1, adj1, to_sort1, vis1);
    dfs(n, 1, adj2, to_sort2, vis2);
    reverse(to_sort1.begin(), to_sort1.end());
    dp[0][to_sort1[0]] = 0;
    for (auto i: to_sort1) {
        for (auto it: adj1[i]) {
            dp[0][it.first] = max(dp[0][it.first], dp[0][i] + it.second);
        }
    }
    reverse(to_sort2.begin(), to_sort2.end());
    dp[1][to_sort2[0]] = 0;
    for (auto i: to_sort2) {
        for (auto it: adj2[i]) {
            dp[1][it.first] = max(dp[1][it.first], dp[1][i] + it.second);
        }
    }
    while (q--) {
        int node;
        cin >> node;
        if (dp[0][node] == -1 or dp[1][node] == -1)
            cout << "-1\n";
        else
            cout << dp[0][node] + dp[1][node] << '\n';
    }
}

/*
7 9 3
1 2 5
1 3 6
1 4 7
2 5 1
2 6 8
5 7 2
6 7 1
3 2 1
3 7 12
2
3
4

 */
signed main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    int tes = 1;
    // cin >> tes;
    for (int i = 1; i <= tes; ++i) {
        code();
    }
}
Editor is loading...
Leave a Comment