#pragma GCC optimize("O3","unroll-loops","Ofast")
#include<iostream>
#include<vector>
#include<queue>
#include<map>
using namespace std;
struct wi{
vector<int> Q;
int odw=0;
long long kiedy_odw=1e18;
int od_kogo=0;
}*w;
long long wynik=1e18;
int kiedy=1;
int n;
int t[60000][4];
long long odl[210][210];
bool nalezy[60000];
void dijkstra(int kto)
{
priority_queue<pair<long long,pair<int,int> > > kolejka;
kolejka.push({0,{1,0}});
for(int i=1;i<=n;i++)
w[i].kiedy_odw=1e18;
while(!kolejka.empty())
{
pair<long long,pair<int,int> > para=kolejka.top();
int x=para.second.first;
kolejka.pop();
if(w[x].kiedy_odw<-para.first)
continue;
w[x].od_kogo=para.second.second;
w[x].odw=kiedy;
w[x].kiedy_odw=-para.first;
for(int i=0;i<w[x].Q.size();i++)
{
int ile=w[x].Q[i];
if(ile==kto)
continue;
int y;
if(ile<0)
y=t[-ile][3];
else y=t[ile][0];
int koszt=t[abs(ile)][1];
if(w[y].kiedy_odw<=-para.first+koszt)
continue;
w[y].kiedy_odw=-para.first+koszt;
kolejka.push({para.first-koszt,{y,ile}});
}
}
long long d1=w[n].kiedy_odw;
bool git1=0,git2=0;
if(w[n].odw==kiedy)
git1=1;
if(kto==0 && w[n].odw==kiedy)
{
int jestem=n;
while(jestem!=1)
{
nalezy[w[jestem].od_kogo]=1;
int x,y;
x=t[w[jestem].od_kogo][0];
y=t[w[jestem].od_kogo][3];
if(x==jestem)
jestem=y;
else jestem=x;
}
}
kiedy++;
kolejka.push({0,{n,0}});
for(int i=1;i<=n;i++)
w[i].kiedy_odw=1e18;
while(!kolejka.empty())
{
pair<long long,pair<int,int> > para=kolejka.top();
int x=para.second.first;
kolejka.pop();
if(w[x].kiedy_odw<-para.first)
continue;
w[x].od_kogo=para.second.second;
w[x].odw=kiedy;
w[x].kiedy_odw=-para.first;
for(int i=0;i<w[x].Q.size();i++)
{
int ile=w[x].Q[i];
if(ile==kto)
continue;
int y;
if(ile<0)
y=t[-ile][3];
else y=t[ile][0];
int koszt=t[abs(ile)][1];
if(w[y].kiedy_odw<=-para.first+koszt)
continue;
w[y].kiedy_odw=-para.first+koszt;
kolejka.push({para.first-koszt,{y,ile}});
}
}
if(w[1].odw==kiedy)
git2=1;
if(kto==0 && w[1].odw==kiedy)
{
int jestem=1;
while(jestem!=n)
{
nalezy[w[jestem].od_kogo]=1;
int x,y;
x=t[w[jestem].od_kogo][0];
y=t[w[jestem].od_kogo][3];
if(x==jestem)
jestem=y;
else jestem=x;
}
}
long long d2=w[1].kiedy_odw;
if(git1==1 && git2==1)
wynik=min(wynik,d1+d2+t[kto][2]);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m,x,y,a,b;
cin>>n>>m;
w=new wi[n+3];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
odl[i][j]=1e18;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>a>>b;
w[x].Q.push_back(i);
t[i][0]=y;
t[i][1]=a;
t[i][2]=b;
t[i][3]=x;
odl[x][y]=min(odl[x][y],(long long)a);
}
for(int i=1;i<=n;i++)
odl[i][i]=0;
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
odl[i][j]=min(odl[i][j],odl[i][k]+odl[k][j]);
dijkstra(0);
kiedy++;
for(int i=1;i<=m;i++)
{
if(nalezy[i])
{
int odwr=t[i][0];
int pocz=t[i][3];
w[odwr].Q.push_back(-i);
dijkstra(i);
kiedy++;
w[odwr].Q.pop_back();
}
else{
int x=t[i][3];
int y=t[i][0];
int odwr=t[i][2];
int koszt=t[i][1];
wynik=min(wynik,odl[1][n]+odl[n][y]+odl[x][1]+odwr+koszt);
wynik=min(wynik,odl[n][1]+odl[x][n]+odl[1][y]+odwr+koszt);
wynik=min(wynik,odl[1][y]+odl[x][n]+odl[n][y]+odl[x][1]+odwr+2*koszt);
}
}
if(wynik>=1e18)
cout<<"-1";
else cout<<wynik;
return 0;
}
Compilation message
ho_t4.cpp: In function 'void dijkstra(int)':
ho_t4.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=0;i<w[x].Q.size();i++)
| ~^~~~~~~~~~~~~~
ho_t4.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i=0;i<w[x].Q.size();i++)
| ~^~~~~~~~~~~~~~
ho_t4.cpp: In function 'int32_t main()':
ho_t4.cpp:153:13: warning: unused variable 'pocz' [-Wunused-variable]
153 | int pocz=t[i][3];
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
8 ms |
596 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
628 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
24 ms |
596 KB |
Output is correct |
11 |
Correct |
37 ms |
696 KB |
Output is correct |
12 |
Correct |
32 ms |
696 KB |
Output is correct |
13 |
Correct |
11 ms |
596 KB |
Output is correct |
14 |
Correct |
10 ms |
596 KB |
Output is correct |
15 |
Correct |
9 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
1856 KB |
Output is correct |
2 |
Correct |
32 ms |
1772 KB |
Output is correct |
3 |
Correct |
31 ms |
1756 KB |
Output is correct |
4 |
Correct |
9 ms |
720 KB |
Output is correct |
5 |
Correct |
8 ms |
596 KB |
Output is correct |
6 |
Correct |
10 ms |
596 KB |
Output is correct |
7 |
Correct |
9 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
33 ms |
1796 KB |
Output is correct |
10 |
Correct |
26 ms |
1696 KB |
Output is correct |
11 |
Correct |
38 ms |
1868 KB |
Output is correct |
12 |
Correct |
30 ms |
1748 KB |
Output is correct |
13 |
Correct |
34 ms |
1788 KB |
Output is correct |
14 |
Correct |
32 ms |
1840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
704 KB |
Output is correct |
2 |
Correct |
8 ms |
596 KB |
Output is correct |
3 |
Correct |
25 ms |
1524 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
31 ms |
1748 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
25 ms |
1756 KB |
Output is correct |
9 |
Correct |
25 ms |
1716 KB |
Output is correct |
10 |
Correct |
27 ms |
1748 KB |
Output is correct |
11 |
Correct |
27 ms |
1740 KB |
Output is correct |
12 |
Correct |
25 ms |
1748 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
25 ms |
1816 KB |
Output is correct |
20 |
Correct |
25 ms |
1748 KB |
Output is correct |
21 |
Correct |
28 ms |
1760 KB |
Output is correct |
22 |
Correct |
33 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
8 ms |
596 KB |
Output is correct |
4 |
Correct |
9 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
10 ms |
628 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
24 ms |
596 KB |
Output is correct |
11 |
Correct |
37 ms |
696 KB |
Output is correct |
12 |
Correct |
32 ms |
696 KB |
Output is correct |
13 |
Correct |
11 ms |
596 KB |
Output is correct |
14 |
Correct |
10 ms |
596 KB |
Output is correct |
15 |
Correct |
9 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
700 KB |
Output is correct |
17 |
Correct |
29 ms |
1856 KB |
Output is correct |
18 |
Correct |
32 ms |
1772 KB |
Output is correct |
19 |
Correct |
31 ms |
1756 KB |
Output is correct |
20 |
Correct |
9 ms |
720 KB |
Output is correct |
21 |
Correct |
8 ms |
596 KB |
Output is correct |
22 |
Correct |
10 ms |
596 KB |
Output is correct |
23 |
Correct |
9 ms |
596 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
33 ms |
1796 KB |
Output is correct |
26 |
Correct |
26 ms |
1696 KB |
Output is correct |
27 |
Correct |
38 ms |
1868 KB |
Output is correct |
28 |
Correct |
30 ms |
1748 KB |
Output is correct |
29 |
Correct |
34 ms |
1788 KB |
Output is correct |
30 |
Correct |
32 ms |
1840 KB |
Output is correct |
31 |
Correct |
8 ms |
704 KB |
Output is correct |
32 |
Correct |
8 ms |
596 KB |
Output is correct |
33 |
Correct |
25 ms |
1524 KB |
Output is correct |
34 |
Correct |
9 ms |
596 KB |
Output is correct |
35 |
Correct |
31 ms |
1748 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
25 ms |
1756 KB |
Output is correct |
39 |
Correct |
25 ms |
1716 KB |
Output is correct |
40 |
Correct |
27 ms |
1748 KB |
Output is correct |
41 |
Correct |
27 ms |
1740 KB |
Output is correct |
42 |
Correct |
25 ms |
1748 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
25 ms |
1816 KB |
Output is correct |
50 |
Correct |
25 ms |
1748 KB |
Output is correct |
51 |
Correct |
28 ms |
1760 KB |
Output is correct |
52 |
Correct |
33 ms |
1748 KB |
Output is correct |
53 |
Correct |
39 ms |
1740 KB |
Output is correct |
54 |
Correct |
48 ms |
1796 KB |
Output is correct |
55 |
Correct |
31 ms |
1724 KB |
Output is correct |
56 |
Correct |
9 ms |
700 KB |
Output is correct |
57 |
Correct |
9 ms |
700 KB |
Output is correct |
58 |
Correct |
125 ms |
1536 KB |
Output is correct |
59 |
Correct |
199 ms |
1492 KB |
Output is correct |
60 |
Correct |
210 ms |
1596 KB |
Output is correct |
61 |
Correct |
135 ms |
1600 KB |
Output is correct |
62 |
Correct |
140 ms |
1596 KB |
Output is correct |
63 |
Correct |
194 ms |
1600 KB |
Output is correct |
64 |
Correct |
747 ms |
2168 KB |
Output is correct |
65 |
Correct |
776 ms |
2520 KB |
Output is correct |
66 |
Correct |
987 ms |
2212 KB |
Output is correct |
67 |
Correct |
14 ms |
1368 KB |
Output is correct |
68 |
Correct |
26 ms |
1876 KB |
Output is correct |
69 |
Correct |
27 ms |
1972 KB |
Output is correct |
70 |
Correct |
30 ms |
1928 KB |
Output is correct |
71 |
Correct |
32 ms |
1944 KB |
Output is correct |
72 |
Correct |
28 ms |
1872 KB |
Output is correct |
73 |
Correct |
31 ms |
2052 KB |
Output is correct |
74 |
Correct |
32 ms |
2004 KB |
Output is correct |