zoukankan      html  css  js  c++  java
  • 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 you have to calculate , 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.

    Input

    The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105).

    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 ≤ 1012) 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, ... a k (1 ≤ a i ≤ 1012) — price to attend the concert in i-th city.

    Output

    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).

    Input 1

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

    Output 1

    6 14 1 25

    Input 2

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

    Output 2

    12 10 12

    分析

    题意:有n个城市,m条路(双向联通),在每个城市将会有一场精彩的演唱会,每个城市的票价不一样,每条路的路费不一样,你在每个城市都有一个朋友,他们都想看演唱会,求每个朋友的花费最小值(票价+来回路费)

    做法:加一个super源点,把所有的点都跟这个点建一条边,权值就是路费的大小。然后就可以Dij求最短路。答案就是2倍的边权加路费。

    代码

    #include <cstdio>
    #include <queue>
    #include <cstring>
    #define ll long long
    using namespace std;
    const ll maxn = 2e5+5;
    struct Edge{
        int to,d,next;
    }e[maxn<<2];
    ll tot,head[maxn];
    void add(ll x, ll y, ll z) {
        e[++tot].next=head[x];
        head[x] = tot;
        e[tot].to = y; e[tot].d = z;
    }
    priority_queue<pair<ll,ll> > q;
    ll dis[maxn]; 
    bool v[maxn];
    void dij(ll x) {
        memset(dis,0x3f,sizeof(dis));
        dis[x] = 0;
        q.push(make_pair(0,x));
        while (!q.empty()){
            ll u = q.top().second; 
            q.pop();
            if (v[u]) continue;
            v[u] = 1;
            for(ll i=head[u];i;i=e[i].next){
                ll v = e[i].to;
                if (dis[v] > dis[u] + e[i].d) {
                    dis[v] = dis[u] + e[i].d;
                    q.push(make_pair(-dis[v], v));
                }
            }
        }
    }
    int n, m;
    int  main() {
        scanf("%lld%lld", &n, &m);
        for(ll i=1;i<=m;i++) {
            ll x, y, z;
            scanf("%lld%lld%lld", &x, &y, &z);
            add(x, y, 2*z), add(y, x, 2*z);
        }
        for(ll i=1;i<=n;i++){
            ll x;
            scanf("%lld",&x);
            add(0, i, x);
        }
        dij(0);
        for(ll i= 1;i<=n;i++)
            printf("%lld ", dis[i]);
        return 0;
    }
  • 相关阅读:
    第五次站立会议
    第四次站立会议
    迪杰斯特拉算法求最短路径问题
    数组课堂作业
    java2
    Java书写add函数
    《大道至简》第二章(是懒人创造了方法)读后感
    大二暑假周进度报告之四
    大二暑假周进度报告之三
    大二暑假周进度报告之二
  • 原文地址:https://www.cnblogs.com/Vocanda/p/12982185.html
Copyright © 2011-2022 走看看