QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#325177#5613. Road To Savingsape_packRE 0ms3856kbC++232.9kb2024-02-11 05:27:052024-02-11 05:27:05

Judging History

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

  • [2024-02-11 05:27:05]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3856kb
  • [2024-02-11 05:27:05]
  • 提交

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(m);
    vector<int> length(n);
    for(int i = 0; i < m; 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));

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

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

    for(int i = 0; i < m; 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: 3564kb

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: 0ms
memory: 3856kb

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: -100
Runtime Error

input:

2 1 1 2
1 2 10

output:


result: