QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#628947#7991. 最小环hhhyhWA 203ms63784kbC++232.4kb2024-10-10 23:26:572024-10-10 23:26:57

Judging History

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

  • [2024-10-10 23:26:57]
  • 评测
  • 测评结果:WA
  • 用时:203ms
  • 内存:63784kb
  • [2024-10-10 23:26:57]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using i64=long long;
#define all(x) x.begin(),x.end()
#define int long long
void solve(){
    //有向图
    int n;cin>>n;
    int m;cin>>m;
    vector<int>live(n,1);
    vector<map<int,int>>e1(n),e2(n);
    i64 ans=1e18;
    for(int i=0;i<m;i++){
        int u,v,w;cin>>u>>v>>w;
        u--,v--;
        if(u==v){ans=min(ans,w);continue;}
        if(!e1[u].count(v))e1[u][v]=w;
        else e1[u][v]=min(e1[u][v],w);

        if(!e2[v].count(u))e2[v][u]=w;
        else e2[v][u]=min(e2[v][u],w);
    }
    auto check=[&](int u){
        if(e1[u].empty()||e2[u].empty()||e1[u].size()+e2[u].size()<=2)return true;
        return false;
    };
    queue<int>q;
    for(int i=0;i<n;i++){
        if(check(i))q.push(i),live[i]=0;
    }
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(auto y:e1[x])e2[y.first].erase(x);
        for(auto y:e2[x])e1[y.first].erase(x);
        for(auto y:e1[x])if(e2[x].count(y.first))ans=min(ans,e2[x][y.first]+y.second);
        //合并u->x->v,u->v
        for(auto [y1,w1]:e2[x]){
            for(auto [y2,w2]:e1[x]){
                if(!e1[y1].count(w2))e1[y1][y1]=w1+w2;
                else e1[y1][y2]=min(e1[y1][y2],w1+w2);
            
                if(!e2[y2].count(y1))e2[y2][y1]=w1+w2;
                else e2[y2][y1]=min(e2[y2][y1],w1+w2);
            }
        }
        for(auto y:e1[x])if(live[y.first]&&check(y.first))q.push(y.first),live[y.first]=0;
        for(auto y:e2[x])if(live[y.first]&&check(y.first))q.push(y.first),live[y.first]=0;
    }
    for(int i=0;i<n;i++){
        if(!live[i])e1[i].clear();
    }
    for(int i=0;i<n;i++){
        if(!live[i])continue;
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>q;
        q.push({0,i});
        vector<i64>dis(n,1e18);
        dis[i]=0;
        while(!q.empty()){
            auto [d,x]=q.top();
            q.pop();
            if(d>dis[x])continue;
            for(auto [y,w]:e1[x]){
                ans=min(dis[x]+dis[y]+w,ans);
                if(dis[y]>dis[x]+w){
                    dis[y]=dis[x]+w;
                    q.push({dis[y],y});
                }
            }
        }
    }
    i64 inf=1e18;
    if(ans==inf)ans=-1;
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    while(T--)solve();
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3812kb

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

input:

1 0

output:

-1

result:

ok single line: '-1'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

1 1
1 1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 170ms
memory: 63784kb

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

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:

3930459170

result:

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