#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;
const int N=5e4+5;
vector <pair <pair <int,int> ,int> > g[205];
int dist[205][205];
int a[N],b[N],c[N],d[N];
priority_queue <pair <int,int> > pq;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=1e9;
}
dist[i][i]=0;
}
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
g[a[i]].pb({{b[i],c[i]},i});
}
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=2e9;
if(dist[1][n]<1e9 && dist[n][1]<1e9)mn=min(dist[1][n]+dist[n][1],mn);
//cout<<mn<<"\n";
for(int i=1;i<=m;i++){
int new1n=min(dist[1][n],dist[1][b[i]]+dist[a[i]][n]+c[i]);
int newn1=min(dist[n][1],dist[n][b[i]]+dist[a[i]][1]+c[i]);
if(new1n==1e9 or newn1==1e9 or new1n+newn1+d[i]>=mn)continue;
vector <int> dis(n+1,1e9),vis(n+1);
dis[1]=0;
pq.push({0,1});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(vis[v])continue;
vis[v]=1;
if(v==b[i]){
if(dis[a[i]]>dis[b[i]]+c[i]){
dis[a[i]]=dis[b[i]]+c[i];
pq.push({dis[a[i]],a[i]});
}
}
for(auto to : g[v]){
if(to.ss==i)continue;
if(dis[to.ff.ff]>dis[v]+to.ff.ss){
dis[to.ff.ff]=dis[v]+to.ff.ss;
pq.push({dis[to.ff.ff],to.ff.ff});
}
}
}
if(dis[n]!=1e9){
int ans=dis[n];
for(int j=1;j<=n;j++){
dis[j]=1e9;
vis[j]=0;
}
dis[n]=0;
pq.push({0,n});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(vis[v])continue;
vis[v]=1;
if(v==b[i]){
if(dis[a[i]]>dis[b[i]]+c[i]){
dis[a[i]]=dis[b[i]]+c[i];
pq.push({dis[a[i]],a[i]});
}
}
for(auto to : g[v]){
if(to.ss==i)continue;
if(dis[to.ff.ff]>dis[v]+to.ff.ss){
dis[to.ff.ff]=dis[v]+to.ff.ss;
pq.push({dis[to.ff.ff],to.ff.ff});
}
}
}
if(dis[1]<1e9 && d[i]<1e9)mn=min(mn,ans+dis[1]+d[i]);
}
}
if(mn==2e9)mn=-1;
cout<<mn<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
660 KB |
Output is correct |
3 |
Correct |
9 ms |
604 KB |
Output is correct |
4 |
Correct |
11 ms |
692 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
19 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
15 ms |
1960 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
17 ms |
2200 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
19 ms |
2140 KB |
Output is correct |
9 |
Correct |
20 ms |
2396 KB |
Output is correct |
10 |
Correct |
17 ms |
2396 KB |
Output is correct |
11 |
Correct |
17 ms |
2392 KB |
Output is correct |
12 |
Correct |
27 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
20 ms |
2232 KB |
Output is correct |
20 |
Correct |
18 ms |
2336 KB |
Output is correct |
21 |
Correct |
18 ms |
2392 KB |
Output is correct |
22 |
Correct |
23 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
7 ms |
660 KB |
Output is correct |
3 |
Correct |
9 ms |
604 KB |
Output is correct |
4 |
Correct |
11 ms |
692 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |