*ABC 237 E - Skiing(最短路)

https://atcoder.jp/contests/abc237/tasks/abc237_e

题目大意:
给定n个数字,m对边。
如果当前处于上坡状态,则两边的连接数值为:-2*(高的-矮的)【负权边】
如果当前属于下坡状态,则两边的连接数值为:高的-矮的 【正权边】
Sample Input 1  
4 4
10 8 12 5
1 2
1 3
2 3
3 4
Sample Output 1  
3 
Sample Input 2  
2 1
0 10
1 2
Sample Output 2  
0

spfa写法,被wa➕tle爆了。
输麻了

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200200,M=2002;
const int INF=0x3f3f3f3f;
LL n,m,maxn=-INF;
LL v[N],dist[N];
LL e[N],ne[N],w[N],h[N],idx=0;
bool st[N];
void add(LL a,LL b,LL c)
{
    e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
int spfa()
{
    memset(dist,-0x3f,sizeof dist);
    dist[1]=0;
    queue<LL> q;
    q.push(1);
    st[1]=true;
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        st[t]=false;
        for(LL i=h[t];i!=-1;i=ne[i])
        {
            LL j=e[i];
            if(dist[j]<dist[t]+w[i])
            {
                dist[j]=dist[t]+w[i];
                if(!st[j])
                {
                    q.push(j);
                    st[j]=true;
                }
            }
        }
    }
    return dist[n];
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        memset(h,-1,sizeof h);
        memset(v,0,sizeof v);
        cin>>n>>m;
        for(LL i=1;i<=n;i++)
            cin>>v[i];
        while(m--)
        {
            LL x,y,z;
            cin>>x>>y;
            if(v[x]>=v[y]) z=v[x]-v[y];
            else z=2*(v[x]-v[y]);
            add(x,y,z);
        }
        spfa();
        for(LL i=1;i<=n;i++)
        {
            //cout<<dist[i]<<" ";
            maxn=max(maxn,dist[i]);
        }
        cout<<maxn<<endl;
    }
    return 0;
}

正解待补。。。

posted @ 2022-09-08 21:01  高尔赛凡尔娟  阅读(52)  评论(0编辑  收藏  举报