#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#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());
int lel;
int gel[201]={};
namespace Graph{
vector<int> BFS(vector<vector<pair<int,int>>> &arr, int src){
lel++;
int n = arr.size();
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
vector<int> dp(n,-1);
pq.push({0,src});
dp[src]=0;
while (pq.size()){
int node = pq.top().second;
int w = pq.top().first;
pq.pop();
if (gel[node]==lel) continue;
gel[node]=lel;
for (int i = 0; i < arr[node].size(); i++){
if (dp[arr[node][i].first]!=-1 && dp[arr[node][i].first]<=w+arr[node][i].second) continue;
dp[arr[node][i].first]=w+arr[node][i].second;
pq.push({w+arr[node][i].second,arr[node][i].first});
}
}
return dp;
}
int BFS(vector<vector<array<int,3>>> &arr, int src, int target, int yasak){
lel++;
int n = arr.size();
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
vector<int> dp(n,-1);
pq.push({0,src});
dp[src]=0;
while (pq.size()){
int node = pq.top().second;
int w = pq.top().first;
pq.pop();
if (node==target) return w;
if (gel[node]==lel) continue;
gel[node]=lel;
for (int i = 0; i < arr[node].size(); i++){
if (arr[node][i][2]==yasak) continue;
if (dp[arr[node][i][0]]!=-1 && dp[arr[node][i][0]]<=w+arr[node][i][1]) continue;
dp[arr[node][i][0]]=w+arr[node][i][1];
pq.push({w+arr[node][i][1],arr[node][i][0]});
}
}
return -1;
}
vector<pair<int,int>> BFS(vector<vector<array<int,3>>> &arr, int src){
lel++;
int n = arr.size();
priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>> pq;
pq.push({0,src,-1});
vector<pair<int,int>> dp(n,{-1,-1});
dp[src]={0,-1};
while (pq.size()){
int w = pq.top()[0];
int node = pq.top()[1];
int ind = pq.top()[2];
pq.pop();
if (gel[node]==lel) continue;
gel[node]=lel;
for (int i = 0; i < arr[node].size(); i++){
if (dp[arr[node][i][0]].first!=-1 && dp[arr[node][i][0]].first<=w+arr[node][i][1]) continue;
dp[arr[node][i][0]]={arr[node][i][1]+w,arr[node][i][2]};
pq.push({w+arr[node][i][1],arr[node][i][0],arr[node][i][2]});
}
}
return dp;
}
};
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=Graph::BFS(duz,0),
dp1=Graph::BFS(ters,0),
dp2=Graph::BFS(duz,n-1),
dp3=Graph::BFS(ters,n-1);
vector<pair<int,int>> hh = Graph::BFS(hayd,0);
vector<pair<int,int>> hh2 = Graph::BFS(hayd,n-1);
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
ho_t4.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("Bismillahirrahmanirrahim")
|
ho_t4.cpp:4: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
4 | #pragma optimize("HIZLAN ASLANIM BENIM")
|
ho_t4.cpp: In function 'std::vector<long long int> Graph::BFS(std::vector<std::vector<std::pair<long long int, long long int> > >&, long long int)':
ho_t4.cpp:55: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]
55 | 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:77: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]
77 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'std::vector<std::pair<long long int, long long int> > Graph::BFS(std::vector<std::vector<std::array<long long int, 3> > >&, long long int)':
ho_t4.cpp:100: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]
100 | for (int i = 0; i < arr[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
ho_t4.cpp:96:8: warning: unused variable 'ind' [-Wunused-variable]
96 | int ind = pq.top()[2];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
13 ms |
632 KB |
Output is correct |
11 |
Correct |
15 ms |
604 KB |
Output is correct |
12 |
Correct |
18 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
6404 KB |
Output is correct |
2 |
Correct |
16 ms |
6236 KB |
Output is correct |
3 |
Correct |
17 ms |
6256 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
16 ms |
6492 KB |
Output is correct |
10 |
Correct |
17 ms |
6408 KB |
Output is correct |
11 |
Correct |
18 ms |
6492 KB |
Output is correct |
12 |
Correct |
20 ms |
6492 KB |
Output is correct |
13 |
Correct |
18 ms |
6236 KB |
Output is correct |
14 |
Correct |
18 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
12 ms |
4788 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
15 ms |
6236 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
15 ms |
6412 KB |
Output is correct |
9 |
Correct |
15 ms |
6232 KB |
Output is correct |
10 |
Correct |
15 ms |
6408 KB |
Output is correct |
11 |
Correct |
14 ms |
6092 KB |
Output is correct |
12 |
Correct |
15 ms |
6408 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
584 KB |
Output is correct |
19 |
Correct |
15 ms |
6484 KB |
Output is correct |
20 |
Correct |
15 ms |
6236 KB |
Output is correct |
21 |
Correct |
15 ms |
6492 KB |
Output is correct |
22 |
Correct |
15 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
13 ms |
632 KB |
Output is correct |
11 |
Correct |
15 ms |
604 KB |
Output is correct |
12 |
Correct |
18 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
16 ms |
6404 KB |
Output is correct |
18 |
Correct |
16 ms |
6236 KB |
Output is correct |
19 |
Correct |
17 ms |
6256 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
16 ms |
6492 KB |
Output is correct |
26 |
Correct |
17 ms |
6408 KB |
Output is correct |
27 |
Correct |
18 ms |
6492 KB |
Output is correct |
28 |
Correct |
20 ms |
6492 KB |
Output is correct |
29 |
Correct |
18 ms |
6236 KB |
Output is correct |
30 |
Correct |
18 ms |
7000 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
12 ms |
4788 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
15 ms |
6236 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
15 ms |
6412 KB |
Output is correct |
39 |
Correct |
15 ms |
6232 KB |
Output is correct |
40 |
Correct |
15 ms |
6408 KB |
Output is correct |
41 |
Correct |
14 ms |
6092 KB |
Output is correct |
42 |
Correct |
15 ms |
6408 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
1 ms |
584 KB |
Output is correct |
49 |
Correct |
15 ms |
6484 KB |
Output is correct |
50 |
Correct |
15 ms |
6236 KB |
Output is correct |
51 |
Correct |
15 ms |
6492 KB |
Output is correct |
52 |
Correct |
15 ms |
6236 KB |
Output is correct |
53 |
Correct |
17 ms |
6236 KB |
Output is correct |
54 |
Correct |
18 ms |
6492 KB |
Output is correct |
55 |
Correct |
16 ms |
6236 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
73 ms |
4812 KB |
Output is correct |
59 |
Correct |
86 ms |
4956 KB |
Output is correct |
60 |
Correct |
117 ms |
4924 KB |
Output is correct |
61 |
Correct |
77 ms |
5564 KB |
Output is correct |
62 |
Correct |
82 ms |
5724 KB |
Output is correct |
63 |
Correct |
114 ms |
5724 KB |
Output is correct |
64 |
Correct |
538 ms |
6460 KB |
Output is correct |
65 |
Correct |
576 ms |
6660 KB |
Output is correct |
66 |
Correct |
742 ms |
6740 KB |
Output is correct |
67 |
Correct |
14 ms |
5120 KB |
Output is correct |
68 |
Correct |
16 ms |
7420 KB |
Output is correct |
69 |
Correct |
16 ms |
7772 KB |
Output is correct |
70 |
Correct |
24 ms |
7516 KB |
Output is correct |
71 |
Correct |
19 ms |
7256 KB |
Output is correct |
72 |
Correct |
23 ms |
7516 KB |
Output is correct |
73 |
Correct |
18 ms |
7768 KB |
Output is correct |
74 |
Correct |
19 ms |
7516 KB |
Output is correct |