最短路 || Codeforces 938D Buy a Ticket
题意:从城市u到v(双向)要花w钱,每个城市看演唱会要花不同的门票钱,求每个城市的人要看一场演唱会花费最少多少(可以在这个城市看,也可以坐车到别的城市看,然后再坐车回来)
思路:本来以为是多源。。实际上是单源
考虑dij的松弛操作,是每次取队列里值最小的点u(队首),看它能拓展到的点v,如果经过u到v的代价比当前到v的代价低,那么就更新v
这里也同理,只不过代价是路程*2加上在v看演唱会的钱
嗯。。神奇的dij
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
#define SZ 411000
int head[SZ],nxt[SZ],tot = ;
struct edge
{
int t;
LL d;
}l[SZ];
void build(int f,int t,LL d)
{
l[++ tot] = (edge){t,d};
nxt[tot] = head[f];
head[f] = tot;
}
struct node
{
int u;
LL d;
};
bool vis[SZ];
LL dist[SZ];
priority_queue<node> q;
bool operator < (node a, node b) {return a.d > b.d; }
int main()
{
int n, m, v, u;
LL w;
scanf("%d %d", &n, &m);
for(int i = ; i < m; i++)
{
scanf("%d %d %lld", &v, &u, &w);
build(v, u, w);
build(u, v, w);
}
for(int i = ; i <= n; i++) scanf("%lld", &dist[i]), q.push((node){i, dist[i]} );
while(q.size())
{
int u = q.top().u;
q.pop();
if(vis[u]) continue;
vis[u] = ;
for(int i = head[u]; i;i = nxt[i])
{
int v = l[i].t;
if(dist[v] > dist[u] + * l[i].d)
{
dist[v] = dist[u] + * l[i].d;
q.push((node){v, dist[v]});
}
}
}
for(int i = ; i <= n; i++)
{
if(i != ) printf(" ");
printf("%lld", dist[i]);
}
printf("\n");
return ;
}
最短路 || Codeforces 938D Buy a Ticket的更多相关文章
- Codeforces 938D Buy a Ticket (转化建图 + 最短路)
题目链接 Buy a Ticket 题意 给定一个无向图.对于每个$i$ $\in$ $[1, n]$, 求$min\left\{2d(i,j) + a_{j}\right\}$ 建立超级源点$ ...
- Codeforces 938D Buy a Ticket
Buy a Ticket 题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的. 题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了, ...
- Codeforces 938D. Buy a Ticket (最短路+建图)
<题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值= ...
- Codeforces 938D Buy a Ticket 【spfa优化】
用到了网络流的思想(大概).新建一个源点s,所有边权扩大两倍,然后所有的点向s连边权为点权的无向边,然后以s为起点跑spfa(S什么L优化的),这样每个点到s的距离就是答案. 原因的话,考虑答案应该是 ...
- 【最短路】CF 938D Buy a Ticket
题目大意 流行乐队"Flayer"将在\(n\)个城市开演唱会,这\(n\)个城市的人都想去听演唱会,每个城市的票价不同,于是这些人就想是否能去其他城市听演唱会更便宜,但是去其他的 ...
- CF 938D Buy a Ticket 题解
题目 Musicians of a popular band "Flayer" have announced that they are going to "make t ...
- Codeforces 938 D. Buy a Ticket (dijkstra 求多元最短路)
题目链接:Buy a Ticket 题意: 给出n个点m条边,每个点每条边都有各自的权值,对于每个点i,求一个任意j,使得2×d[i][j] + a[j]最小. 题解: 这题其实就是要我们求任意两点的 ...
- 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 ...
- Buy A Ticket(图论)
Buy A Ticket 题目大意 每个点有一个点权,每个边有一个边权,求对于每个点u的\(min(2*d(u,v)+val[v])\)(v可以等于u) solution 想到了之前的虚点,方便统计终 ...
随机推荐
- k8s认证及ServiceAccount-十五
一.ServiceAccount (1)简介 https://www.kubernetes.org.cn/service-account Service account是为了方便Pod里面的进程调用K ...
- bzoj 4975: [Lydsy1708月赛]区间翻转【博弈论】
必败状态是倒序排列,也就是正序对为0 然后发现,每次翻转都是有奇数个数对(\( C_{4x+2}^{2} C_{4x+3}^{2} \) 都是奇数),所以每次翻转丢回改变正反数对的奇偶性 又因为偶数为 ...
- P5166 xtq的口令
传送门 这题要是搞懂在干什么其实不难(虽然某个花了几个小时才搞明白的家伙似乎没资格这么说--) 假设所有人都没有听到老师的命令,我们从左到右考虑,对于当前的人,如果它没有观察者,那么肯定要让它听到老师 ...
- CentOS 7 设置系统语言为英文并解决 cannot change locale 问题
首次安装Cent OS 7.6时,将系统语言设置成了中文.后续学习和使用过程中却发现种种不便,甚至有翻译错误.为锻炼自己的英文能力,所以将系统语言设置问英文. 编辑 locale 配置文件,将 LAN ...
- windows虚拟机下 安装docker 踩过的坑
首先,你的是win7.8还是win10 windows对docker兼容也是最近的事,所以win7.8的话,安装docker是要用工具箱的,不过官方给了下载. 国内话的可以通过,阿里镜像,或者清华镜像 ...
- kettle 导入xml 资源文件
Repository | ExploreRight click the root node of the repositorySelect Import objects from an XML fil ...
- the little schemer 笔记(2)
第二章 Do it, Do it Again, and Again, and Again... 假设l是 (Jack Sprat could eat no chicken fat) 那么 (lat? ...
- nginx添加模块
[root@VM_0_3_centos nginx]# ./sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 ...
- Cake slicing UVA - 1629
UVA - 1629 ans[t][b][l][r]表示t到b行,l到r列那一块蛋糕切好的最小值d[t][b][l][r]表示t到b行,l到r列区域的樱桃数,需要预处理 #include<cst ...
- 系统中同时存在python2和python3时 pip有时候更新后会报错 解决安装的方法如下
官网原链接:https://pip.pypa.io/en/stable/installing/ Installation Do I need to install pip? pip is alread ...