QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561238 | #8673. 最短路径 | zhulexuan | 0 | 59ms | 18952kb | C++14 | 4.8kb | 2024-09-12 21:25:06 | 2024-09-12 21:25:10 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 1e18
#define fr(i,l,r) for (i=(l); i<=(r); i++)
#define rfr(i,l,r) for (i=(l); i>=(r); i--)
template<typename T>inline void read(T &n){
T w=1; n=0; char ch=getchar();
while (!isdigit(ch) && ch!=EOF){ if (ch=='-') w=-1; ch=getchar(); }
while (isdigit(ch) && ch!=EOF){ n=(n<<3)+(n<<1)+(ch&15); ch=getchar(); }
n*=w;
}
template<typename T>inline void write(T x){
if (x==0){ putchar('0'); return ; }
T tmp;
if (x>0) tmp=x;
else tmp=-x;
if (x<0) putchar('-');
char F[105];
long long cnt=0;
while (tmp){
F[++cnt]=tmp%10+48;
tmp/=10;
}
while (cnt) putchar(F[cnt--]);
}
#define Min(x,y) x = min(x,y)
#define Max(x,y) x = max(x,y)
//基础配置=================================================================================
const ll N = 200005, M = 3000005;
ll n,m,qt,seed;
struct edge{
ll y,v;
edge(ll _y=0,ll _v=0){
y = _y; v = _v;
}
bool operator < (const edge o)const{
return v<o.v;
}
};
vector<edge> e[2][N];//0反边,1正边
void generate_edges(int n, int m, unsigned seed) {
std::mt19937 gen(seed);
std::vector<std::tuple<int, int, long long>> edges(m);
unsigned max = -1u / n * n;
auto sample = [&]() {
unsigned x;
do { x = gen(); } while(x >= max);
return x % n + 1;
};
for(auto & [u, v, w] : edges) {
u = sample();
v = sample();
w = gen();
// printf("%lld %lld %lld\n",u,v,w);
e[1][u].push_back(edge(v,w));
e[0][v].push_back(edge(u,w));
} // u 到 v 存在边权为 w 的边
// return edges;
}
struct node{
ll x,v,op,ft,nw;
node(ll _x=0,ll _v=0,ll _op=0,ll _ft=0,ll _nw=0){
x = _x; v = _v; op = _op; ft = _ft; nw = _nw;
}
bool operator < (const node o)const{
return v>o.v;
}
};
priority_queue<node> q;
ll top,stk[N+N];
ll ss,cl[N+N];
ll f[N][2];
void insert(ll x,ll op,ll nw){
for (ll i=nw; i<e[op][x].size(); i++){
ll go = e[op][x][i].y;
if (f[x][op]+e[op][x][i].v<f[go][op]){
if (f[go][0]>=inf && f[go][1]>=inf) cl[++ss] = go;
f[go][op] = f[x][op]+e[op][x][i].v;
// printf("insert f[%lld][%lld] = %lld\n",go,op,f[go][op]);
q.push(node(go,f[go][op],op,x,i+1));
break;
}
}
}
ll solve(ll x,ll y){
if (x==y) return 0;
ll i,j;
ss = top = 0;
f[x][1] = f[y][0] = 0; cl[++ss] = x; cl[++ss] = y;
while (!q.empty()) q.pop();
q.push(node(x,0,1,-1,inf));
q.push(node(y,0,0,-1,inf));
ll pos = -1;
while (!q.empty()){
node p = q.top(); q.pop();
if (p.ft>0) insert(p.ft,p.op,p.nw);
if (p.v>f[p.x][p.op]) continue;
ll x = p.x, op = p.op;
// printf("f[%lld][%lld] = %lld\n",x,op,f[x][op]);
stk[++top] = x;
if (f[x][0]<inf && f[x][1]<inf && pos==-1){ pos = x; break; }
// if (p.ft>0) insert(p.ft,op,p.nw);
insert(x,op,0);
}
if (pos==-1){
fr(i,1,ss){
ll x = cl[i];
f[x][0] = f[x][1] = inf;
}
return -1;
}
ll ans = f[pos][1]+f[pos][0];
fr(i,1,top){
ll x = stk[i], lim;
lim = 2*(f[pos][1]-f[x][1]);
for (j=0; j<e[1][x].size(); j++){
ll go = e[1][x][j].y, v = e[1][x][j].v;
// if (v>lim) break;
// if (f[x][1]+v+f[go][0]<=ans){
// ans = f[x][1]+v+f[go][0];
// printf("ans = %lld , lim = %lld , v = %lld\n",ans,lim,v);
// }
Min(ans,f[x][1]+v+f[go][0]);
}
lim = 2*(f[pos][0]-f[x][0]);
for (j=0; j<e[0][x].size(); j++){
ll go = e[0][x][j].y, v = e[0][x][j].v;
// if (v>lim) break;
// if (f[x][0]+v+f[go][1]<=ans){
// ans = f[x][0]+v+f[go][1];
// printf("ans = %lld , lim = %lld , v = %lld\n",ans,lim,v);
// }
Min(ans,f[x][0]+v+f[go][1]);
}
}
fr(i,1,ss){
ll x = cl[i];
f[x][0] = f[x][1] = inf;
}
return ans;
}
int main(){
// freopen("a.in","r",stdin);
// freopen(".out","w",stdout);
ll i,j;
read(n); read(m); read(qt); read(seed);
if (m>2000) return 0;
generate_edges(n,m,seed);
fr(i,1,n){
if (e[1][i].size()==0) continue;
sort(e[0][i].begin(),e[0][i].end());
sort(e[1][i].begin(),e[1][i].end());
}
fr(i,1,n) f[i][0] = f[i][1] = inf;
while (qt--){
ll x,y;
read(x); read(y);
ll ans = solve(x,y);
printf("%lld\n",ans);
}
return 0;
}
//g++ a.cpp -o a -Wall -Wl,--stack=512000000 -std=c++11 -O2
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 4ms
memory: 18952kb
input:
4 8 5 1112792816 2 3 4 3 4 3 3 2 1 4
output:
3419197189 1798364963 1798364963 3986398077 2337967903
result:
ok 5 lines
Test #2:
score: 5
Accepted
time: 7ms
memory: 18756kb
input:
2000 2000 2000 3336994405 659 1650 1678 341 818 235 1380 1865 1927 1366 1233 1673 267 1698 775 1022 1255 1110 1533 1928 1854 169 1579 729 449 1335 943 583 360 50 795 926 1584 911 1924 604 280 309 1429 420 1107 1858 1466 76 265 1109 1077 622 245 1941 957 1434 1560 1128 122 51 229 925 826 1006 851 323...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 2000 lines
Test #3:
score: 0
Wrong Answer
time: 59ms
memory: 18456kb
input:
1000 2000 2000 1526732796 400 914 837 927 7 271 873 60 934 156 981 329 973 512 276 54 540 278 605 230 681 555 636 706 955 618 640 214 859 696 267 595 38 839 309 12 484 919 746 49 948 337 607 638 438 163 817 869 95 518 534 376 369 331 665 64 736 970 154 41 510 425 876 907 143 803 270 403 350 286 131 ...
output:
14198403396 -1 20203456441 11552404306 16160464812 27144556597 -1 5570702410 -1 19513776618 10597134504 8945453029 20326028889 -1 12608727274 17050357023 -1 -1 15134668738 19589312812 33030045272 16255615559 -1 20150114514 15485138820 -1 5265380455 -1 19291857101 -1 -1 -1 19427108277 17619903738 -1 ...
result:
wrong answer 21st lines differ - expected: '32078322699', found: '33030045272'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 3ms
memory: 14540kb
input:
3000 3000000 10000 37461678 2873 1368 1757 2000 1262 1822 2484 1778 2055 2096 2545 366 2923 2028 1469 1874 691 631 1173 2967 894 2020 1207 881 373 236 1913 1923 1351 16 1066 2032 471 1561 1047 2043 457 145 2728 1752 2521 1199 1568 904 2515 543 1472 2161 748 2744 748 1908 912 172 2340 2494 977 267 10...
output:
result:
wrong answer 1st lines differ - expected: '36084543', found: ''
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 3ms
memory: 13148kb
input:
200000 200000 10000 1824322211 104482 112162 130667 13436 36792 142259 51832 97549 15358 180076 128251 92635 45296 195115 62109 38014 22014 86754 79735 103777 94797 96086 196760 5955 45622 59618 12995 62585 55686 156402 23085 68138 170749 148553 97603 160274 112975 22651 116322 190720 84774 57075 23...
output:
result:
wrong answer 1st lines differ - expected: '-1', found: ''
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 0ms
memory: 14620kb
input:
200000 500000 10000 3113327438 68816 31422 174349 125983 18111 188786 84806 87249 142007 180723 95611 116398 104758 196349 77547 89859 120350 77199 110906 10209 177461 194861 115505 105566 27493 166237 15676 158290 86204 116010 159979 125659 132461 61989 194289 157721 18830 82910 166696 98162 125208...
output:
result:
wrong answer 1st lines differ - expected: '21671419385', found: ''
Subtask #5:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 3ms
memory: 13648kb
input:
200000 500000 10000 1843053063 3409 108359 168924 184622 13119 119837 109492 38050 97152 51201 49047 12472 183998 191613 193074 177289 194248 104409 15509 88499 61967 143398 4532 56790 196650 158711 63655 70744 140178 107299 63530 87330 127334 159237 7134 184418 125289 28604 176966 179527 181695 128...
output:
result:
wrong answer 1st lines differ - expected: '18098332289', found: ''
Subtask #6:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 0ms
memory: 14108kb
input:
100000 3000000 10000 3892765041 14843 34156 43390 49542 38564 95501 26194 87126 18638 53346 69414 47011 95472 58303 44370 77172 75652 90555 94386 31888 47911 9905 70599 97061 52764 24896 31445 15589 82314 43852 97155 93412 11834 45082 75614 42459 67802 32024 82389 4968 32860 62514 97630 28012 14839 ...
output:
result:
wrong answer 1st lines differ - expected: '1547972368', found: ''
Subtask #7:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 4ms
memory: 14804kb
input:
200000 3000000 10000 3910662331 161257 40967 50546 86049 199665 199302 177403 36274 158790 143151 193304 78511 28032 149723 96394 37099 2157 76024 195400 34830 41933 147591 191613 96468 194837 67293 57992 63117 24749 6694 117818 87323 46130 53470 174812 24950 149173 124886 119910 54123 2297 124533 5...
output:
result:
wrong answer 1st lines differ - expected: '3371897180', found: ''