#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
vector < pair <int,int> > g[205];
int dis(int u,int u2,int n,int a,int b,int c){
vector <int> d(n+1,1e10);
d[u]=0;
priority_queue <pair <int,int> > pq;
pq.push({0,u});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(v==b){
if(d[a]>d[b]+c){
d[a]=d[b]+c;
pq.push({d[a],a});
}
}
for(auto to : g[v]){
if(v==a && b==to.ff && c==to.ss)continue;
if(d[to.ff]>d[v]+to.ss){
d[to.ff]=d[v]+to.ss;
pq.push({d[to.ff],to.ff});
}
}
}
return d[u2];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
vector <vector <int> > dist(n+1,vector <int> (n+1,1e10));
vector <int> a(m),b(m),c(m),d(m);
for(int i=0;i<m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
g[a[i]].push_back({b[i],c[i]});
dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
}
for(int i=1;i<=n;i++)dist[i][i]=0;
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
int mn=1e10;
mn=min(mn,dist[1][n]+dist[n][1]);
for(int i=1;i<=m;i++){
if(min(dist[1][n],dist[1][b[i]]+dist[a[i]][n]+c[i])+min(dist[n][1],dist[n][b[i]]+dist[a[i]][1]+c[i])+d[i]>=mn)continue;
int x=dis(1,n,n,a[i],b[i],c[i]);
int y=dis(n,1,n,a[i],b[i],c[i]);
mn=min(mn,x+y+d[i]);
}
if(mn==1e10)mn=-1;
cout<<mn<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
5 ms |
848 KB |
Output is correct |
4 |
Correct |
5 ms |
836 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
72 ms |
844 KB |
Output is correct |
11 |
Correct |
311 ms |
604 KB |
Output is correct |
12 |
Correct |
16 ms |
604 KB |
Output is correct |
13 |
Correct |
7 ms |
844 KB |
Output is correct |
14 |
Correct |
5 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
604 KB |
Output is correct |
16 |
Correct |
6 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
3636 KB |
Output is correct |
2 |
Correct |
24 ms |
3748 KB |
Output is correct |
3 |
Correct |
20 ms |
3584 KB |
Output is correct |
4 |
Incorrect |
7 ms |
856 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
14 ms |
2908 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
16 ms |
3504 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
16 ms |
3420 KB |
Output is correct |
9 |
Correct |
16 ms |
3676 KB |
Output is correct |
10 |
Correct |
14 ms |
3672 KB |
Output is correct |
11 |
Correct |
15 ms |
3676 KB |
Output is correct |
12 |
Correct |
16 ms |
3676 KB |
Output is correct |
13 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
5 ms |
848 KB |
Output is correct |
4 |
Correct |
5 ms |
836 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
72 ms |
844 KB |
Output is correct |
11 |
Correct |
311 ms |
604 KB |
Output is correct |
12 |
Correct |
16 ms |
604 KB |
Output is correct |
13 |
Correct |
7 ms |
844 KB |
Output is correct |
14 |
Correct |
5 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
604 KB |
Output is correct |
16 |
Correct |
6 ms |
604 KB |
Output is correct |
17 |
Correct |
17 ms |
3636 KB |
Output is correct |
18 |
Correct |
24 ms |
3748 KB |
Output is correct |
19 |
Correct |
20 ms |
3584 KB |
Output is correct |
20 |
Incorrect |
7 ms |
856 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |