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;
    }
    
  • 相关阅读:
    SpringBoot RequestBody ajax提交对象
    微信小程序常用样式汇总
    微信小程序常用控件汇总
    java多线程实现多客户端socket通信
    客户端连接Codis集群
    crontab 解析
    在 RHEL/CentOS 7 上配置NTP时间服务器
    tomcat的bin目录中startup.bat/tomcat.6.exe/tomcat6w.exe区别
    Windows 下tomcat安装及将多个tomcat注册为Windows服务
    Oracle 数据库排错之 ORA-00600
  • 原文地址:https://www.cnblogs.com/youxam/p/cf-938d.html
Copyright © 2011-2022 走看看