CCPC-Wannafly Winter Camp Day1 (Div2, onsite) F 爬爬爬山(dijkstra)

9 篇文章 0 订阅
8 篇文章 0 订阅

爬爬爬山

题解:因为降低山需要花费 l ∗ l l * l ll的代价,因此我们可以将这部分花费加到边上。然后跑最短路就好了。

#include<bits/stdc++.h>
#define P pair<LL,int>
typedef long long LL;
using namespace std;
const int N = 2E5+10;
LL dis[N];
priority_queue<P,vector<P>,greater<P> > pq;
vector<P> e[N];
int h[N];
void dijkstra(int s)
{
	memset(dis,0x3f,sizeof dis);
	pq.push(P(dis[s] = 0,s));
	while(!pq.empty()) {
		P cur = pq.top();
		pq.pop();
		int u = cur.second;
		LL cost = cur.first;
		if(dis[u] < cost) continue;
		for(int i = 0; i < e[u].size(); ++i) {
			LL w = e[u][i].first;
			int v  = e[u][i].second;
			if(dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				pq.push(P(dis[v],v));
			}
		}
	}
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n,m,k,x,y,z;
	scanf("%d%d%d",&n,&m,&k);
	for(int i = 0; i < n; ++i) {
		scanf("%d",&h[i + 1]);
	}
	k += h[1];
	for(int i = 0; i < m; ++i) {
		scanf("%d%d%d",&x,&y,&z);
		if(h[x] > k)
			e[y].push_back(P(z + 1LL * (h[x] - k) * (h[x] - k), x));
		else
			e[y].push_back(P(z,x));
		if(h[y] > k)
			e[x].push_back(P(z + 1LL * (h[y] - k) * (h[y] - k), y));
		else
			e[x].push_back(P(z,y));
	}
	dijkstra(1);
	cout << dis[n] << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值