QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371471#7991. 最小环24bot#WA 114ms288084kbC++145.0kb2024-03-30 12:58:072024-03-30 12:58:07

Judging History

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

  • [2024-03-30 12:58:07]
  • 评测
  • 测评结果:WA
  • 用时:114ms
  • 内存:288084kb
  • [2024-03-30 12:58:07]
  • 提交

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 = 0x3f3f3f3f3f3f3f, 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();
}
int dis1[maxn];
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 t = 1;t <= n;t++)if(!book2[t]){
    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(t);dfs(t,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){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]));}
        // cerr << "totall = " << totall << endl;
    // cerr << "111111111111111\n";
    for(int S = 1;S <= cnt;S++){
        for(int j = 1;j <= cnt;j++){book[j] = 0;dis1[j] = INF;}
        while(!que.empty())que.pop();
        que.push(make_pair(0,make_pair(S,0)));dis1[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(dis1[v] > dis1[u] + w){
                    dis1[v] = dis1[u] + w;
                    que.push(make_pair(dis1[v],make_pair(v,0)));
                }
            }
        }
        for(int i = 1;i <= cnt;i++){dist[S][i] = dis1[i];}
    }
    for(int i : st)dis[i] = 0, dep[i] = 0,book[i] = 0;
    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]]);
    }
    // if(ans < 0){ cerr << "S = " << t << " ans = " << ans;break;}
}
    // 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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 237588kb

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

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 11ms
memory: 237684kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 55ms
memory: 288084kb

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: 108ms
memory: 272128kb

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: -100
Wrong Answer
time: 114ms
memory: 272624kb

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:

136244513930

result:

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