#include<bits/stdc++.h>
using namespace std;
#define int long long
const long long inf=2e13;
const int N=2e2+5;
const int M=5e4+5;
int n,m,a[N];
int d[3][N][N];
int cost[M];
int curd[N],p[N];
vector<int >g[N];
vector<int >path1,path2;
priority_queue<pair<long long,long long>>q;
int is[2][M];
struct edge{
int u,v,c,d;
edge(){
u=v=c=d=0;
}
};
edge e[M];
void recover(int v,vector<int >&path){
while(p[v]!=-1){
path.push_back(p[v]);
v=e[p[v]].u;
}
reverse(path.begin(),path.end());
}
void dxtra(int s){
fill(curd,curd+N,inf);
fill(p,p+N,-1);
q.push(make_pair(0,s));
curd[s]=0;
while(!q.empty()){
int d=-q.top().first,v=q.top().second;
q.pop();
if(d>curd[v])
continue;
for(int ind:g[v]){
int to=e[ind].v,w=e[ind].c;
if(curd[to]>curd[v]+w){
p[to]=ind;
curd[to]=curd[v]+w;
q.push(make_pair(-curd[to],to));
}
}
}
}
void calc_distance(){
for(int i=0;i<3;++i)
for(int j=0;j<N;++j)
for(int k=0;k<N;++k)
if(j!=k)
d[i][j][k]=inf;
for(int i=1;i<=m;++i)
d[0][e[i].u][e[i].v]=min(d[0][e[i].u][e[i].v],e[i].c);
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
d[0][i][j]=min(d[0][i][j],d[0][i][k]+d[0][k][j]);
for(int t=0;t<2;++t){
for(int i=1;i<=m;++i)
if(!is[t][i])
d[t+1][e[i].u][e[i].v]=min(d[t+1][e[i].u][e[i].v],e[i].c);
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
d[t+1][i][j]=min(d[t+1][i][j],d[t+1][i][k]+d[t+1][k][j]);
}
}
main(){
cin>>n>>m;
for(int i=1;i<=m;++i){
cin>>e[i].u>>e[i].v>>e[i].c>>e[i].d;
g[e[i].u].push_back(i);
}
dxtra(1);
if(curd[n]!=inf)
recover(n,path1);
dxtra(n);
if(curd[1]!=inf)
recover(1,path2);
for(int i=0;i<path1.size();++i)
is[0][path1[i]]=i+1;
for(int i=0;i<path2.size();++i)
is[1][path2[i]]=i+1;
calc_distance();
for(int i=1;i<=m;++i){
int u=e[i].u,v=e[i].v,c=e[i].c;
if(!is[0][i])
cost[i]+=min(d[0][1][n],d[0][1][v]+d[0][u][n]+e[i].c);
else{
int j=is[0][i]-1;
int now=inf;
for(int i=0;i<=j;++i){
for(int k=j;k<path1.size();++k){
int u=e[path1[i]].u,v=e[path1[k]].v;
now=min(now,d[0][1][u]+d[1][u][v]+d[0][v][n]);
}
}
cost[i]+=now;
}
}
for(int i=1;i<=m;++i){
int u=e[i].u,v=e[i].v,c=e[i].c;
if(!is[1][i])
cost[i]+=min(d[0][n][1],d[0][n][v]+d[0][u][1]+e[i].c);
else{
int j=is[1][i]-1;
int now=inf;
for(int i=0;i<=j;++i)
for(int k=j;k<path2.size();++k){
int u=e[path2[i]].u,v=e[path2[k]].v;
now=min(now,d[0][n][u]+d[2][u][v]+d[0][v][1]);
}
cost[i]+=now;
}
}
int ans=d[0][1][n]+d[0][n][1];
for(int i=1;i<=m;++i)
ans=min(ans,cost[i]+e[i].d);
if(ans>=inf)
cout<<-1<<endl;
else
cout<<ans<<endl;
return 0;
}
Compilation message
ho_t4.cpp:85:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:100:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path1.size();++i)
~^~~~~~~~~~~~~
ho_t4.cpp:102:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path2.size();++i)
~^~~~~~~~~~~~~
ho_t4.cpp:115:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=j;k<path1.size();++k){
~^~~~~~~~~~~~~
ho_t4.cpp:108:25: warning: unused variable 'c' [-Wunused-variable]
int u=e[i].u,v=e[i].v,c=e[i].c;
^
ho_t4.cpp:132:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k=j;k<path2.size();++k){
~^~~~~~~~~~~~~
ho_t4.cpp:125:25: warning: unused variable 'c' [-Wunused-variable]
int u=e[i].u,v=e[i].v,c=e[i].c;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2936 KB |
Output is correct |
2 |
Correct |
34 ms |
2936 KB |
Output is correct |
3 |
Correct |
35 ms |
2936 KB |
Output is correct |
4 |
Correct |
36 ms |
2936 KB |
Output is correct |
5 |
Correct |
8 ms |
2936 KB |
Output is correct |
6 |
Correct |
35 ms |
2936 KB |
Output is correct |
7 |
Correct |
6 ms |
2936 KB |
Output is correct |
8 |
Correct |
7 ms |
2936 KB |
Output is correct |
9 |
Correct |
8 ms |
2940 KB |
Output is correct |
10 |
Correct |
42 ms |
2936 KB |
Output is correct |
11 |
Correct |
43 ms |
2936 KB |
Output is correct |
12 |
Correct |
42 ms |
2936 KB |
Output is correct |
13 |
Correct |
36 ms |
2936 KB |
Output is correct |
14 |
Correct |
35 ms |
2936 KB |
Output is correct |
15 |
Correct |
35 ms |
2936 KB |
Output is correct |
16 |
Correct |
35 ms |
2936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
3924 KB |
Output is correct |
2 |
Correct |
109 ms |
3960 KB |
Output is correct |
3 |
Correct |
121 ms |
3964 KB |
Output is correct |
4 |
Correct |
40 ms |
3064 KB |
Output is correct |
5 |
Correct |
35 ms |
2936 KB |
Output is correct |
6 |
Correct |
35 ms |
2936 KB |
Output is correct |
7 |
Correct |
34 ms |
2936 KB |
Output is correct |
8 |
Correct |
6 ms |
2808 KB |
Output is correct |
9 |
Correct |
118 ms |
3960 KB |
Output is correct |
10 |
Correct |
118 ms |
3960 KB |
Output is correct |
11 |
Correct |
120 ms |
3924 KB |
Output is correct |
12 |
Correct |
118 ms |
3960 KB |
Output is correct |
13 |
Correct |
118 ms |
3960 KB |
Output is correct |
14 |
Correct |
122 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2936 KB |
Output is correct |
2 |
Correct |
33 ms |
2936 KB |
Output is correct |
3 |
Correct |
89 ms |
3704 KB |
Output is correct |
4 |
Correct |
35 ms |
2936 KB |
Output is correct |
5 |
Correct |
107 ms |
3832 KB |
Output is correct |
6 |
Correct |
6 ms |
2936 KB |
Output is correct |
7 |
Correct |
6 ms |
2936 KB |
Output is correct |
8 |
Correct |
106 ms |
3836 KB |
Output is correct |
9 |
Correct |
107 ms |
3832 KB |
Output is correct |
10 |
Correct |
107 ms |
3960 KB |
Output is correct |
11 |
Correct |
110 ms |
3836 KB |
Output is correct |
12 |
Correct |
106 ms |
3960 KB |
Output is correct |
13 |
Correct |
6 ms |
2936 KB |
Output is correct |
14 |
Correct |
6 ms |
2936 KB |
Output is correct |
15 |
Correct |
6 ms |
2936 KB |
Output is correct |
16 |
Correct |
6 ms |
2936 KB |
Output is correct |
17 |
Correct |
6 ms |
2936 KB |
Output is correct |
18 |
Correct |
6 ms |
2936 KB |
Output is correct |
19 |
Correct |
107 ms |
3960 KB |
Output is correct |
20 |
Correct |
105 ms |
3960 KB |
Output is correct |
21 |
Correct |
106 ms |
3960 KB |
Output is correct |
22 |
Correct |
107 ms |
3964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2936 KB |
Output is correct |
2 |
Correct |
34 ms |
2936 KB |
Output is correct |
3 |
Correct |
35 ms |
2936 KB |
Output is correct |
4 |
Correct |
36 ms |
2936 KB |
Output is correct |
5 |
Correct |
8 ms |
2936 KB |
Output is correct |
6 |
Correct |
35 ms |
2936 KB |
Output is correct |
7 |
Correct |
6 ms |
2936 KB |
Output is correct |
8 |
Correct |
7 ms |
2936 KB |
Output is correct |
9 |
Correct |
8 ms |
2940 KB |
Output is correct |
10 |
Correct |
42 ms |
2936 KB |
Output is correct |
11 |
Correct |
43 ms |
2936 KB |
Output is correct |
12 |
Correct |
42 ms |
2936 KB |
Output is correct |
13 |
Correct |
36 ms |
2936 KB |
Output is correct |
14 |
Correct |
35 ms |
2936 KB |
Output is correct |
15 |
Correct |
35 ms |
2936 KB |
Output is correct |
16 |
Correct |
35 ms |
2936 KB |
Output is correct |
17 |
Correct |
110 ms |
3924 KB |
Output is correct |
18 |
Correct |
109 ms |
3960 KB |
Output is correct |
19 |
Correct |
121 ms |
3964 KB |
Output is correct |
20 |
Correct |
40 ms |
3064 KB |
Output is correct |
21 |
Correct |
35 ms |
2936 KB |
Output is correct |
22 |
Correct |
35 ms |
2936 KB |
Output is correct |
23 |
Correct |
34 ms |
2936 KB |
Output is correct |
24 |
Correct |
6 ms |
2808 KB |
Output is correct |
25 |
Correct |
118 ms |
3960 KB |
Output is correct |
26 |
Correct |
118 ms |
3960 KB |
Output is correct |
27 |
Correct |
120 ms |
3924 KB |
Output is correct |
28 |
Correct |
118 ms |
3960 KB |
Output is correct |
29 |
Correct |
118 ms |
3960 KB |
Output is correct |
30 |
Correct |
122 ms |
4088 KB |
Output is correct |
31 |
Correct |
35 ms |
2936 KB |
Output is correct |
32 |
Correct |
33 ms |
2936 KB |
Output is correct |
33 |
Correct |
89 ms |
3704 KB |
Output is correct |
34 |
Correct |
35 ms |
2936 KB |
Output is correct |
35 |
Correct |
107 ms |
3832 KB |
Output is correct |
36 |
Correct |
6 ms |
2936 KB |
Output is correct |
37 |
Correct |
6 ms |
2936 KB |
Output is correct |
38 |
Correct |
106 ms |
3836 KB |
Output is correct |
39 |
Correct |
107 ms |
3832 KB |
Output is correct |
40 |
Correct |
107 ms |
3960 KB |
Output is correct |
41 |
Correct |
110 ms |
3836 KB |
Output is correct |
42 |
Correct |
106 ms |
3960 KB |
Output is correct |
43 |
Correct |
6 ms |
2936 KB |
Output is correct |
44 |
Correct |
6 ms |
2936 KB |
Output is correct |
45 |
Correct |
6 ms |
2936 KB |
Output is correct |
46 |
Correct |
6 ms |
2936 KB |
Output is correct |
47 |
Correct |
6 ms |
2936 KB |
Output is correct |
48 |
Correct |
6 ms |
2936 KB |
Output is correct |
49 |
Correct |
107 ms |
3960 KB |
Output is correct |
50 |
Correct |
105 ms |
3960 KB |
Output is correct |
51 |
Correct |
106 ms |
3960 KB |
Output is correct |
52 |
Correct |
107 ms |
3964 KB |
Output is correct |
53 |
Correct |
115 ms |
4032 KB |
Output is correct |
54 |
Correct |
112 ms |
3960 KB |
Output is correct |
55 |
Correct |
107 ms |
3960 KB |
Output is correct |
56 |
Correct |
35 ms |
2936 KB |
Output is correct |
57 |
Correct |
35 ms |
2936 KB |
Output is correct |
58 |
Correct |
111 ms |
4344 KB |
Output is correct |
59 |
Correct |
105 ms |
4176 KB |
Output is correct |
60 |
Correct |
103 ms |
4216 KB |
Output is correct |
61 |
Correct |
100 ms |
4216 KB |
Output is correct |
62 |
Correct |
103 ms |
4344 KB |
Output is correct |
63 |
Correct |
103 ms |
4216 KB |
Output is correct |
64 |
Correct |
110 ms |
4580 KB |
Output is correct |
65 |
Correct |
109 ms |
4604 KB |
Output is correct |
66 |
Correct |
112 ms |
4604 KB |
Output is correct |
67 |
Correct |
70 ms |
3696 KB |
Output is correct |
68 |
Correct |
120 ms |
4020 KB |
Output is correct |
69 |
Correct |
117 ms |
3960 KB |
Output is correct |
70 |
Correct |
117 ms |
3960 KB |
Output is correct |
71 |
Correct |
121 ms |
3960 KB |
Output is correct |
72 |
Correct |
118 ms |
3964 KB |
Output is correct |
73 |
Correct |
123 ms |
3960 KB |
Output is correct |
74 |
Correct |
118 ms |
3960 KB |
Output is correct |