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+;
struct Edge{
int to,d,next;
}e[maxn<<];
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] = ;
q.push(make_pair(,x));
while (!q.empty()){
ll u = q.top().second;
q.pop();
if (v[u]) continue;
v[u] = ;
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=;i<=m;i++) {
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
add(x, y, *z), add(y, x, *z);
}
for(ll i=;i<=n;i++){
ll x;
scanf("%lld",&x);
add(, i, x);
}
dij();
for(ll i= ;i<=n;i++)
printf("%lld ", dis[i]);
return ;
}
Buy a Ticket 【最短路】的更多相关文章
- Codeforces 938D. Buy a Ticket (最短路+建图)
<题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值= ...
- Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...
- Codeforces 938D Buy a Ticket (转化建图 + 最短路)
题目链接 Buy a Ticket 题意 给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$ ...
- Codeforces 938.D Buy a Ticket
D. Buy a Ticket time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces - 938D-Buy a Ticket+最短路
Buy a Ticket 题意:有n个点和m条路(都收费),n个点在开演唱会,门票不同,对于生活在n个点的小伙伴,要求计算出每个小伙伴为了看一场演唱会要花费的最小价格: 思路: 这道题我一开始觉得要对 ...
- Buy A Ticket(图论)
Buy A Ticket 题目大意 每个点有一个点权,每个边有一个边权,求对于每个点u的\(min(2*d(u,v)+val[v])\)(v可以等于u) solution 想到了之前的虚点,方便统计终 ...
- Buy the Ticket{HDU1133}
Buy the TicketTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【HDU 1133】 Buy the Ticket (卡特兰数)
Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be on sh ...
- 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Buy the Ticket(卡特兰数+递推高精度)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
随机推荐
- 从0开始探究vue-双向绑定原理
理解 vue是一个非常优秀的框架,其优秀的双向绑定原理,mvvm模型,组件,路由解析器等,非常的灵活方便,也使开发者能够着重于数据处理,让开发者更清晰的设计自己的业务. 双向绑定,就是数据变化的时候, ...
- Vue中导出Excel表格方法
本文记录一下在Vue中实现导出Excel表格的做法.参考度娘上各篇博客,最后实现功能 Excel表格,我的后端返回的是数据流,然后文件名是放进了content-disposition中,前端进行获取. ...
- (Java实现)洛谷 P1164 小A点菜
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...
- Java实现蓝桥杯历届试题兰顿蚂蚁
历届试题 兰顿蚂蚁 时间限制:1.0s 内存限制:256.0MB 提交此题 问题描述 兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种. 平面上的正方形格子被填上黑色或白色.在其 ...
- Java实现 蓝桥杯VIP 算法提高 打水问题
算法提高 打水问题 时间限制:1.0s 内存限制:512.0MB 问题描述 N个人要打水,有M个水龙头,第i个人打水所需时间为Ti,请安排一个合理的方案使得所有人的等待时间之和尽量小. 输入格式 第一 ...
- java中装箱和拆箱的详细使用(详解)
一.什么是装箱?什么是拆箱? 在前面的文章中提到,Java为每种基本数据类型都提供了对应的包装器类型,至于为什么会为每种基本数据类型提供包装器类型在此不进行阐述,有兴趣的朋友可以查阅相关资料.在Jav ...
- java类加载器是什么?
类加载器是有了解吗? 解析:底层原理的考察,其中涉及到类加载器的概念,功能以及一些底层的实现. 答:顾名思义,类加载器(class loader)用来加载 Java 类到 Java 虚拟机中.一般来说 ...
- java实现矩阵变换加密法
一种Playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重复,且都为小写字母),然后与字母表中其他字母一起填入至一个5x5的方阵中,填入方法如下: 1.首先按行填入密钥串 ...
- Swift 语法总结
1,用 var 定义变量 ,与js类似. let 用于定义常量,定义完后不能修改. var 用于定义变量,可以修改. swift可以自动识别属性类别. 2,使用 import 语句来引入任何的 Obj ...
- PHP 安装 XDebug
下载XDebug扩展 下载对应PHP版本的Xdebug 线程安全(TS)和非线程安全(NTS) 安装Xdebug扩展-php.ini [XDebug] xdebug.profiler_output_d ...