This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define ll long long
const ll maxn=100001;
const ll inf=999999999999999999;
ll n,m,s1,f1,s2,f2,ans[maxn],p[maxn];
bool was[maxn];
vector<pair<ll,ll>> a[maxn];
set<pair<ll,ll>> ro;
vector<ll> vp[maxn];
bool change_p=1;
void dfs_dj(ll v){
priority_queue<pair<ll,ll>> q;
q.push(make_pair(0,v));
while(!q.empty()){
ll cur_ans=-q.top().first,cur=q.top().second;
q.pop();
if(ans[cur]!=cur_ans)continue;
for(ll i=0;i<int(a[cur].size());i++){
ll to=a[cur][i].first,cost=a[cur][i].second;
if(ro.count(make_pair(cur,to))){
cost=0;
}
if(ans[cur]+cost<ans[to]){
if(ans[to]>ans[cur]+cost and change_p){
vp[to].clear();
}
ans[to]=ans[cur]+cost;
if(change_p){
vp[to].push_back(cur);
p[to]=cur;
}
q.push(make_pair(-ans[to],to));
}
}
}
}
void set_route(ll v){
ro.clear();
while(p[v]!=v){
ro.insert(make_pair(p[v],v));
ro.insert(make_pair(v,p[v]));
v=p[v];
}
}
void cl_ans(ll v){
for(int i=1;i<=n;i++)ans[i]=inf;
ans[v]=0;
}
void go(ll v){
cl_ans(v);
dfs_dj(v);
}
ll final_ans=inf;
void set_was(ll v){
was[v]=1;
for(ll i=0;i<int(vp[v].size());i++){
ll to=vp[v][i];
if(!was[to]){
set_was(to);
}
}
}
bool uniq(ll cur){
if(p[cur]==cur)return true;
if(vp[cur].size()>1)return false;
return uniq(p[cur]);
}
void subtask2(){
set_route(f1);
go(s2);
cout<<ans[f2];
}
int main(){
cin>>n>>m>>s1>>f1>>s2>>f2;
ll st,ft,c;
for(int i=0;i<m;i++){
cin>>st>>ft>>c;
a[st].push_back(make_pair(ft,c));
a[ft].push_back(make_pair(st,c));
}
p[s1]=s1;
go(s1);
change_p=false;
bool un=uniq(f1);
un=true;
if(un and s1!=s2)subtask2();
else if(s1==s2){
set_was(f1);
go(f2);
for(int i=1;i<=n;i++){
cout<<ans[i]<<' ';
}
ll final_ans=inf;
for(ll i=1;i<=n;i++){
if(was[i]){
final_ans=min(final_ans,ans[i]);
}
}
cout<<final_ans;
if(final_ans==inf){
while(final_ans--)final_ans--;
}
}else{
cout<<"unluck";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |