Codeforces 938D Buy a Ticket
2024-08-27 21:06:31
题意要求:求出每个城市看演出的最小费用, 注意的一点就是车票要来回的。
题解: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 ;
}
最新文章
- magento模板文件结构详解
- Android如何使用NoHttp
- C语言课设心得分享(一)
- UML系列03之UML时序图
- Emit Mapper官方文档
- Backup and Recovery Strategies1
- mysql优化----第一篇:综述
- MPU6050程序(转)
- Struts2之环境配置
- javascript作用域和闭包之我见
- Photoshop给草坪上的人物加上唯美的紫色霞光
- Gedit —— 推荐于NOI系列考试(NOIlinux)的轻量编程环境
- Bypass 360主机卫士SQL注入防御(附tamper脚本)
- 5、AngularJS 直接绑定显示html ($sce、$sanitize服务)
- MongodbHelper
- [Functional Programming] Using Lens to update nested object
- settings.xml配置详解
- css + div 列表布局
- [Cnbeta]企业与家用无线路由器的区别
- [java] DOS编译 .java 文件得到 .class 文件 并执行 以及使用外部 .jar包 时的命令