This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma optimize("Bismillahirrahmanirrahim")
#pragma optimize("HIZLAN ASLANIM BENIM")
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█■█
//█─█─█▄─█▄─█─█─█─█
//Allahuekber
//ahmet23 orz...
//Sani buyuk Osman Pasa Plevneden cikmam diyor.
//FatihSultanMehmedHan
//YavuzSultanSelimHan
//AbdulhamidHan
#define author tolbi
#include <bits/stdc++.h>
using namespace std;
template<typename X, typename Y> istream& operator>>(istream& in, pair<X,Y> &pr) {return in>>pr.first>>pr.second;}
template<typename X, typename Y> ostream& operator<<(ostream& os, pair<X,Y> pr) {return os<<pr.first<<" "<<pr.second;}
template<typename X> istream& operator>>(istream& in, vector<X> &arr) {for(auto &it : arr) in>>it; return in;}
template<typename X> ostream& operator<<(ostream& os, vector<X> arr) {for(auto &it : arr) os<<it<<" "; return os;}
template<typename X, size_t Y> istream& operator>>(istream& in, array<X,Y> &arr) {for(auto &it : arr) in>>it; return in;}
template<typename X, size_t Y> ostream& operator<<(ostream& os, array<X,Y> arr) {for(auto &it : arr) os<<it<<" "; return os;}
#define int long long
#define endl '\n'
#define vint(x) vector<int> x
#define deci(x) int x;cin>>x;
#define decstr(x) string x;cin>>x;
#define cinarr(x) for (auto &it : x) cin>>it;
#define coutarr(x) for (auto &it : x) cout<<it<<" ";cout<<endl;
#define sortarr(x) sort(x.begin(),x.end())
#define sortrarr(x) sort(x.rbegin(),x.rend())
#define det(x) cout<<"NO\0YES"+x*3<<endl;
#define INF LONG_LONG_MAX
#define rev(x) reverse(x.begin(),x.end());
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define tol(bi) (1LL<<((int)(bi)))
const int MOD = 1e9+7;
mt19937 ayahya(chrono::high_resolution_clock().now().time_since_epoch().count());
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
vector<int> us;
namespace Graph{
void BFS(vector<vector<pair<int,int>>> &arr, int src, vector<int>& dp){
int n = arr.size();
if (dp.size()!=n) dp.resize(n);
fill(dp.begin(), dp.end(), -1);
pq.push({0,src});
while (pq.size()){
int node = pq.top().second;
int w = pq.top().first;
pq.pop();
if (dp[node]!=-1) continue;
dp[node]=w;
for (int i = 0; i < arr[node].size(); i++){
pq.push({w+arr[node][i].second,arr[node][i].first});
}
}
}
int BFS(vector<vector<array<int,3>>> &arr, int src, int target, int yasak){
int n = arr.size();
if (us.size()!=n) us.resize(n);
fill(us.begin(), us.end(), -1);
pq.push({0,src});
while (pq.size()){
int node = pq.top().second;
int w = pq.top().first;
pq.pop();
if (us[node]!=-1) continue;
if (node==target) {
while (pq.size()) pq.pop();
return w;
}
us[node]=w;
for (int i = 0; i < arr[node].size(); i++){
if (arr[node][i][2]==yasak) continue;
pq.push({w+arr[node][i][1],arr[node][i][0]});
}
}
return -1;
}
void BFS(vector<vector<array<int,3>>> &arr, int src, vector<pair<int,int>> &ret){
int n = arr.size();
priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>> pq;
pq.push({0,src,-1});
if (ret.size()!=n) ret.resize(n);
fill(ret.begin(), ret.end(), make_pair(-1,-1));
while (pq.size()){
int w = pq.top()[0];
int node = pq.top()[1];
int ind = pq.top()[2];
pq.pop();
if (ret[node].first!=-1) continue;
ret[node]={node,ind};
for (int i = 0; i < arr[node].size(); i++){
pq.push({w+arr[node][i][1],arr[node][i][0],arr[node][i][2]});
}
}
}
};
int32_t main(){
ios;
int t=1;
int tno = 0;
if (!t) cin>>t;
while (t-(tno++)){
deci(n);deci(m);
vector<array<int,2>> edges;
vector<vector<pair<int,int>>> duz(n);
vector<vector<pair<int,int>>> ters(n);
vector<vector<array<int,3>>> hayd(n);
vector<array<int,4>> ed(m);
cinarr(ed);
for (int i = 0; i < m; ++i)
{
ed[i][0]--,ed[i][1]--;
duz[ed[i][0]].push_back({ed[i][1],ed[i][2]});
ters[ed[i][1]].push_back({ed[i][0],ed[i][2]});
hayd[ed[i][0]].push_back({ed[i][1],ed[i][2],i});
}
vector<int> dp,dp1,dp2,dp3;
Graph::BFS(duz,0,dp);
Graph::BFS(ters,0,dp1);
Graph::BFS(duz,n-1,dp2);
Graph::BFS(ters,n-1,dp3);
vector<pair<int,int>> hh;
Graph::BFS(hayd,0,hh);
vector<pair<int,int>> hh2;
Graph::BFS(hayd,n-1,hh2);
vector<bool> kul(m,false);
int nod = n-1;
while (hh[nod].second!=-1){
kul[hh[nod].second]=true;
nod=ed[hh[nod].second][0];
}
nod=0;
while (hh2[nod].second!=-1){
kul[hh2[nod].second]=true;
nod=ed[hh2[nod].second][0];
}
int ans = INF;
if (dp[n-1]!=-1 && dp2[0]!=-1) ans=dp[n-1]+dp2[0];
for (int i = 0; i < m; i++){
int crr = INF;
if (kul[i]){
hayd[ed[i][1]].push_back({ed[i][0],ed[i][2],m});
int a = Graph::BFS(hayd,0,n-1,i);
int b = Graph::BFS(hayd,n-1,0,i);
hayd[ed[i][1]].pop_back();
if (a==-1 || b==-1) continue;
crr=a+b+ed[i][3];
}
else {
int git = INF;
int don = INF;
if (dp[n-1]!=-1) git=dp[n-1];
if (dp2[0]!=-1) don=dp2[0];
if (dp[ed[i][1]]!=-1 && dp3[ed[i][0]]!=-1){
git=min(git,dp[ed[i][1]]+dp3[ed[i][0]]+ed[i][2]);
}
if (dp2[ed[i][1]]!=-1 && dp1[ed[i][0]]!=-1){
don=min(don,dp2[ed[i][1]]+dp1[ed[i][0]]+ed[i][2]);
}
if (git!=INF && don!=INF) crr=git+don+ed[i][3];
}
ans=min(ans,crr);
}
if (ans==INF) ans=-1;
cout<<ans<<endl;
}
}
Compilation message (stderr)
ho_t4.cpp:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
1 | #pragma optimize("Bismillahirrahmanirrahim")
|
ho_t4.cpp:2: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
2 | #pragma optimize("HIZLAN ASLANIM BENIM")
|
ho_t4.cpp: In function 'void Graph::BFS(std::vector<std::vector<std::pair<long long int, long long int> > >&, long long int, std::vector<long long int>&)':
ho_t4.cpp:42:16: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
42 | if (dp.size()!=n) dp.resize(n);
| ~~~~~~~~~^~~
ho_t4.cpp:51:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'long long int Graph::BFS(std::vector<std::vector<std::array<long long int, 3> > >&, long long int, long long int, long long int)':
ho_t4.cpp:58:16: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
58 | if (us.size()!=n) us.resize(n);
| ~~~~~~~~~^~~
ho_t4.cpp:71:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'void Graph::BFS(std::vector<std::vector<std::array<long long int, 3> > >&, long long int, std::vector<std::pair<long long int, long long int> >&)':
ho_t4.cpp:82:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
82 | if (ret.size()!=n) ret.resize(n);
| ~~~~~~~~~~^~~
ho_t4.cpp:91:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
# | 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... |