题目
题意: 给定n个点的点权值hi,m条边。规定有向边(u,v)的边权为
hu - hv (hu > hv)
-2(hu - hv) (hu < hv)
0 (hu = hv)
求点1到其他点的最长路。
思路: 所有边取反然后spfa跑最短路,赛后加强数据以后就不行了。考虑用Dij跑最短路,把所有边取反,但是有负边权,需要换一种建图方式。把所有诸如的(u,v)边都加上 h[u] + h[v].这样就没有负边权了。自己推一下对于势能的影响。 p’ = p + (h[st] - h[ed]).
时间复杂度: O(nlogm)
代码:
// Problem: E - Skiing
// Contest: AtCoder - AtCoder Beginner Contest 237
// URL: https://atcoder.jp/contests/abc237/tasks/abc237_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
if(x<0)
{
putchar('-');
x=-x;
}
if(x>9)
{
write(x/10);
}
putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int h[N],e[N<<1],ne[N<<1],w[N<<1],idx = 0;
int a[N];
void add(int a,int b,int c)
{
e[idx] = b,w[idx] = c,ne[idx] = h[a],h[a] = idx++;
}
int cal(int u,int v)
{
// if(a[u] > a[v]) return a[u] - a[v];
// return 2*(a[u] - a[v]);
if(a[u] > a[v]) return 0;
return abs(a[u] - a[v]);
}
bool vis[N];
ll dist[N];
void Dij(int S)
{
mem(dist,0x3f);
dist[S] = 0;
priority_queue<PII,vector<PII>,greater<PII> >q;
q.push({dist[S],S});
while(q.size())
{
PII tmp = q.top(); q.pop();
int u = tmp.second,dis = tmp.first;
if(dis != dist[u]) continue;
for(int i=h[u];~i;i=ne[i])
{
int j = e[i];
if(dist[j] > dist[u] + w[i])
{
dist[j] = dist[u] + w[i];
q.push({dist[j],j});
}
}
}
}
void solve()
{
mem(h,-1); idx = 0;
read(n);read(m);
for(int i=1;i<=n;++i) read(a[i]);
for(int i=1;i<=m;++i)
{
int x,y; read(x),read(y);
add(x,y,cal(x,y));
add(y,x,cal(y,x));
}
Dij(1);
ll ans = 0;
for(int i=2;i<=n;++i) ans = min(ans,dist[i] - (a[1] - a[i]));
ans *= -1;
write(ans);
}
signed main(void)
{
T = 1;
// OldTomato; cin>>T;
// read(T);
while(T--)
{
solve();
}
return 0;
}