# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
65971 | ikura355 | 007 (CEOI14_007) | C++14 | 19 ms | 1100 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 5;
const int inf = 1e9;
struct node {
int u,val;
node(int _u = 0, int _val = 0) {
u = _u; val = _val;
}
bool operator < (node a) const {
return a.val<val;
}
};
int n,m,a,b,s1,s2;
vector<int> way[maxn];
int len[maxn];
priority_queue<node> heap;
int d1[maxn], d2[maxn];
int sssp(int u, int ban = 0) {
for(int i=1;i<=n;i++) len[i] = inf;
len[u] = 0;
heap.push(node(u,0));
while(!heap.empty()) {
auto t = heap.top(); heap.pop();
int u = t.u, val = t.val;
if(len[u]!=val) continue;
for(auto v : way[u]) {
if(v!=ban && len[v] > len[u] + 1) {
len[v] = len[u] + 1;
heap.push(node(v,len[v]));
}
}
}
}
int main() {
scanf("%d%d",&n,&m);
scanf("%d%d%d%d",&a,&b,&s1,&s2);
for(int i=1;i<=m;i++) {
int u,v; scanf("%d%d",&u,&v);
way[u].push_back(v); way[v].push_back(u);
}
sssp(a);
for(int i=1;i<=n;i++) d1[i] = len[i];
sssp(b);
for(int i=1;i<=n;i++) d2[i] = len[i];
if(d2[s1]<d1[s1] || d2[s2]<d1[s2]) printf("-1");
else {
int res = min(d2[s1]-d1[s1], d2[s2]-d1[s2]);
sssp(b,a);
int tmp = min(len[s1]-d1[s1], len[s2]-d1[s2]);
if(tmp==res) printf("%d",res-1);
else printf("%d",res);
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |