CCPC-Wannafly Winter Camp Day1 (Div2, onsite) F climb climb

Category: Shortest Path [Dijkstra+ Priority Queue Version]
Portal: https://www.zhixincode.com/contest/1/problem/F

Ideas:

/*
At the beginning, it is on the first mountain, so the height of the mountain passed from 1->n will not exceed
t=h[0]+k (k is the initial physical strength value given by the question), which
is higher than t Mountain will contribute (h[i]-t)^2, and this value can be added to the edge weight.

Since the height of each mountain can only be reduced once, the two sides are built separately when building the map.
From the question: n mountains, the height of the mountain is hi, and the weight z of the edge connecting the mountains is <= 100000.
We need to add the square times the value, it should be reasonable to open longlong
*/

tips

  1. About pair<first, second> sorting with first as the key value
  2. The priority queue is sorted in descending order by default, so when storing the distance, you can store the negative number of the distance.
  3. vector push_back({a,b}) is convenient to write

AC code:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=2e+5;
typedef long long ll;
int n,m,k,s,t;
ll dis[maxn],h[maxn];
int vis[maxn];
vector<pair<int,ll>>E[maxn];//i->int 边权为ll
void init()
{
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=n;i++)E[i].clear();
}
void Dijkstra()
{
    priority_queue<pair<ll,int>>q;//以距离为排序键值
    dis[s]=0;
    q.push({-dis[s],s});
    while(!q.empty())
    {
        int now=q.top().second;
        q.pop();
        if(vis[now])continue;
        vis[now]=1;
        for(ll j=0;j<E[now].size();j++)
        {
            int t=E[now][j].first;
            if(!vis[t] && dis[t]>dis[now]+E[now][j].second)
            {
                dis[t]=dis[now]+E[now][j].second;
                q.push({-dis[t],t});
            }
        }
    }
}
int main()
{
    init();
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++) cin>>h[i];
    k+=h[1];//初始体力,也为最大高度
    int a,b;ll z;
    while(m--)
    {
        cin>>a>>b>>z;
        if(h[b]>k) E[a].push_back({b,z+(h[b]-k)*(h[b]-k)});
        else E[a].push_back({b,z});
        if(h[a]>k) E[b].push_back({a,z+(h[a]-k)*(h[a]-k)});
        else E[b].push_back({a,z});
    }
    s=1,t=n;
    Dijkstra();
    cout<<dis[t]<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325686907&siteId=291194637