#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
const ll INF=LLONG_MAX;
const int mxn=202;
ll dist1[mxn],dist2[mxn],d[50005];
vector<pair<int,ll>>adj[mxn];
int n;
struct flag{
int u,v; ll c;
flag(int u, int v, ll c):u(u),v(v),c(c){}
};
vector<flag>str;
//subtask 1
ll dij(int ind){
//set all dist = INF
for(int i=0; i<n; i++)
dist1[i]=dist2[i]=ll(1e18);
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>pq;
//from 1 to n;
dist1[0]=0LL;
pq.push({dist1[0],0});
while(!pq.empty()){
auto tp = pq.top();
pq.pop();
int u = tp.se;
if(u==n-1)break;
if(u==str[ind].v){
int v = str[ind].u;
ll w = str[ind].c;
if(dist1[v]>dist1[u]+w){
dist1[v] = dist1[u]+w;
pq.push({dist1[v],v});
}
}
for(auto x:adj[u]){
int v = x.fi;
ll w = x.se;
if(str[ind].u==u&&str[ind].v==v&&str[ind].c==w)continue;
if(dist1[v]>dist1[u]+w){
dist1[v] = dist1[u]+w;
pq.push({dist1[v],v});
}
}
}
//from n to 1;
dist2[n-1]=0LL;
pq.push({dist2[n-1],n-1});
while(!pq.empty()){
auto tp = pq.top();
pq.pop();
int u = tp.se;
if(u==0)break;
if(u==str[ind].v){
int v = str[ind].u;
ll w = str[ind].c;
if(dist2[v]>dist2[u]+w){
dist2[v] = dist2[u]+w;
pq.push({dist2[v],v});
}
}
for(auto x:adj[u]){
int v = x.fi;
ll w = x.se;
if(str[ind].u==u&&str[ind].v==v&&str[ind].c==w)continue;
if(dist2[v]>dist2[u]+w){
dist2[v] = dist2[u]+w;
pq.push({dist2[v],v});
}
}
}
if(dist1[n-1]==1e18||dist2[0]==1e18)return 2e18;
return dist1[n-1]+dist2[0];
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
int m;
cin >> n >> m;
str.eb(-1,-1,0);
d[0]=0LL;
for(int i=1; i<=m; i++){
int u,v; ll c;
cin >> u >> v >> c >> d[i];
u--; v--;
adj[u].eb(v,c);
str.eb(u,v,c);
}
ll ans = (ll)2e18;
for(int i=0; i<=m; i++){
ll now = dij(i);
ans = min(ans, now+d[i]);
}
if(ans==2e18)ans = -1;
cout << ans << endl;
}
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
34 ms |
384 KB |
Output is correct |
4 |
Correct |
42 ms |
384 KB |
Output is correct |
5 |
Correct |
16 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
512 KB |
Output is correct |
10 |
Correct |
83 ms |
384 KB |
Output is correct |
11 |
Correct |
79 ms |
384 KB |
Output is correct |
12 |
Correct |
82 ms |
384 KB |
Output is correct |
13 |
Correct |
29 ms |
384 KB |
Output is correct |
14 |
Correct |
38 ms |
512 KB |
Output is correct |
15 |
Correct |
25 ms |
384 KB |
Output is correct |
16 |
Correct |
39 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1069 ms |
3828 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Execution timed out |
1084 ms |
3192 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
34 ms |
384 KB |
Output is correct |
4 |
Correct |
42 ms |
384 KB |
Output is correct |
5 |
Correct |
16 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
512 KB |
Output is correct |
10 |
Correct |
83 ms |
384 KB |
Output is correct |
11 |
Correct |
79 ms |
384 KB |
Output is correct |
12 |
Correct |
82 ms |
384 KB |
Output is correct |
13 |
Correct |
29 ms |
384 KB |
Output is correct |
14 |
Correct |
38 ms |
512 KB |
Output is correct |
15 |
Correct |
25 ms |
384 KB |
Output is correct |
16 |
Correct |
39 ms |
384 KB |
Output is correct |
17 |
Execution timed out |
1069 ms |
3828 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |