2019ccpc network game hdu6705 path

path

Topic Portal

Problem-solving ideas

FIG first with vector memory, then each vector value in accordance with the weight of the edges in ascending order. Each vertex edge as a starting point in the shortest edge into the priority queue. For route information deposited, there should be a starting point, end point, the weight, and the latest addition to the side is the first of several short side of its starting point. According to the priority queue weights from small to large, the current shortest path every time a team for a path, updated two new shortest possible path, which is behind this road together with a minimum of side to go, and this Finally, a side road into a second short side. You will be asked to sort, the answer can be constantly updated.

code show as below

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int N = 100010;

struct T{
    int x, y, f;
    ll w;
    T(int x, int y, ll w, int f): x(x), y(y), w(w), f(f){}
    bool operator<(const T& a)const{
        return w > a.w;
    }
};

struct line{
    int r;
    ll w;
    line(int r, ll w): r(r), w(w){}
    bool operator<(const line& a)const{
        return w < a.w;
    }
};
vector<line> vec[N];
struct R{
    int i, k;
    bool operator<(const R& a)const{
        return k < a.k;
    }
}qy[N];
ll ans[N];
priority_queue<T> pq;

int main()
{
    int t;
    scanf("%d", &t);
    while(t --){
        int n, m, q;
        scanf("%d%d%d", &n, &m, &q);
        for(int i = 1; i <= m; i ++){
            int u, v;
            ll w;
            scanf("%d%d%lld", &u, &v, &w);
            vec[u].push_back(line(v, w));
        }
        for(int i = 1; i <= n; i ++){
            sort(vec[i].begin(), vec[i].end());
        }  
        for(int i = 1; i <= n; i ++){
            if(vec[i].size())
                pq.push(T(i, vec[i][0].r, vec[i][0].w, 0));
        }
        for(int i = 1; i <= q; i ++){
            scanf("%d", &qy[i].k);
            qy[i].i = i;
        }
        sort(qy + 1, qy + q + 1);
        int cnt = 0;
        int id = 1;
        while(!pq.empty()){
            int x = pq.top().x;
            int y = pq.top().y;
            ll w = pq.top().w;
            int f = pq.top().f;
            pq.pop();
            ++cnt;
            while(id <= q && cnt == qy[id].k){
                ans[qy[id].i] = w;
                ++id;
            }
            if(id > q)
                break;
            if(vec[y].size())
                pq.push(T(y, vec[y][0].r, w + vec[y][0].w, 0));
            if(vec[x].size() > f + 1)
                pq.push(T(x, vec[x][f + 1].r, w + vec[x][f + 1].w - vec[x][f].w, f + 1));
        }
        for(int i = 1; i <= q; i ++){
            printf("%lld\n", ans[i]);
        }
        for(int i = 0; i <= n; i ++)
            vec[i].clear();
        while(!pq.empty())
            pq.pop();
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/whisperlzw/p/11403890.html