zoukankan      html  css  js  c++  java
  • HDU-6705 Path

    Description

    You have a directed weighted graph with n vertexes and m edges. The value of a path is the sum of the weight of the edges you passed. Note that you can pass any edge any times and every time you pass it you will gain the weight.

    Now there are q queries that you need to answer. Each of the queries is about the k-th minimum value of all the paths.

    Input

    The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of the test cases.
    The first line of each test case contains three positive integers n,m,q. ((1≤n,m,q≤5∗10^4))

    Each of the next m lines contains three integers ui,vi,wi, indicating that the i−th edge is from ui to vi and weighted wi.(1≤ui,vi≤n,1≤wi≤109)

    Each of the next q lines contains one integer k as mentioned above.((1≤k≤5∗10^4))

    It's guaranteed that (Σn ,Σm, Σq,Σmax(k)≤2.5∗10^5) and max(k) won't exceed the number of paths in the graph.

    Output

    For each query, print one integer indicates the answer in line.

    Sample Input

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

    Sample Output

    3
    3
    

    题解

    给定一张有向图,q次询问,每次询问第k小的路径长度。

    离线,预处理出最大的k范围内的所有路径长度。先将所有边按边权排序,用一个set存储当前可以成为答案的边,且set的最大的大小为maxk,每次从set中取出w最小的边,看看能否更新set中的元素,不能更新则break(边权从小到大排序,小边权无法更新之后边权也无法更新),对set中的元素都做一次这样的处理后,我们就得到了[1,maxk]的答案,输出询问即可,复杂度(O(k*log(m+k)))

    AC代码

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 5e4 + 50;
    struct node {
        int v; ll w;
        node (int v = 0, int w = 0): v(v), w(w) {}
        bool operator < (const node &b) const {
            return w < b.w;
        }
    };
    vector<node> G[N];
    struct Edge {
        int u, v; ll w;
        int id;
        Edge(int u = 0, int v = 0, ll w = 0, int id = 0): u(u), v(v), w(w), id(id) {}
        bool operator < (const Edge &b) const {
            if (w == b.w)
                if (u == b.u)
                    if (v == b.v)
                        return id < b.id;
                    else return v < b.v;
                else return u < b.u;
            else return w < b.w;
        }
        bool operator == (const Edge &b) const {
            return w == b.w && u == b.u && v == b.v && id == b.id;
        }
    };
    int Q[N];
    ll ans[N];
    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 <= n; i++) G[i].clear();
            set<Edge> st; st.clear();
            int cnt = 0;
            for (int i = 1; i <= m; i++) {
                int u, v, w;
                scanf("%d%d%d", &u, &v, &w);
                G[u].push_back(node(v, w));
                st.insert(Edge(u, v, w, ++cnt));
            }
            for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end());
            int maxk = 0;
            for (int i = 1; i <= q; i++) {
                scanf("%d", &Q[i]);
                maxk = max(maxk, Q[i]);
            }
            while (st.size() > maxk) st.erase(st.end());
            for (int i = 1; i <= maxk; i++) {
                Edge now = *st.begin();
                st.erase(st.begin());
                ans[i] = now.w;
                if (i == maxk) break;
                int u = now.v;
                for (int j = 0; j < G[u].size(); j++) {
                    int v = G[u][j].v;
                    ll w = G[u][j].w;
                    if (i + st.size() < maxk) st.insert(Edge(now.u, v, now.w + w, ++cnt));
                    else {
                        set<Edge>::iterator it = st.end(); it--;
                        Edge last = *it;
                        if (now.w + w < last.w) {
                            st.erase(it);
                            st.insert(Edge(u, v, now.w + w, ++cnt));
                        }
                        else break;
                    }
                }
            }
            for (int i = 1; i <= q; i++) printf("%lld
    ", ans[Q[i]]);
        }
        return 0;
    }
    
  • 相关阅读:
    jenkins1—docker快速搭建jenkins环境
    UPC-6616 Small Multiple(BFS广搜&双向队列)
    UPC-5502 打地鼠游戏(贪心&优先队列)
    UPC-5500 经营与开发(贪心&逆推)
    NBUT
    UPC-6690 Transit Tree Path(树上最短路径SPFA)
    UPC-6359 售票(字典树)
    UPC-6358 庭师的利刃(两数与运算最大值)
    HDU-6308 Time Zone(时区转换)
    欧拉函数模板及拓展
  • 原文地址:https://www.cnblogs.com/artoriax/p/11453665.html
Copyright © 2011-2022 走看看