代码改变世界

爬爬爬山

2019-04-22 22:16  一只弱鸡丶  阅读(206)  评论(0编辑  收藏  举报

传送门:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N=1e6;
const int P=1e9+7;
void read(int &a)
{
    a=0;
    int d=1;
    char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch-'0';
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=a*10+ch-'0';
    a*=d;
}
void write(int x)
{
    if(x<0)
        putchar(45),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
int head[N];
struct note
{
    int v,dis,next;
}edge[N];
int num;
void add(int u,int v,int dis)
{
    edge[++num].next=head[u];
    edge[num].v=v;
    edge[num].dis=dis;
    head[u]=num;
}
struct node
{
    ll cost,now,nowh,pos;
    bool operator < (const node & x) const {return x.cost<cost;}
};
priority_queue <node> q;
ll dis[N];
int h[N];
inline void dijstra()
{
    memset(dis,0x3f,sizeof(dis));
    dis[1]=0;
    while(!q.empty())
    {
        node p=q.top();
        q.pop();
        int u=p.pos;
        if(dis[u]<p.cost)
            continue;
        for(re int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].v;
            int w=edge[i].dis;
            ll co=min(0ll,p.now-(h[v]-p.nowh));
            if(dis[v]>dis[u]+w+co*co)
            {
                dis[v]=dis[u]+w+co*co;
                q.push({dis[v],max(0ll,p.now-(h[v]-p.nowh)),h[v]+co,v});
            }
        }
    }
}
int main()
{
    int n,m,k;
    read(n);
    read(m);
    read(k);
    for(re int i=1;i<=n;i++)
        read(h[i]);
    for(re int i=1;i<=m;i++)
    {
        int u,v,w;
        read(u);
        read(v);
        read(w);
        add(u,v,w);
        add(v,u,w);
    }
    q.push({0,k,h[1],1});
    dijstra();
    cout<<dis[n]<<endl;
    return 0;
}