QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#561365#8673. 最短路径zhulexuan0 3ms14716kbC++145.2kb2024-09-12 22:01:272024-09-12 22:01:27

Judging History

你现在查看的是最新测评结果

  • [2024-09-12 22:01:27]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:14716kb
  • [2024-09-12 22:01:27]
  • 提交

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]);
    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: 14280kb

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: 13928kb

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: 0ms
memory: 14716kb

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: 13324kb

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: 13012kb

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
Time Limit Exceeded

Test #24:

score: 0
Time Limit Exceeded

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:


Subtask #7:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 3ms
memory: 13068kb

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: ''