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;
    }
    
    
  • 相关阅读:
    开发中常见的七种加密算法及实现
    MySql 函数大全(一)
    MySql 函数大全(二)
    MySql中查询优化方法
    double类型保留一位小数, 其他位数舍弃方法
    java.lang.IllegalArgument,Parse error in application web.xml file at jndi:/localhost/WEB-INF/web.xml
    c#版HOOK微信来了。实时获取微信消息以及公众号文章等
    c#hook微信,实现实时获取微信公众号文章
    C# 未能创建 SSL/TLS 安全通道 和C# 基础连接已经关闭: 发送时发生错误. 解决方案
    .net post一个xml文件到url
  • 原文地址:https://www.cnblogs.com/ghosh/p/12972567.html
Copyright © 2011-2022 走看看