QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561382 | #8673. 最短路径 | zhulexuan | 0 | 7440ms | 217476kb | C++14 | 5.2kb | 2024-09-12 22:05:41 | 2024-09-12 22:05:42 |
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];
bool vis[N][2];
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 (vis[p.x][p.op]) continue;
vis[p.x][p.op] = true;
// printf("v = %lld\n",p.v);
ll x = p.x, op = p.op;
// printf("f[%lld][%lld] = %lld\n",x,op,f[x][op]);
stk[++top] = x;
if (vis[x][0] && vis[x][1] && 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;
vis[x][0] = vis[x][1] = false;
}
return -1;
}
ll ans = f[pos][1]+f[pos][0];
// printf("pos = %lld\n",pos);
// printf("f0 = %lld , f1 = %lld\n",f[pos][0],f[pos][1]);
// printf("top = %lld\n",top);
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 , s --> %lld --> %lld --> t\n",ans,lim,v,x,go);
// }
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 , s --> %lld --> %lld --> t\n",ans,lim,v,go,x);
// // }
// Min(ans,f[x][0]+v+f[go][1]);
// }
}
fr(i,1,ss){
ll x = cl[i];
f[x][0] = f[x][1] = inf;
vis[x][0] = vis[x][1] = false;
}
return ans;
}
int main(){
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
ll i,j;
read(n); read(m); read(qt); read(seed);
if (n!=100000) return 0;
// 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;
memset(vis,false,sizeof(vis));
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: 0
Wrong Answer
time: 0ms
memory: 14072kb
input:
4 8 5 1112792816 2 3 4 3 4 3 3 2 1 4
output:
result:
wrong answer 1st lines differ - expected: '3419197189', found: ''
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 3ms
memory: 14060kb
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: 13532kb
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: 14160kb
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: 4ms
memory: 15128kb
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: 10
Accepted
time: 7440ms
memory: 217476kb
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:
1547972368 1533240012 1192488694 1802115335 1491444021 1888896300 1720188008 1762089620 1815841406 1831208977 1250925907 1756812381 2027344758 1385409721 1937527554 1877583272 1632784703 2090242303 1694524102 1818975564 1429598050 1599437722 2286394605 1416358110 1929044811 2022891575 1487757623 156...
result:
ok 10000 lines
Test #25:
score: 10
Accepted
time: 6567ms
memory: 148164kb
input:
100000 2000000 10000 2082503433 58880 78421 14548 46231 99049 88344 22391 26025 25236 34840 77162 82668 5667 67117 12870 11907 49640 62723 1755 5382 21226 76188 59145 70335 4679 71179 32038 73516 72621 41497 49627 18273 40479 91715 73191 40867 26710 98234 99898 23597 48509 24994 15771 1679 11605 571...
output:
2550292014 2088525319 2688949421 2128205012 3265691684 2456734516 1642691812 2983165881 3021975646 3122679543 2170817246 3179344726 2692378689 2567981687 2298179789 2073907330 2763664628 1855487724 2293201092 2937148401 3601836798 3010987679 3688384387 2780648332 2363790153 2058109458 2361104139 370...
result:
ok 10000 lines
Test #26:
score: 0
Wrong Answer
time: 5423ms
memory: 80108kb
input:
100000 1000000 10000 272241824 59814 46877 53003 67113 92238 72676 61692 32219 21435 50927 52205 42516 15862 43227 81371 46643 23628 77996 17636 78876 35758 42470 56202 76312 91185 74357 8439 64147 30223 82246 36692 51645 77637 81452 11984 6570 85619 99036 17407 42226 88351 11665 66616 99537 49586 7...
output:
6418345560 3595930024 6543274734 5474244226 4520275434 5150335953 4277692210 5986379098 4573937177 7984631087 5980452817 4449908880 5275131238 6897728511 5018007685 6108102390 5945939138 5849450340 3278653602 6392948014 4711245030 5196851535 5369208668 4949967489 4687794608 6120501385 5234779104 587...
result:
wrong answer 6768th lines differ - expected: '7199168146', found: '7237630733'
Subtask #7:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 0ms
memory: 14176kb
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: ''