2022河南萌新联赛第(三)场:河南大学 I - 旅行

I - 旅行

题意:从城市 1 到城市 n, 每隔一天多花费 w。
解法:将每个城市看成两个城市, 缴费的, 未缴费的, 然后让缴费于未缴费的城市建边, 跑最短路即可。

  • 时间复杂度 O ( n × l o g ( n ) ) O(n × log(n)) O(n×log(n))
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,PII> PLII;
const int N = 100010;

vector<PII> g[N];
bool st[N][2];
LL d[N][2];
int n,m,x;

void dijkstra()
{
	memset(d,0x3f,sizeof d);
	priority_queue<PLII,vector<PLII>,greater<PLII> > Q;//Q.first -> d, Q.second.first -> didian, Q.second.second -> hesuan
	d[1][0]=0;
	Q.push({0,{1,0}});
	while(!Q.empty())
	{
		auto t=Q.top();Q.pop();
		int u=t.second.first,ok=t.second.second;
		if(st[u][ok]) continue;
		st[u][ok]=true;
		for(auto &p:g[u])
		{
			int j=p.first,w=p.second;
			if(ok==1 && d[j][0]>d[u][1]+w) // 0 -> 1
			{
				d[j][0]=d[u][1]+w;
				Q.push({d[j][0],{j,0}});
			}
			if(ok==0 && d[j][1]>d[u][0]+w+x)// 1 -> 0,  + x;
			{
				d[j][1]=d[u][0]+w+x;
				Q.push({d[j][1],{j,1}});
			}
		}
	}
}

int main()
{
	cin>>n>>m>>x;
	for(int i=1;i<=m;i++)
	{
		int u,v,w;cin>>u>>v>>w;
		g[u].push_back({v,w});
		g[v].push_back({u,w});
	}
	dijkstra();
	cout<<min(d[n][0],d[n][1])<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值