题意:从城市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 ;
}

最新文章

  1. (转)String、StringBuffer与StringBuilder之间区别
  2. IOS网络第二天 - 09-多值参数
  3. select 选择的制作
  4. Python开发入门与实战7-Django Form
  5. JS中的event 对象详解
  6. 为什么开发者热衷在Stack Overflow上查阅API文档?
  7. JDBC标准事物编程模式
  8. DLoopDetector回环检测算法
  9. sae中thinkphp使用smarty
  10. JavaScript正则表达式函数总结
  11. iOS 系统通知
  12. Angularjs 通过asp.net web api认证登录
  13. C#读取wav文件
  14. BZOJ 4552 [Tjoi2016&amp;Heoi2016]排序 线段树的分裂和合并
  15. 解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
  16. 解决GP服务产生的结果无法自动发布为地图服务的问题
  17. jq塞入不同状态html的写法 switch (defaults.type)
  18. 记一款bug管理系统(bugdone.cn)的开发过程(4) - 新增BugTalk功能
  19. 【Git】Git与GitHub 入门
  20. Linux IPC 之信号量

热门文章

  1. File System Programming---(三)
  2. (二十六)分类信息的curd-分类信息添加
  3. 探究final在java中的作用
  4. Codeforces - 1114C - Trailing Loves (or L&#39;oeufs?) - 简单数论
  5. Photoshop下载
  6. Hexo瞎折腾系列(3) - 添加GitHub彩带和GitHub Corner
  7. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
  8. Angular4项目,默认的package.json创建及配置
  9. 自己写的MD5加密原码
  10. PT2264解码心得