Buy a Ticket

题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的。

题解:dijkstra 生成优先队列的时候直接将在本地城市看演出的费用放入队列里, 然后直接跑就好了,  dis数组存的是, 当前情况下的最小花费是多少。

代码:

 #include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstdio>
#define LL long long
#define ULL unsigned LL
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
using namespace std;
typedef pair<LL, int> pll;
const int N = 2e5+;
LL dis[N];
int head[N];
struct Node{
int to;
int nt;
LL ct;
}e[N<<];
priority_queue<pll, vector<pll>, greater<pll> > q;
void dijkstra(){
while(!q.empty()){
int u = q.top().se;
LL w = q.top().fi;
q.pop();
if(dis[u] != w) continue;
for(int i = head[u]; ~i; i = e[i].nt){
int v = e[i].to;
if(dis[v] > dis[u] + e[i].ct){
dis[v] = dis[u] + e[i].ct;
q.push(pll(dis[v],v));
}
}
}
}
int tot = ;
void add(int u, int v, LL w){
e[tot].ct = w;
e[tot].to = v;
e[tot].nt = head[u];
head[u] = tot++;
}
int main(){
ios::sync_with_stdio(); cin.tie(); cout.tie();
memset(head, -, sizeof(head));
int n, m;
cin >> n >> m;
int u, v;
LL ct;
for(int i = ; i <= m; i++){
cin >> u >> v >> ct;
add(u,v,ct*);
add(v,u,ct*);
}
for(int i = ; i <= n; i++){
cin >> ct;
q.push(pll(ct,i));
dis[i] = ct;
}
dijkstra();
for(int i = ; i < n; i++){
cout << dis[i] << ' ';
}
cout << dis[n] << endl;
return ;
}

最新文章

  1. magento模板文件结构详解
  2. Android如何使用NoHttp
  3. C语言课设心得分享(一)
  4. UML系列03之UML时序图
  5. Emit Mapper官方文档
  6. Backup and Recovery Strategies1
  7. mysql优化----第一篇:综述
  8. MPU6050程序(转)
  9. Struts2之环境配置
  10. javascript作用域和闭包之我见
  11. Photoshop给草坪上的人物加上唯美的紫色霞光
  12. Gedit —— 推荐于NOI系列考试(NOIlinux)的轻量编程环境
  13. Bypass 360主机卫士SQL注入防御(附tamper脚本)
  14. 5、AngularJS 直接绑定显示html ($sce、$sanitize服务)
  15. MongodbHelper
  16. [Functional Programming] Using Lens to update nested object
  17. settings.xml配置详解
  18. css + div 列表布局
  19. [Cnbeta]企业与家用无线路由器的区别
  20. [java] DOS编译 .java 文件得到 .class 文件 并执行 以及使用外部 .jar包 时的命令

热门文章

  1. 【SVN】eclipse 安装 SVN 插件
  2. JAVA-Spring AOP基础 - 代理设计模式
  3. 【python-django后端开发】Redis缓存配置使用详细教程!!!
  4. MOCTF-MISC-writeup
  5. Struts完成用户新增操作
  6. ceph 初始化函数解析
  7. 自定义 Button 选择器
  8. 基于Spring框架应用的权限控制系统的研究和实现
  9. PythonDay05
  10. Maven安装配置及其插件m2e(Eclipse Indigo 和 MyEclipse8.5)的安装配置