感谢 huangyurui 的支持,他给我 hack 了 3 次。(我真想杀了他)
这是一道可癌的乱搞题。
前置知识
思路
我们考虑字节暴力搜索,如何优化呢?我们定义状态 表示到了 ,高度为 的答案,如果 可以通过向上爬或者向下爬达到 的答案甚至更优,那么我们就不存储 了,我们只有在如果不爬就到达不了下一个点的情况下再爬,否则不爬。
具体实现可以先用 unordered_map 存储每个状态的答案,然后用 set 存储每个 的每个 ,那么每次更新时就先判断 unordered_map 中是否比现在更优秀,再去 set 中找到大于等于或者小于 的第一个,判断从这些节点转移过来是否是更优的,如果更优了,那就不更新了。
我们可以用一个堆,按照当前答案排序,小的先来更新,然后我们为了防止被卡,我们就将边从小到大排序,优先访问边权较小的边,堆边去重,每个 选择较小的权值的边留下来。
然后就通过了(被同学 hack 了),考虑如何优化,我们将每个点新增一个入度,用类似拓扑排序的方法,每次等到入度小于等于 时再将点加入堆,然后第二次操作将所有入度大于 的点加入队列,再在没有限制的情况下搜索。
时间复杂度是玄学的。
Code
#include<bits/stdc++.h>
#define N 100005
#define ll long long
#define pil pair<int,ll>
#define iter set<int>::iterator
#define pii pair<int,int>
#define inf 1000000000000000000ll
using namespace std;
struct node{
int u,x;
ll w;
bool operator < (const node &opt) const {
return w > opt.w;
}
};
struct edge{
int v,w;
};
struct myhash{
size_t operator()(const pil &opt) const {
return 1ull * opt.second * 998244353ull + 1ull * opt.first;
}
};
int n,m,x,dat[N],deg[N];
priority_queue<node> q;
vector<edge> g[N],gg[N];
unordered_map<pil,ll,myhash> dist;
set<int> dis[N];
ll fnd(int u,int x){
iter id = dis[u].lower_bound(x);
ll ans = inf;
if(id != dis[u].end()){
ans = min(ans,(*id - x) + dist[{u,*id}]);
}
if(id != dis[u].begin()){
id --;
ans = min(ans,(x - *id) + dist[{u,*id}]);
}
return ans;
}
pil calc(int u,int v,ll x,ll y){
ll z = x - y;
if(0 <= z and z <= dat[v]){
return {z,y};
}
if(z < 0 and (-z) + x <= dat[u]){
return {0,y + (-z)};
}
if(z > dat[v] and x - z + dat[v] >= 0){
return {dat[v],z - 1ll * dat[v] + y};
}
return {0,inf};
}
bool cmp(edge a,edge b){
return a.w < b.w;
}
bitset<N> vis;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> x;
for(int i = 1;i <= n;i ++){
cin >> dat[i];
}
while(m --){
int u,v,w;
cin >> u >> v >> w;
gg[u].push_back({v,w});
gg[v].push_back({u,w});
}
for(int i = 1;i <= n;i ++){
sort(gg[i].begin(),gg[i].end(),cmp);
for(edge j : gg[i]){
if(vis[j.v] == false){
g[i].push_back({j.v,j.w});
deg[j.v] ++;
vis[j.v] = true;
}
}
for(edge j : gg[i]){
vis[j.v] = false;
}
}
dis[1].insert(x);
dist[{1,x}] = 0;
q.push({1,x,0});
while(q.empty() == false){
node u = q.top();
q.pop();
if(dist[{u.u,u.x}] < u.w){
continue;
}
if(u.u == n){
continue;
}
for(edge e : g[u.u]){
pil res = calc(u.u,e.v,u.x,e.w);
int nowv = res.first;
ll noww = res.second + u.w;
if(noww == inf){
continue;
}
node now = {e.v,nowv,noww};
if(dist.find({e.v,nowv}) != dist.end() and dist[{e.v,nowv}] <= noww){
continue;
}
if(fnd(e.v,nowv) <= noww){
continue;
}
dist[{e.v,nowv}] = noww;
dis[e.v].insert(nowv);
deg[now.u] --;
if(deg[now.u] <= 10){
q.push(now);
}
}
}
for(int i = 1;i <= n;i ++){
if(deg[i] > 10){
for(int j : dis[i]){
q.push({i,j,dist[{i,j}]});
}
}
}
while(q.empty() == false){
node u = q.top();
q.pop();
if(dist[{u.u,u.x}] < u.w){
continue;
}
if(u.u == n){
continue;
}
for(edge e : g[u.u]){
pil res = calc(u.u,e.v,u.x,e.w);
int nowv = res.first;
ll noww = res.second + u.w;
if(noww == inf){
continue;
}
node now = {e.v,nowv,noww};
if(dist.find({e.v,nowv}) != dist.end() and dist[{e.v,nowv}] <= noww){
continue;
}
if(fnd(e.v,nowv) <= noww){
continue;
}
dist[{e.v,nowv}] = noww;
dis[e.v].insert(nowv);
q.push(now);
}
}
ll res = fnd(n,dat[n]);
cout << (res == inf ? -1 : res);
return 0;
}