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 img you have to calculate img, 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·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.

    输出

    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
    

    样例输出1

    6 14 1 25 
    

    样例输入2

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

    样例输出2

    12 10 12 
    

    分析

    其实这个题还是比较简单的,有点类似挖水井的那道题

    我们建一个超级源点,把每个点的点权作为超级源点和该点之间的边权,把原图的边权乘以,再跑最短路就行了,最后输出答案。

    代码

    /*************************************************************************
    	> File Name: h.cpp
    	> Author: LiuGeXian
    	> Mail: 1019630230@qq.com 
    	> Created Time: 2020/5/27 7:15:52
     ************************************************************************/
    
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 2e5 + 5;
    const ll INF = 1e12 + 10;
    struct Node{
    	ll pos, dis;
    	Node(){}
    	Node(ll x, ll y){
    		pos = x;
    		dis = y;
    	}
    	bool operator < (const Node &a)const{
    		return dis > a.dis;
    	}
    };
    struct Edge{
    	ll to, dis, next;
    }edge[maxn << 2];
    int n, m, vis[maxn], cnt, head[maxn];
    ll dis[maxn];
    void Add(ll u, ll v, ll w){
    	edge[++cnt].to = v;
    	edge[cnt].next = head[u];
    	edge[cnt].dis = w;
    	head[u] = cnt;
    }
    void Dij(ll s){
    	priority_queue<Node> q;
    	q.push(Node(s, 0));
    	for (int i = 1; i <= n; i++) dis[i] = INF;
    	dis[s] = 0;
    	while (q.size()){
    		ll d = q.top().dis;
    		ll p = q.top().pos;
    		q.pop();
    		if (vis[p]) continue;
    		vis[p] = 1;
    		for (int i = head[p]; i; i = edge[i].next){
    			int v = edge[i].to;
    			if (dis[v] > d + edge[i].dis){
    				dis[v] = d + edge[i].dis;
    				q.push(Node(v, dis[v]));
    			}
    		}
    	}
    }
    int main(){
    	scanf("%d%d", &n, &m);
    	for (int i = 1; i <= m; i++){
    		ll u, v, w;
    		scanf("%lld%lld%lld", &u, &v, &w);
    		Add(u, v, w << 1);
    		Add(v, u, w << 1);
    	}
    	for (int i = 1; i <= n; i++){
    		ll x;
    		scanf("%lld", &x);
    		Add(0, i, x);
    		Add(i, 0, x);
    	}
    	Dij(0);
    	for (int i = 1; i <= n; i++)
    		printf("%lld ", dis[i]);
    	return 0;
    }
    
    
  • 相关阅读:
    几种 JavaScript 动画库推荐
    微软工程师为你推荐了十本程序员必读书目
    前端新老手必备的34种JavaScript简写优化技术
    Airbnb 爱彼迎 visx 项目介绍
    开源中间件技术支持(5000+元/天)
    C# Byte数组与Int16数组之间的转换(转)
    【636】K.sum 与 np.sum 的区别
    【635】语义分割 label 通道与模型输出通道的
    【634】ndarray 提取行列进行任意变换 & 相关 ndarray 操作
    面试官:设计一个安全的登录都要考虑哪些?我一脸懵逼。。
  • 原文地址:https://www.cnblogs.com/ghosh/p/12972567.html
Copyright © 2011-2022 走看看