QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561151 | #8673. 最短路径 | zhulexuan | 0 | 575ms | 219484kb | C++14 | 4.5kb | 2024-09-12 20:41:02 | 2024-09-12 20:41:02 |
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 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]){
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[op][go],op,x,i+1));
break;
}
}
}
ll solve(ll x,ll y){
if (x==y) return 0;
ll i,j;
top = 0;
f[x][1] = f[y][0] = 0;
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 = x; break; }
// if (p.ft>0) insert(p.ft,op,p.nw);
insert(x,op,0);
}
if (pos==-1) 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,top){
ll x = stk[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);
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: 0ms
memory: 16916kb
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: 0
Wrong Answer
time: 4ms
memory: 15836kb
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 385298762 -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 442655345 -1 -1 -1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3627355564 -1 -1 0 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 950457283 -1 -1 -1 -1 -1 -1 -...
result:
wrong answer 14th lines differ - expected: '-1', found: '385298762'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 342ms
memory: 183876kb
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:
264840136 95822142 45803385 65984088 173781811 163700625 16058539 35044348 132350072 89220557 76420932 398731225 333645836 78477841 31574029 111734786 7499773 37568204 110120806 86706574 83752125 71210423 34950584 56816805 81831515 34580363 58057430 469731216 43082289 60292024 35754600 249254869 513...
result:
wrong answer 1st lines differ - expected: '36084543', found: '264840136'
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 36ms
memory: 29984kb
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:
-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:
wrong answer 498th lines differ - expected: '-1', found: '533432631'
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 82ms
memory: 46712kb
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:
-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:
wrong answer 1st lines differ - expected: '21671419385', found: '-1'
Subtask #5:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 91ms
memory: 48380kb
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:
-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:
wrong answer 1st lines differ - expected: '18098332289', found: '-1'
Subtask #6:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 451ms
memory: 216216kb
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:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3251775351 -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 187001938 -1 -1 -1 -1 7545169426 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 4404901195 4975139944 -1 10928120502 252131...
result:
wrong answer 1st lines differ - expected: '1547972368', found: '-1'
Subtask #7:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 575ms
memory: 219484kb
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:
-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:
wrong answer 1st lines differ - expected: '3371897180', found: '-1'