QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#352760 | #7991. 最小环 | Zpair | WA | 423ms | 96892kb | C++14 | 2.5kb | 2024-03-13 16:31:30 | 2024-03-13 16:31:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+5;
unordered_set<int> s;
set<pair<int, ll > >e[N],e2[N];
bool pd[N];
int d1[N],d2[N],n,m;
bool isdel(int p){
return d1[p]==0||d2[p]==0||(d1[p]==1&&d2[p]==1);
}
set<pair<pair<int, int>, ll> > E;//边集
void erase(int x,int y,ll z){
E.erase({{x,y},z});
}
void del(int p){
pd[p]=1;
if(d1[p]==1&&d2[p]==1){
auto x=*e[p].begin();
auto y=*e2[p].begin();
int u=y.first,v=x.first;
ll w=x.first+y.first;
E.insert({{u,v},w});
e[u].insert({v,w});
e2[v].insert({u,w});
e[p].clear();
e[u].erase({p,y.second});
erase(u,p,y.second);
e2[v].erase({p,x.second});
erase(p,v,x.second);
s.erase(p);
return;
//u->p->v
}
for(auto it:e[p]){
int t=it.first;
ll w=it.second;
d1[t]--;
if(isdel(t))s.insert(t);
e[t].erase({p,w});
erase(p,t,w);
}
for(auto it:e2[p]){
int t=it.first;
ll w=it.second;
d2[t]--;
if(isdel(t))s.insert(t);
e[t].erase({p,w});
erase(t,p,w);
}
e[p].clear(),e2[p].clear();
s.erase(p);
}
namespace solve{
const int N=3005;
ll dis[N][N];
priority_queue<pair<ll, int> > q;
vector<pair<int, ll> > e[N];
void add(int x,int y,ll z){
e[x].push_back({y,z});
// cout<<x<<" "<<y<<" "<<z<<endl;
}
void solve(ll *dis,int s){
for(int i=1;i<=n;++i)
dis[i]=1e18;
q.push({0,s}),dis[s]=0;
while(!q.empty()){
int p=q.top().second;
ll v=-q.top().first;
q.pop();
if(dis[p]>v)continue;
for(auto it:e[p]){
int t=it.first;ll v=it.second;
if(dis[t]>dis[p]+v){
dis[t]=dis[p]+v;
q.push({-dis[t],t});
}
}
}
}
ll work(){
for(int i=1;i<=n;++i)
solve(dis[i],i);
ll ans=1e18;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(i!=j)ans=min(ans,dis[i][j]+dis[j][i]);
return ans;
}
}
int mp[N],tot;
int main(){
cin>>n>>m;int x,y,z;
ll ans=1e18;
for(int i=1;i<=m;++i){
scanf("%d%d%d",&x,&y,&z);
if(x==y){
ans=min(ans,(ll)z);
continue;
}
if(E.find({{x,y},z})!=E.end())
continue;
e[x].insert({y,z});
e2[y].insert({x,z});
d1[y]++,d2[x]++;
E.insert({{x,y},z});
}
for(int i=1;i<=n;++i)
if(isdel(i))s.insert(i);
while(!s.empty()){
int p=*s.begin();
del(p);
s.erase(p);
}
for(auto it:E){
int x=it.first.first,y=it.first.second;
ll z=it.second;
if(!mp[x])mp[x]=++tot;
if(!mp[y])mp[y]=++tot;
x=mp[x],y=mp[y];
solve::add(x,y,z);
}
n=tot;
ans=min(ans,solve::work());
if(ans>1e17)puts("-1");
else cout<<ans;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 37976kb
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: 37652kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 38012kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 341ms
memory: 96892kb
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: 423ms
memory: 94064kb
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:
8
result:
wrong answer 1st lines differ - expected: '98674714245', found: '8'