当前位置:网站首页>练习40,小蓝的旅行【最短路】
练习40,小蓝的旅行【最短路】
2022-08-02 08:32:00 【ILECY】
根据题意,我们知道只能从未做核酸的城市到做核酸的城市或者从做核酸的城市到未做核酸的城市,所以我们可以将每个点分别当成未做核酸的和做了核酸的,然后分别建边求最短路
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define NOTLE ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
#define T int TT;cin >> TT;while (TT--)
#define lint long long
#define pb push_back
#define eb emplace_back
int last[800010],ne[800010],to[800010],w[800010],cnt=0; //要开4倍(无向图+是否做核酸
int n,m,x;
lint d[200010],vis[200010]={0};
void add(int a,int b,int c){ //链式前向星存图
ne[++cnt]=last[a];
to[cnt]=b;
w[cnt]=c;
last[a]=cnt;
}
void SPFA(){
memset(d,INF,sizeof(d));
queue<int> q;
q.push(1);
d[1]=0,vis[1]=1;
while(!q.empty()){
int now=q.front();
q.pop();
vis[now]=0;
for(int i=last[now];i;i=ne[i]){
if(d[to[i]]>d[now]+w[i]){
d[to[i]]=d[now]+w[i];
if(!vis[to[i]]){
q.push(to[i]);
vis[to[i]]=1;
}
}
}
}
}
int main(){
NOTLE;
cin >> n >> m >> x;
for(int i=1;i<=m;i++){
int a,b,c;
cin >> a >> b >> c;
if(a==1){ //一开始是没有做核酸的
add(a,b+n,c+x);
add(b+n,a,c);
}
else{ //由于是无向图存两遍,而且要保证只能从有→无或无→有
add(a+n,b,c);
add(b,a+n,c+x);
add(a,b+n,c+x);
add(b+n,a,c);
}
}
SPFA();
cout << min(d[n],d[n+n]) << endl;
return 0;
}
边栏推荐
- Codeforces Round #811 (Div. 3)无DF
- Flink 程序剖析
- Redis分布式锁入门
- 工程师如何对待开源 --- 一个老工程师的肺腑之言
- LeetCode_2357_使数组种所有元素都等于零
- Write a small game in C (three chess)
- 构建Flink第一个应用程序
- openpyxl 单元格合并
- Redisson distributed lock source code analysis for high-level use of redis
- Technology Cloud Report: To realize the metaverse, NVIDIA starts from building an infrastructure platform
猜你喜欢
[OC学习笔记]weak的实现原理
Write a small game in C (three chess)
OneinStack多版本PHP共存
Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI
Scala类型转换
【开源项目】X-TRACK源码分析
自定义卡包效果实现
查看变量的数据格式
Biotin-C6-amine|N-biotinyl-1,6-hexanediamine|CAS: 65953-56-2
prometheus monitoring mysql_galera cluster
随机推荐
小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?
轴流式水轮机隐私政策
Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
Jenkins--基础--5.4--系统配置--全局工具配置
High imitation [Huawei consumer business official website] and wonderful animation analysis: practice embedding JS code in low-code platform
Spark 系统性学习笔记系列
Codeforces Round #811 (Div. 3)无DF
主流监控系统工具选型及落地场景参考
PostgreSQL学习总结(11)—— PostgreSQL 常用的高可用集群方案
测试时大量TIME_WAIT
A little bit of knowledge - why do not usually cook with copper pots
软件测试技术之解析图灵测试离我们还有多远
Biotin hydrazide HCl|CAS:66640-86-6|Biotin-hydrazide hydrochloride
Hikari连接池源码解读
uvm-phase机制
C语言基础_共用体
王学岗-编译出运行的文件
day_05 time 模块
MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?
积分商城商品供应商选择的三个要求