QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#325206#5613. Road To Savingsape_packTL 1ms3792kbC++233.0kb2024-02-11 05:43:242024-02-11 05:43:24

Judging History

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

  • [2024-02-11 05:43:24]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3792kb
  • [2024-02-11 05:43:24]
  • 提交

answer

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include<set>
#include<cstring>

using namespace std;
typedef long long ll;

struct movestruct {
    int dist, curr, prev, road_id;
    bool operator<(const movestruct& rhs) const {
        return dist < rhs.dist;
    }
};

int main(){
    int n, m, a, b;
    cin >> n >> m >> a >> b;

    vector<vector<pair<int, int>>> adj(n+1);
    vector<int> length(m);
    for(int i = 0; i < n+1; i++){
        vector<pair<int, int>> nv; adj[i] = nv;
    }

    int total_length = 0;

    for(int i = 0; i < m; i++){
        int i1, i2, l;
        cin >> i1 >> i2 >> l;
        i1--; i2--;

        // cout << i1 << " " << i2 << " " << l << endl;

        adj[i1].push_back(make_pair(i2, i));
        adj[i2].push_back(make_pair(i1, i));

        // cout << i1 << " " << i2 << " " << l << endl;

        length[i] = l;
        total_length += l;
    }

    priority_queue<movestruct> pq;
    vector<int> min_dist(n+1, -1);
    vector<set<int>> involved_roads(n+1);

    for(int i = 0; i < n + 1; i++){
        set<int> s; involved_roads[i] = s;
    }

    a--; b--;

    pq.push({0, 0, 0, -1});

    while(!pq.empty()){
        movestruct m = pq.top();
        pq.pop();

        // cout << "Visiting node m: " << m.dist << " " << m.curr << " " << m.prev << " " << m.road_id << endl;
        if(min_dist[m.curr] == m.dist){
            // cout << "       Same as previous length" << endl;

            // Add this information to the set
            min_dist[m.curr] = m.dist;
            involved_roads[m.curr].insert(involved_roads[m.prev].begin(), involved_roads[m.prev].end());
            involved_roads[m.curr].insert(m.road_id);

            for(auto ni: adj[m.curr]){
                // cout << "       Pushing to priority queue: " << ni.first << " with road id: " << ni.second << endl;
                pq.push({m.dist + length[ni.second], ni.first, m.curr, ni.second});
            }
        } else if (min_dist[m.curr] == -1 || min_dist[m.curr] > m.dist) {
            // Overwrite - this path is shorter
            // cout << "           Unseen or shorter path: " << adj[m.curr].size() << endl;
            min_dist[m.curr] = m.dist;
            set<int> new_set; new_set.insert(involved_roads[m.prev].begin(), involved_roads[m.prev].end());
            new_set.insert(m.road_id);
            involved_roads[m.curr] = new_set;

            for(auto ni: adj[m.curr]){
                // cout << "       Pushing to priority queue: " << ni.first << " with road id: " << ni.second << endl;
                pq.push({m.dist + length[ni.second], ni.first, m.curr, ni.second});
            }
        }
    }

    int total_length_paved = 0;

    for(auto i: involved_roads[b]){
        if(i == -1){
            continue;
        }
        total_length_paved += length[i];
        // cout << i << endl;
    }
    cout << total_length - total_length_paved << endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

4 5 1 4
1 2 1
1 3 2
1 4 2
4 2 1
3 4 1

output:

3

result:

ok single line: '3'

Test #2:

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

input:

4 5 1 4
1 2 1
1 3 2
1 4 1
4 2 1
3 4 1

output:

5

result:

ok single line: '5'

Test #3:

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

input:

2 1 1 2
1 2 10

output:

0

result:

ok single line: '0'

Test #4:

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

input:

3 3 1 2
1 2 10
2 3 10
3 1 10

output:

20

result:

ok single line: '20'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3524kb

input:

3 3 1 2
1 2 10
2 3 5
3 1 5

output:

0

result:

ok single line: '0'

Test #6:

score: -100
Time Limit Exceeded

input:

100 4950 74 24
1 2 8
1 3 6
1 4 5
1 5 6
1 6 2
1 7 6
1 8 1
1 9 10
1 10 1
1 11 10
1 12 9
1 13 4
1 14 4
1 15 1
1 16 4
1 17 7
1 18 8
1 19 1
1 20 7
1 21 2
1 22 9
1 23 9
1 24 7
1 25 6
1 26 5
1 27 4
1 28 3
1 29 4
1 30 6
1 31 8
1 32 10
1 33 10
1 34 3
1 35 6
1 36 10
1 37 5
1 38 4
1 39 1
1 40 10
1 41 9
1 42 7
...

output:


result: