#include <bits/stdc++.h>
using namespace std;
int n;int m;
int uar[50010];int var[50010];long long war[50010];long long dar[50010];
long long dis[210];
long long rdis[210];
int vis[210];
long long odis[210];
long long ordis[210];
vector<pair<int,long long>>e[210];
long long dijkstera(){
for(int i=1;i<=n;i++){
dis[i]=1e18;
vis[i]=0;
rdis[i]=1e18;
e[i].clear();
}
for(int i=1;i<=m;i++){
e[uar[i]].push_back({var[i],war[i]});
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
dis[1]=0;
pq.push({0,1});
while(pq.size()){
int nw=pq.top().second;
pq.pop();
if(vis[nw]){continue;}
vis[nw]=1;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;
long long w=e[nw][i].second;
if(dis[v]>dis[nw]+w){
dis[v]=dis[nw]+w;
pq.push({dis[v],v});
}
}
}
rdis[n]=0;
pq.push({0,n});
while(pq.size()){
int nw=pq.top().second;
pq.pop();
if(vis[nw]==2){continue;}
vis[nw]=2;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i].first;
long long w=e[nw][i].second;
if(rdis[v]>rdis[nw]+w){
rdis[v]=rdis[nw]+w;
pq.push({rdis[v],v});
}
}
}
return dis[n]+rdis[1];
}
vector<int>ee[210];
vector<int>er[210];
long long dis1[210];int frm[210];
long long rdis1[210];int frmn[210];
long long disn[210];
long long rdisn[210];
long long fdis[210][210];
int isin[50010];
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
fdis[i][j]=1e18;
}
fdis[i][i]=0;
}
for(int i=1;i<=m;i++){
cin>>uar[i]>>var[i]>>war[i]>>dar[i];
ee[uar[i]].push_back(i);
er[var[i]].push_back(i);
fdis[uar[i]][var[i]]=min(fdis[uar[i]][var[i]],war[i]);
}
for(int i=1;i<=n;i++){
dis1[i]=1e18;
rdis1[i]=1e18;
disn[i]=1e18;
rdisn[i]=1e18;
}
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
dis1[1]=0;
pq.push({0,1});
while(pq.size()){
int nw=pq.top().second;
pq.pop();
if(vis[nw]){continue;}
vis[nw]=1;
for(int i=0;i<ee[nw].size();i++){
int id=ee[nw][i];
int v=var[id];int w=war[id];
if(dis1[v]>dis1[nw]+w){
dis1[v]=dis1[nw]+w;
frm[v]=id;
pq.push({dis1[v],v});
}
}
}
for(int i=1;i<=n;i++){
vis[i]=0;
}
disn[n]=0;
pq.push({0,n});
while(pq.size()){
int nw=pq.top().second;
pq.pop();
if(vis[nw]){continue;}
vis[nw]=1;
for(int i=0;i<ee[nw].size();i++){
int id=ee[nw][i];
int v=var[id];int w=war[id];
if(disn[v]>disn[nw]+w){
disn[v]=disn[nw]+w;
frmn[v]=id;
pq.push({disn[v],v});
}
}
}
for(int i=1;i<=n;i++){
for(int a=1;a<=n;a++){
for(int b=1;b<=n;b++){
fdis[a][b]=min(fdis[a][b],fdis[a][i]+fdis[i][b]);
}
}
}
if(dis1[n]<1e18){
int nw=n;
while(nw!=1){
int id=frm[nw];
isin[id]=1;
nw=uar[id];
}
}
if(disn[1]<1e18){
int nw=1;
while(nw!=n){
int id=frmn[nw];
isin[id]=1;
nw=uar[id];
}
}
long long ans=dijkstera();
//cout<<"hihi\n";
for(int i=1;i<=m;i++){
// cout<<i<<" ";
if(isin[i]){
swap(uar[i],var[i]);
ans=min(ans,dijkstera()+dar[i]);
swap(uar[i],var[i]);
}else{
// continue;
int a=uar[i];int b=var[i];
long long cal=min(dis1[n],fdis[1][b]+war[i]+fdis[a][n] )+fdis[n][b]+fdis[a][1]+war[i];
cal=min(cal,min(disn[1],fdis[n][b]+war[i]+fdis[a][1])+fdis[1][b]+fdis[a][n]+war[i] );
ans=min(ans,cal+dar[i]);
}
//}
// cout<<ans<<endl;
}
if(ans>=1e18){ans=-1;}
cout<<ans<<endl;
}
Compilation message
ho_t4.cpp: In function 'long long int dijkstera()':
ho_t4.cpp:31:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
ho_t4.cpp:49:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:106:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for(int i=0;i<ee[nw].size();i++){
| ~^~~~~~~~~~~~~~
ho_t4.cpp:126:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for(int i=0;i<ee[nw].size();i++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
724 KB |
Output is correct |
3 |
Correct |
11 ms |
748 KB |
Output is correct |
4 |
Correct |
10 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
9 ms |
720 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
25 ms |
788 KB |
Output is correct |
11 |
Correct |
30 ms |
852 KB |
Output is correct |
12 |
Correct |
30 ms |
852 KB |
Output is correct |
13 |
Correct |
10 ms |
756 KB |
Output is correct |
14 |
Correct |
10 ms |
852 KB |
Output is correct |
15 |
Correct |
10 ms |
724 KB |
Output is correct |
16 |
Correct |
9 ms |
752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
3716 KB |
Output is correct |
2 |
Correct |
34 ms |
3748 KB |
Output is correct |
3 |
Correct |
34 ms |
3860 KB |
Output is correct |
4 |
Correct |
9 ms |
852 KB |
Output is correct |
5 |
Correct |
10 ms |
744 KB |
Output is correct |
6 |
Correct |
9 ms |
724 KB |
Output is correct |
7 |
Correct |
9 ms |
724 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
30 ms |
3776 KB |
Output is correct |
10 |
Correct |
26 ms |
3712 KB |
Output is correct |
11 |
Correct |
35 ms |
3788 KB |
Output is correct |
12 |
Correct |
33 ms |
3848 KB |
Output is correct |
13 |
Correct |
33 ms |
3796 KB |
Output is correct |
14 |
Correct |
33 ms |
4052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
748 KB |
Output is correct |
2 |
Correct |
8 ms |
724 KB |
Output is correct |
3 |
Correct |
25 ms |
2900 KB |
Output is correct |
4 |
Correct |
9 ms |
724 KB |
Output is correct |
5 |
Correct |
31 ms |
3688 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
29 ms |
3780 KB |
Output is correct |
9 |
Correct |
25 ms |
3724 KB |
Output is correct |
10 |
Correct |
28 ms |
3792 KB |
Output is correct |
11 |
Correct |
27 ms |
3692 KB |
Output is correct |
12 |
Correct |
27 ms |
3784 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
28 ms |
3864 KB |
Output is correct |
20 |
Correct |
28 ms |
3792 KB |
Output is correct |
21 |
Correct |
31 ms |
3696 KB |
Output is correct |
22 |
Correct |
28 ms |
3660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
724 KB |
Output is correct |
2 |
Correct |
9 ms |
724 KB |
Output is correct |
3 |
Correct |
11 ms |
748 KB |
Output is correct |
4 |
Correct |
10 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
9 ms |
720 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
25 ms |
788 KB |
Output is correct |
11 |
Correct |
30 ms |
852 KB |
Output is correct |
12 |
Correct |
30 ms |
852 KB |
Output is correct |
13 |
Correct |
10 ms |
756 KB |
Output is correct |
14 |
Correct |
10 ms |
852 KB |
Output is correct |
15 |
Correct |
10 ms |
724 KB |
Output is correct |
16 |
Correct |
9 ms |
752 KB |
Output is correct |
17 |
Correct |
33 ms |
3716 KB |
Output is correct |
18 |
Correct |
34 ms |
3748 KB |
Output is correct |
19 |
Correct |
34 ms |
3860 KB |
Output is correct |
20 |
Correct |
9 ms |
852 KB |
Output is correct |
21 |
Correct |
10 ms |
744 KB |
Output is correct |
22 |
Correct |
9 ms |
724 KB |
Output is correct |
23 |
Correct |
9 ms |
724 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
30 ms |
3776 KB |
Output is correct |
26 |
Correct |
26 ms |
3712 KB |
Output is correct |
27 |
Correct |
35 ms |
3788 KB |
Output is correct |
28 |
Correct |
33 ms |
3848 KB |
Output is correct |
29 |
Correct |
33 ms |
3796 KB |
Output is correct |
30 |
Correct |
33 ms |
4052 KB |
Output is correct |
31 |
Correct |
9 ms |
748 KB |
Output is correct |
32 |
Correct |
8 ms |
724 KB |
Output is correct |
33 |
Correct |
25 ms |
2900 KB |
Output is correct |
34 |
Correct |
9 ms |
724 KB |
Output is correct |
35 |
Correct |
31 ms |
3688 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
29 ms |
3780 KB |
Output is correct |
39 |
Correct |
25 ms |
3724 KB |
Output is correct |
40 |
Correct |
28 ms |
3792 KB |
Output is correct |
41 |
Correct |
27 ms |
3692 KB |
Output is correct |
42 |
Correct |
27 ms |
3784 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
28 ms |
3864 KB |
Output is correct |
50 |
Correct |
28 ms |
3792 KB |
Output is correct |
51 |
Correct |
31 ms |
3696 KB |
Output is correct |
52 |
Correct |
28 ms |
3660 KB |
Output is correct |
53 |
Correct |
36 ms |
3696 KB |
Output is correct |
54 |
Correct |
37 ms |
3792 KB |
Output is correct |
55 |
Correct |
35 ms |
3700 KB |
Output is correct |
56 |
Correct |
10 ms |
724 KB |
Output is correct |
57 |
Correct |
10 ms |
724 KB |
Output is correct |
58 |
Correct |
156 ms |
3180 KB |
Output is correct |
59 |
Correct |
184 ms |
3152 KB |
Output is correct |
60 |
Correct |
247 ms |
3160 KB |
Output is correct |
61 |
Correct |
157 ms |
3172 KB |
Output is correct |
62 |
Correct |
184 ms |
3276 KB |
Output is correct |
63 |
Correct |
246 ms |
3168 KB |
Output is correct |
64 |
Correct |
632 ms |
3636 KB |
Output is correct |
65 |
Correct |
679 ms |
3680 KB |
Output is correct |
66 |
Correct |
923 ms |
3656 KB |
Output is correct |
67 |
Correct |
14 ms |
3660 KB |
Output is correct |
68 |
Correct |
28 ms |
4940 KB |
Output is correct |
69 |
Correct |
28 ms |
4852 KB |
Output is correct |
70 |
Correct |
36 ms |
4964 KB |
Output is correct |
71 |
Correct |
34 ms |
4940 KB |
Output is correct |
72 |
Correct |
33 ms |
4904 KB |
Output is correct |
73 |
Correct |
38 ms |
5016 KB |
Output is correct |
74 |
Correct |
39 ms |
4988 KB |
Output is correct |