QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#784104#8874. Labelled PathsalexhamidiWA 1ms3616kbC++202.0kb2024-11-26 13:24:372024-11-26 13:24:39

Judging History

This is the latest submission verdict.

  • [2024-11-26 13:24:39]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3616kb
  • [2024-11-26 13:24:37]
  • Submitted

answer

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <tuple>
using namespace std;

int main() {
    int n, m, d, s;
    cin >> n >> m >> d >> s;

    string A;  // All edge labels are substrings of A
    cin >> A;

    vector<vector<pair<string, int>>> adj(n + 1); // {substring, target node}
    for (int i = 0; i < m; i++) {
        int u, v, p, l;
        cin >> u >> v >> p >> l;
        adj[u].push_back({A.substr(p - 1, l), v});
    }

    // Output result for each target node
    for (int target = 1; target <= n; target++) {
        vector<string> dp(n + 1, string(d + 1, 'z')); // DP table for shortest string
        vector<int> parent(n + 1, -1); // To reconstruct paths
        dp[s] = ""; // Start at source with empty string

        priority_queue<tuple<string, int>, vector<tuple<string, int>>, greater<>> pq;
        pq.push({"", s});

        while (!pq.empty()) {
            auto [curStr, u] = pq.top();
            pq.pop();

            if (dp[u] < curStr) continue; // Skip if not optimal

            for (auto [edgeStr, v] : adj[u]) {
                string newStr = curStr + edgeStr;

                if (newStr < dp[v] && newStr.size() <= d) {
                    dp[v] = newStr;
                    parent[v] = u; // Update parent for path reconstruction
                    pq.push({newStr, v});
                }
            }
        }

        // Reconstruct path
        if (dp[target] == string(d + 1, 'z')) {
            cout << 0 << "\n"; // No path found
        } else {
            vector<int> path;
            for (int v = target; v != -1; v = parent[v]) {
                path.push_back(v);
            }
            reverse(path.begin(), path.end());

            cout << path.size() << " ";
            for (int node : path) {
                cout << node << " ";
            }
            cout << "\n";
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3616kb

input:

11 30 105 9
fufuffuffuuuuuufuffuffuufffuufufuuuuuuuuuufffufufffuuufuffufufuuffffuufffuffffuffffufuuuufuufuuffuuuufffu
1 6 51 1
5 2 6 1
9 6 57 3
11 8 86 4
10 8 95 0
6 2 17 0
6 3 78 0
7 3 50 0
11 4 98 3
10 3 77 3
5 4 18 4
7 4 81 1
9 7 82 0
1 3 79 2
7 5 13 4
1 10 86 2
10 4 10 0
9 3 4 0
6 11 10 4
6 4 82...

output:

2 9 1 
3 9 1 2 
2 9 3 
3 9 7 4 
3 9 1 5 
3 9 1 6 
2 9 7 
3 9 3 8 
1 9 
3 9 1 10 
3 9 7 11 

result:

wrong answer wrong solution for vertex 8