这段代码能通过所有讨论区和题解提到的 hack 数据。
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline char _read(T &x) {char t=getchar();bool tmp=0;x=0;while(t<'0'||t>'9'){if(t=='-')tmp=1;t=getchar();};while('0'<=t&&t<='9'){x=(x<<1)+(x<<3)+(t^48);t=getchar();}if(tmp)x=-x;return t;}
void read(){}template<typename T,typename ...T2>inline void read(T &x,T2 &...oth) {_read(x);read(oth...);}
template<typename T>inline void read(T *l,T *r) {T *cur=l;while(cur!=r){_read(*cur);++cur;}}
inline void write(){putchar('\n');}
template<typename T>inline void _write(T x) {if(x<0){putchar('-');x=-x;}if(x/10)_write(x/10);putchar((x%10)^48);}
template<typename T,typename ...T2>inline void write(T x,T2 ...oth) {_write(x);putchar(' ');write(oth...);}
#define int long long
const int mod=998244353;
const int N=2e3+3;
const int M=3e5+5;
const int inf=1e18;
int s[4],n;
int head[M],nxt[M*2],to[M*2],v[M*2],tot=-1;
int d[N][4];
struct node {
int x,v;
bool operator()(const node &x,const node &y) {
return x.v>y.v;
}
};
priority_queue<node,vector<node>,node> q;
inline void dij(const int t) {
q.push({s[t],0});
while(!q.empty()) {
node cur=q.top();
q.pop();
if(~d[cur.x][t]) continue;
d[cur.x][t]=cur.v;
for(int i=head[cur.x];~i;i=nxt[i]) {
if(~d[to[i]][t]) continue;
q.push({to[i],cur.v+v[i]});
}
}
}
inline void add(int x,int y,int w) {
nxt[++tot]=head[x];
head[x]=tot;
to[tot]=y;
v[tot]=w;
}
inline bool chk(int x) {
return d[x][0]+d[x][1]==d[s[1]][0]&&d[x][2]+d[x][3]==d[s[3]][2];
}
inline int getlen(int x,int y) {
int ans=d[x][0]-d[y][0];
if(ans*ans!=(d[x][1]-d[y][1])*(d[x][1]-d[y][1])) return -1;
if(ans*ans!=(d[x][2]-d[y][2])*(d[x][2]-d[y][2])) return -1;
if(ans*ans!=(d[x][3]-d[y][3])*(d[x][3]-d[y][3])) return -1;
return abs(ans);
}
signed main() {
#if defined(fre)
fropen();
#endif
memset(head,-1,sizeof head);
memset(nxt,-1,sizeof nxt);
memset(d,-1,sizeof d);
int m;read(n,m);
read(s,s+4);
while(m--) {
int u,v,x;read(u,v,x);
add(u,v,x);
add(v,u,x);
}
dij(0);
dij(1);
dij(2);
dij(3);
int ans=0;
for(int i=1;i<=n;i++)
if(chk(i))
for(int j=1;j<=n;j++)
if(chk(j))
ans=max(ans,getlen(i,j));
write(ans);
#if defined(fre)
frclose();
#endif
return 0;
}//写题偶遇神秘数据,拼尽全力无法战胜。