题目

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 \times 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;
}

CF 938D Buy a Ticket 题解的更多相关文章

  1. 【最短路】CF 938D Buy a Ticket

    题目大意 流行乐队"Flayer"将在\(n\)个城市开演唱会,这\(n\)个城市的人都想去听演唱会,每个城市的票价不同,于是这些人就想是否能去其他城市听演唱会更便宜,但是去其他的 ...

  2. Codeforces 938D Buy a Ticket

    Buy a Ticket 题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的. 题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了, ...

  3. Codeforces 938D Buy a Ticket (转化建图 + 最短路)

    题目链接  Buy a Ticket 题意   给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$ ...

  4. Codeforces 938D. Buy a Ticket (最短路+建图)

    <题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值= ...

  5. 最短路 || Codeforces 938D Buy a Ticket

    题意:从城市u到v(双向)要花w钱,每个城市看演唱会要花不同的门票钱,求每个城市的人要看一场演唱会花费最少多少(可以在这个城市看,也可以坐车到别的城市看,然后再坐车回来) 思路:本来以为是多源..实际 ...

  6. Codeforces 938D Buy a Ticket 【spfa优化】

    用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是 ...

  7. Buy a Ticket,题解

    题目连接 题意: 没个位置有一个点权,每个边有一个边权,求对于每个点u的min(2*d(u,v)+val[v])(v可以等于u) 分析: 我们想这样一个问题,从u到v的边权*2再加一个点权就完了,我们 ...

  8. Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)

    题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...

  9. HDU 1133 Buy the Ticket (数学、大数阶乘)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. 自己动手写SQL执行引擎

    自己动手写SQL执行引擎 前言 在阅读了大量关于数据库的资料后,笔者情不自禁产生了一个造数据库轮子的想法.来验证一下自己对于数据库底层原理的掌握是否牢靠.在笔者的github中给这个database起 ...

  2. .Net Core 会逆袭成为最受欢迎开发平台吗?

    本文由葡萄城技术团队于博客园原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. .Net Core 是什么? 最新.Net Core 热词霸占了各个 ...

  3. python IDE pycharm的安装与使用

    Python开发最牛逼的IDE——pycharm (其实其它的工具,例如eclipse也可以写,只不过比较麻烦,需要安装很多的插件,所以说pycharm是最牛逼的) pycharm,下载专业版的,不要 ...

  4. How to delete a directory recursively in Java

    在java8或更高版本中,使用NIO API递归删除一个非空目录: try { // 创建stream流 Stream<Path> file = Files.walk(Paths.get( ...

  5. Telegraf和Grafana监控多平台上的SQL Server-自定义监控数据收集

    问题 在上一篇文章中,我们使用Telegraf自带的Plugin配置好了的监控,但是自带的Plugin并不能完全覆盖我们想要的监控指标,就需要收集额外的自定义的监控数据,实现的方法有: 开发自己的Te ...

  6. [web][学习随笔]php中http post&get数据传输

    GET <!--客户端发送--> <form id="form1" action="doGet.php" method="get&q ...

  7. 0.大话Spring Cloud

    天天说Spring cloud ,那到底它是什么? 定义 它不是云计算解决方案 它是一种微服务开发框架 它是(快速构建分布式系统的通用模式的)工具集 它基于Spring boot 构建开发 它是云原生 ...

  8. 【javascript的那些事】等待加载完js后执行方法

    很多时候,你也许会碰到 使用的情景: js文件b.js还没有从服务器端加载到web端,而吧a.js中已经调用了b.js中的方法 实例: 这里是加载echart的时候碰到的具体实例 引入js " ...

  9. oracle 锁表解决方式

    /*查看被锁住的存储过程*/ SELECT * FROM V$DB_OBJECT_CACHE WHERE OWNER = 'APPADMIN' AND LOCKS != '0'; SELECT * F ...

  10. 通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布

    前提 前段时间顺利地把整个服务集群和中间件全部从UCloud迁移到阿里云,笔者担任了架构和半个运维的角色.这里详细记录一下通过Nginx.Consul.Upsync实现动态负载均衡和服务平滑发布的核心 ...