QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#701800#5081. Forbidden Turnstamthegod#WA 1ms3768kbC++233.3kb2024-11-02 14:43:432024-11-02 14:43:44

Judging History

你现在查看的是最新测评结果

  • [2024-11-02 14:43:44]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3768kb
  • [2024-11-02 14:43:43]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 5e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;

struct ForbiddenPath {
    int x, y;
};

struct RealVertex {
    int u, w;

    bool operator<(const RealVertex &other) const {
        return (u == other.u)? w > other.w : u > other.u;
    }
};

struct CheckVertex {
    int pre, cur;

    bool operator<(const CheckVertex &other) const {
        return (pre == other.pre)? cur < other.cur : pre < other.pre;
    }
};

struct Vertex {
    int pre, cur, w;

    bool operator<(const Vertex &other) const {
        return w > other.w;
    }
};

int n, m, k;
int source, target;
map<CheckVertex, int> f;
map<RealVertex, int> forbid;
vector<RealVertex> adj[maxN];
priority_queue<Vertex> pq;

void ReadInput()
{
    cin >> m >> n >> k;
    cin >> source >> target;
    for(int i = 1; i <= m; i++) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].pb({v, w});
    }
    for(int i = 1; i <= k; i++) {
        int x, y, z; cin >> x >> y >> z;
        forbid[{x, y}] = z;
    }
}

void Dijkstra() {
    pq.push({-1, source, 0});
    f[{-1, source}] = 0;
    while(!pq.empty()) {
        Vertex u = pq.top();
        pq.pop();
        auto &fu = f[{u.pre, u.cur}];
        if(u.w != fu) continue;
        for(auto v: adj[u.cur]) {
            auto it = forbid.find({u.pre, u.cur});
            //cerr << u.pre << ' ' << u.cur << ' ' << v.u << ' ' << (it != forbid.end()) << '\n';
            if(it != forbid.end() && it->second == v.u) continue;
            //cerr << u.pre << ' ' << u.cur << ' ' << v.u << '\n';
            if(f.find({u.cur, v.u}) == f.end()) {
                auto &fv = f[{u.cur, v.u}];
                fv = fu + v.w;
                pq.push({u.cur, v.u, fv});
                //cerr << u.cur << ' ' << v.u << ' ' << fu << ' ' << v.w << ' ' << fv << '\n';
                continue;
            }
            auto &fv = f[{u.cur, v.u}];
            if(fv > fu + v.w) {
                fv = fu + v.w;
                pq.push({u.cur, v.u, fv});
                continue;
            }
        }
    }
}

void Solve()
{
    if(source == target) {
        cout << 0 << '\n';
        return;
    }
    int res = oo;
    for(int u = 0; u < n; u++) {
        //cerr << u << '\n';
        for(auto v: adj[u]) {
            //cerr << u << ' ' << v.u << ' ' << res << '\n';
            if(v.u == target) {
                if(f.find({u, v.u}) == f.end()) continue;
                res = min(res, f[{u, v.u}]);
                //cerr << u << ' ' << v.u << ' ' << res << '\n';
            }
        }
    }
    if(res == oo) cout << -1 << '\n';
    else cout << res << '\n';
}
#define taskname "sol"
int32_t main()
{
    if (fopen(taskname ".inp", "r"))
    {
        freopen(taskname ".inp", "r", stdin);
        freopen(taskname ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    for(int itest=1; itest<=T; itest++)
    {
        ReadInput();
        Dijkstra();
        Solve();
    }
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3684kb

input:

9 7 3
3 2
6 3 2
3 0 3
0 1 12
1 0 4
1 2 2
1 5 4
4 1 8
5 4 7
5 2 5
0 1 2
4 1 5
1 5 2

output:

36

result:

ok single line: '36'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3624kb

input:

4 4 1
0 3
0 1 2
1 2 3
0 2 7
2 3 10
0 1 2

output:

17

result:

ok single line: '17'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3768kb

input:

4 4 0
0 3
0 1 2
1 2 3
0 2 7
2 3 10

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

4 4 1
1 0
1 2 3
2 3 10
3 2 12
2 0 7
1 2 0

output:

32

result:

ok single line: '32'

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 3680kb

input:

13 8 5
0 5
0 2 3
2 1 7
4 2 10
2 3 10
3 2 12
2 5 10
3 6 10
6 7 5
7 3 5
6 4 10
4 1 0
1 4 10
4 6 10
0 2 1
0 2 5
3 2 5
2 3 2
6 4 2

output:

40

result:

wrong answer 1st lines differ - expected: '63', found: '40'