QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#371462#7991. 最小环24bot#WA 811ms288272kbC++144.9kb2024-03-30 12:46:192024-03-30 12:46:53

Judging History

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

  • [2024-03-30 12:46:53]
  • 评测
  • 测评结果:WA
  • 用时:811ms
  • 内存:288272kb
  • [2024-03-30 12:46:19]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
bool stmemory;
namespace Call_me_Eric{
inline int read(){
    int x = 0, f = 1;char ch = getchar();
    while(ch < '0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}
    return x * f;
}
const int maxn = 4e5 + 10, INF = 0x3f3f3f3f3f3f3f3f, maxp = 5e3 + 10;
int n, m;
bool book2[maxn];

int head[maxn], tot;
struct edge{
    int to, nexte, wei;
    edge(int to = 0,int ne = 0,int we = 0):to(to),nexte(ne),wei(we){}
}e[maxn << 1];
void add(int u,int v,int w){e[++tot] = edge(v,head[u],w);head[u] = tot;}

typedef pair<int,pair<int,int> > pii;
priority_queue<pii,vector<pii>,greater<pii> > que;
int dis[maxn], pre[maxn];bool book[maxn];vector<int> st;

void dijsktra(int S){
    que.push(make_pair(0,make_pair(S,0)));dis[S] = 0;
    while(!que.empty()){
        auto tmp = que.top();
        int u = tmp.second.first;que.pop();
        if(book[u])continue;book[u] = 1;pre[u] = tmp.second.second;
        if(book2[u])continue;book2[u] = 1;st.push_back(u);
        for(int i = head[u];i;i = e[i].nexte){
            int v = e[i].to, w = e[i].wei;
            if(dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                que.push(make_pair(dis[v],make_pair(v,i)));
            }
        }
    }
}
int dfn[maxn], low[maxn], idx;
vector<pii> ed;bool anc[maxn];
vector<pair<int,int> > tre[maxn];
int ans = INF;
int dep[maxn];
void dfs(int u,int f){
    anc[u] = 1;dfn[u] = low[u] = ++idx;
    for(int i = head[u];i;i = e[i].nexte){
        int v = e[i].to, w = e[i].wei;
        if(dis[v] == dis[u] + w && pre[v] == i && !dep[v] && book2[v]){
            dep[v] = dep[u] + 1;
            tre[u].push_back(make_pair(v, w));
            dfs(v, u);
        }
        else{
            if(anc[v]){ans = min(ans,w + dis[u] - dis[v]);}
            else{ed.push_back(make_pair(w,make_pair(u, v)));}
        }
    }
    anc[u] = 0;low[u] = idx;
}

int id[maxn];
int dist[maxp][maxp];
vector<pair<int,int> > edg2[maxn];

vector<int> vec;
void dfs2(int u,int f){
    book[u] = 1;
    if(id[u]){
        for(int i : vec){dist[id[i]][id[u]] = dis[u] - dis[i];}
        vec.push_back(u);
    }
    for(auto i : tre[u]){int v = i.first;dfs2(v, u);}
    if(id[u])vec.pop_back();
}
void main(){
    n = read(); m = read();int u, v, w;
    for(int i = 1;i <= m;i++){
        u = read(); v = read(); w = read();
        add(u, v, w);
    }
    for(int i = 1;i <= n;i++)dis[i] = INF,book[i] = 0;
    int cnt = 0;
    int total = 0;
    memset(dist,0x3f,sizeof(dist));
for(int i = 1;i <= n;i++)if(!book2[i]){
    for(int i = 1;i <= cnt;i++){edg2[i].clear();for(int j = 1;j <= cnt;j++)dist[i][j] = INF;}
    for(int i : st)id[i] = 0;
    cnt = 0;st.clear();ed.clear();
    dijsktra(i);dfs(i,0);
    for(auto i : ed){
        int u = i.second.first, v = i.second.second, w = i.first;
        if((dfn[u] <= dfn[v] && dfn[v] <= low[u]))continue;
        if(!id[u]){id[u] = ++cnt;}
        if(!id[v]){id[v] = ++cnt;}
        dist[id[u]][id[v]] = w;
    }
    // cerr << "cnt = " << cnt << " st = " << st.size() << " ed = " << ed.size() << endl;
    for(int i : st)book[i] = 0;
    for(int i : st){if(!book[i])dfs2(i,i);}
    int totall = 0;
    for(int i = 1;i <= cnt;i++)for(int j = 1;j <= cnt;j++)
        if(dist[i][j] != INF){edg2[i].push_back(make_pair(j,dist[i][j]));totall++;}
        // cerr << "totall = " << totall << endl;
    // cerr << "111111111111111\n";
    for(int S = 1;S <= cnt;S++){
        for(int j = 1;j <= cnt;j++){book[j] = 0;dis[j] = INF;}
        while(!que.empty())que.pop();
        que.push(make_pair(0,make_pair(S,0)));dis[S] = 0;
        while(!que.empty()){
            auto tmp = que.top();
            int u = tmp.second.first;que.pop();
            if(book[u])continue;book[u] = 1;
            for(auto i : edg2[u]){
                total++;
                int v = i.first, w = i.second;
                // cerr << " v = " << v << endl;
                if(dis[v] > dis[u] + w){
                    dis[v] = dis[u] + w;
                    que.push(make_pair(dis[v],make_pair(v,0)));
                }
            }
        }
        for(int i = 1;i <= cnt;i++){dist[S][i] = dis[i];}
    }
    for(auto i : ed){
        int u = i.second.first, v = i.second.second, w = i.first;
        ans = min(ans,w + dist[id[v]][id[u]]);
    }
}
    // cerr << "total = " << total << endl;
    printf("%lld\n",ans == INF ? -1 : ans);
    return;
}
};
bool edmemory;
signed main(){
    // freopen("tmp.in","r",stdin);
    auto stclock = clock();
    Call_me_Eric::main();
    auto edclock = clock();
    cerr << (&stmemory - &edmemory) / 1024.0 / 1024.0 << " Mib cost.\n";
    cerr << (edclock - stclock) * 1.0 / CLOCKS_PER_SEC << " Sec cost.\n";
    return 0;
}
/*
5 6
1 2 1
1 4 1
2 3 1
4 5 1
3 4 5
5 2 5
*/

詳細信息

Test #1:

score: 100
Accepted
time: 19ms
memory: 237612kb

input:

4 6
1 2 1
4 3 3
4 1 9
2 4 1
3 1 2
3 2 6

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 3ms
memory: 237616kb

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 7ms
memory: 237640kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 72ms
memory: 288272kb

input:

258420 258419
33061 33062 767169384
212916 212917 1741339
229881 229882 896760805
173467 173468 273055172
233189 233190 800433307
10157 10158 126766550
174605 174606 552176083
224030 224031 886617880
229102 229103 783848581
67588 67589 510826095
233648 233649 879695751
214453 214454 867104578
153140...

output:

-1

result:

ok single line: '-1'

Test #5:

score: 0
Accepted
time: 204ms
memory: 259304kb

input:

248016 248896
82688 82755 592665252
202847 203260 294408121
26821 28237 269132335
150967 152178 3125829
246069 247390 29492546
7470 7673 55843492
33975 35414 442802995
28451 28948 193736988
34484 34637 84441058
60168 60309 501991354
79579 79844 26854803
239672 239706 111702667
73548 73658 149840530
...

output:

98674714245

result:

ok single line: '98674714245'

Test #6:

score: 0
Accepted
time: 199ms
memory: 261744kb

input:

270530 271285
80489 81855 218173724
188930 190845 783975756
29830 30626 22189315
234320 234472 70840355
198096 198272 300313423
224194 226906 105128197
115010 115834 37228105
134788 135583 18647938
257292 257358 98569041
146988 147215 69398857
248752 250002 409565478
62128 63751 839744551
121918 122...

output:

133486910467

result:

ok single line: '133486910467'

Test #7:

score: 0
Accepted
time: 603ms
memory: 259652kb

input:

222087 223141
123107 123811 2984035
216346 217464 675263
139741 141286 892140
77973 78018 453931100
38603 39546 157182459
13105 14616 775862
97035 97704 379136464
86254 88311 84193802
83968 84398 246202498
152486 160164 65619516
73213 73517 1129576
15618 16541 498613468
192241 195576 889879
21363 21...

output:

47599478278

result:

ok single line: '47599478278'

Test #8:

score: 0
Accepted
time: 811ms
memory: 258500kb

input:

212718 214066
104602 105717 148385760
163427 165307 437059346
108663 111803 784753745
15490 15784 789609
77598 80118 53908869
97776 98040 78287597
26994 27717 989577
134781 134919 531908
22362 24185 185680
114422 114890 609661
192852 192861 155477
45695 45800 35773
150695 152662 511678590
101629 102...

output:

36329947627

result:

ok single line: '36329947627'

Test #9:

score: 0
Accepted
time: 364ms
memory: 253580kb

input:

166349 167207
127268 127447 264589535
87716 91194 596943
123233 126065 170996332
16886 20295 35862710
4657 7035 31814455
95412 96577 195164337
17282 19855 200600035
18848 20733 547079078
139859 141952 197062
124361 126887 37905401
30749 32439 248082130
115409 121655 13113841
85640 88061 989595
74722...

output:

30821798636

result:

ok single line: '30821798636'

Test #10:

score: 0
Accepted
time: 443ms
memory: 267484kb

input:

289406 290248
136815 139417 24401
82238 82679 391891
117261 117722 23784755
45898 47276 91613
19042 20538 139326255
90781 91014 33771
173238 174945 166532570
64778 65593 89778641
107363 112432 3864090
260499 261031 165160
167079 167190 807727902
15135 17610 819060894
46707 48909 252893
51782 55878 3...

output:

61125659219

result:

ok single line: '61125659219'

Test #11:

score: -100
Wrong Answer
time: 80ms
memory: 255068kb

input:

246266 246265
67999 29611 208851615
22833 19844 11567655
78556 60887 111689338
95799 91984 129780604
41384 117633 410486433
7780 17854 417509938
16799 26207 657779642
94022 203027 902990247
41361 49284 914930989
188504 211149 506036119
55526 231127 316210314
179380 117042 590986492
198142 119962 623...

output:

-734989818

result:

wrong answer 1st lines differ - expected: '-1', found: '-734989818'