zoukankan      html  css  js  c++  java
  • CF 938D Buy a Ticket 题解

    题目

    Musicians of a popular band "Flayer" have announced that they are going to "make their exit" with a world tour. Of course, they will visit Berland as well.

    There are n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i-th route can be used to go from city v i to city u i (and from (u_i) to (v_i)), and it costs w i coins to use this route.

    Each city will be visited by "Flayer", and the cost of the concert ticket in i-th city is a i coins.

    You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city i you have to compute the minimum number of coins a person from city (i) has to spend to travel to some city (j) (or possibly stay in city (i)), attend a concert there, and return to city i (if (j ≠ i)).

    Formally, for every (i in [1,n]) you have to calculate (min {2*d_{i,j}}(j in [1, n])) where (d_{i, j}) is the minimum number of coins you have to spend to travel from city (i) to city (j). If there is no way to reach city (j) from city (i), then we consider (d_{i, j}) to be infinitely large.

    输入格式

    The first line contains two integers (n) and (m) (2 ≤ n ≤ 2·10^5, 1 ≤ m ≤ 2·10^5)$.

    Then (m) lines follow, i-th contains three integers (v_i, u_i) and (w_i (1 ≤ v_i, u_i ≤ n, v_i ≠ u_i, 1 ≤ w i ≤ 10^{12})) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each ((v, u)) neither extra ((v, u)) nor ((u, v)) present in input.

    The next line contains (n) integers (a_1, a_2, dots a_k (1 ≤ a_i ≤ 10^{12})) — price to attend the concert in i-th city.

    输出格式

    Print n integers. i-th of them must be equal to the minimum number of coins a person from city (i) has to spend to travel to some city (j) (or possibly stay in city (i)), attend a concert there, and return to city (i) (if (j ≠ i)).

    输入样例1

    4 2
    1 2 4
    2 3 7
    6 20 1 25
    

    输出样例

    6 14 1 25 
    

    输入样例2

    3 3
    1 2 1
    2 3 1
    1 3 1
    30 10 20
    

    输出样例2

    12 10 12 
    

    代码

    (n)个城市,(m)条无向边,点权(a), 边权(w_{i,j})

    对每个节点(i), 找出节点(j), 使得(2 imes d_{i, j} + a_j)最小, (d_{i,j})表示i到j最短路径长度

    乍一看像是多源最短路, 其实可以转化为单源最短路, 乘2不难处理, 关键是加上的终点点权

    我们可以建立一个虚点, 把所有的点(j)到这个虚点建立一条边, 边权为(a_j)

    这样, 求以这个虚点为起点, 到每个点的最短路, 就变成了单源最短路, 使用dijkstra即可

    注意开long long

    代码

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    struct Edge {
        int v, next;
        long long w;
    } edges[1000000];
    int head[300000], tot, vis[250000], n, m, u ,v;
    long long dis[250000], a[230000], w;
    void add(int x, int y, long long w) { edges[++tot] = (Edge){y, head[x], w}, head[x] = tot; }
    struct node {
        int id;
        long long w;
        bool operator<(node b) const  { return w > b.w; }
    };
    void dijkstra(int x) {
        priority_queue<node> queue;
        dis[x] = 0;
        queue.push((node){x, 0});
        while (!queue.empty()) {
            node newn = queue.top();
            queue.pop();
            if (vis[newn.id]) continue;
            vis[newn.id] = 1;
            for (int i = head[newn.id]; i; i = edges[i].next) {
                int v = edges[i].v;
                if (dis[v] > dis[newn.id] + edges[i].w) {
                    dis[v] = dis[newn.id] + edges[i].w;
                    queue.push((node){v, dis[v]});
                }
            }
        }
    }
    int main() {
        memset(dis, 0x3f, sizeof(dis));
        scanf("%d%d", &n, &m);
        for (int i = 0; i < m; i++) scanf("%d%d%lld", &u, &v, &w), add(u, v, 2 * w), add(v, u, 2 * w);
        for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), add(0, i, a[i]);
        dijkstra(0);
        for (int i = 1; i <= n; i++) printf("%lld ", dis[i]);
        return 0;
    }
    
  • 相关阅读:
    c++调用lua
    HTTP实现长连接(TTP1.1和HTTP1.0相比较而言,最大的区别就是增加了持久连接支持Connection: keep-alive)
    C++: std::string 与 Unicode 如何结合?
    统计一下你写过多少代码
    解读jQuery中extend函数
    C#如何通过SOCKET的方式获取HTTPONLY COOKIE
    Java进阶代码
    SQLSERVER聚集索引与非聚集索引的再次研究(上)
    c,c++函数返回多个值的方法
    COM思想的背后
  • 原文地址:https://www.cnblogs.com/youxam/p/cf-938d.html
Copyright © 2011-2022 走看看