#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF=1e17;
struct graph{
vector<vector<pair<int,int> > >adi;
vector<int> c;
vector<int> d;
graph(int n, int m){
adi.assign(n, {});
c.assign(m, 0);
d.assign(m, 0);
}
};
void dijkstra(int start, vector<int>& dist, vector<int>& par, int n, graph& g){
dist.assign(n, INF);
par.assign(n, -1);
vector<bool> vis(n, false);
priority_queue<pair<int,int> > pq;
dist[start]=0;
pq.push({-0, start});
while(!pq.empty()){
int v=pq.top().second;
pq.pop();
if(vis[v]){
continue;
}
vis[v]=true;
for(auto [u, ind]: g.adi[v]){
if(dist[v]+g.c[ind] < dist[u]){
par[u]=ind;
dist[u] = dist[v] + g.c[ind];
pq.push({-dist[u], u});
}
}
}
}
vector<int> find_sp(int a, int b, vector<int>& par, vector<pair<int,int> >& edge, int m){
vector<int> p;
if(par[b]==-1){
return {};
}
int v=b;
while(v!=a){
p.push_back(par[v]);
v=edge[par[v]].first;
}
reverse(p.begin(), p.end());
return p;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
graph g(n, m);
graph rev(n, m);
vector<pair<int,int> > edge(m);
for(int i=0; i<m; i++){
int a, b;
cin >> a >> b >> g.c[i] >> g.d[i];
a--; b--;
g.adi[a].push_back({b, i});
rev.c[i]=g.c[i];
rev.d[i]=g.d[i];
rev.adi[b].push_back({a, i});
edge[i]={a, b};
}
//distances
vector<int> par0(n, -1);
vector<int> to0(n, INF);
dijkstra(0, to0, par0, n, rev);
vector<int> from0(n, INF);
dijkstra(0, from0, par0, n, g);
vector<int> parn(n, -1);
vector<int> ton(n, INF);
dijkstra(n-1, ton, parn, n, rev);
vector<int> fromn(n, INF);
dijkstra(n-1, fromn, parn, n, g);
//paths
vector<int> path1=find_sp(0, n-1, par0, edge, m);
vector<int> path2=find_sp(n-1, 0, parn, edge, m);
vector<bool> used(m, false);
for(auto e: path1){
used[e]=true;
}
for(auto e: path2){
used[e]=true;
}
//possibility 0: turning no edge
int ans=INF;
ans = min(ans, from0[n-1] + fromn[0]);
//possibility 1: edge not on path
for(int i=0; i<m; i++){
if(used[i]) continue;
auto [a, b]=edge[i];
int d1=min(from0[b] + g.c[i] + ton[a], from0[n-1]);
int d2=min(fromn[b] + g.c[i] + to0[a], fromn[0]);
int d=d1 + d2 + g.d[i];
ans=min(ans, d);
}
//possibility 2: edge on path
vector<int> path;
for(int i=0; i<m; i++){
if(used[i]) path.push_back(i);
}
for(auto e: path){
graph neu(n, m);
for(int i=0; i<m; i++){
neu.c[i]=g.c[i];
neu.d[i]=g.d[i];
auto [a, b]=edge[i];
if(i==e){
neu.adi[b].push_back({a, i});
}
else{
neu.adi[a].push_back({b, i});
}
}
int dis=g.d[e];
vector<int> dist(n, INF);
vector<int> p(n, -1);
dijkstra(0, dist, p, n, neu);
dis += dist[n-1];
dijkstra(n-1, dist, p, n, neu);
dis+=dist[0];
ans=min(ans, dis);
}
//output
if(ans==INF){
cout << -1 << "\n";
}
else{
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
32 ms |
508 KB |
Output is correct |
11 |
Correct |
53 ms |
488 KB |
Output is correct |
12 |
Correct |
41 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
7172 KB |
Output is correct |
2 |
Correct |
35 ms |
7204 KB |
Output is correct |
3 |
Correct |
38 ms |
7244 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
19 ms |
5156 KB |
Output is correct |
10 |
Correct |
22 ms |
6344 KB |
Output is correct |
11 |
Correct |
47 ms |
8380 KB |
Output is correct |
12 |
Correct |
36 ms |
8364 KB |
Output is correct |
13 |
Correct |
40 ms |
8320 KB |
Output is correct |
14 |
Correct |
46 ms |
8892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
22 ms |
5436 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
27 ms |
7004 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
18 ms |
5204 KB |
Output is correct |
9 |
Correct |
20 ms |
6160 KB |
Output is correct |
10 |
Correct |
31 ms |
7980 KB |
Output is correct |
11 |
Correct |
23 ms |
8036 KB |
Output is correct |
12 |
Correct |
26 ms |
8108 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
32 ms |
8432 KB |
Output is correct |
20 |
Correct |
25 ms |
8040 KB |
Output is correct |
21 |
Correct |
23 ms |
8008 KB |
Output is correct |
22 |
Correct |
27 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
32 ms |
508 KB |
Output is correct |
11 |
Correct |
53 ms |
488 KB |
Output is correct |
12 |
Correct |
41 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
456 KB |
Output is correct |
16 |
Correct |
2 ms |
456 KB |
Output is correct |
17 |
Correct |
39 ms |
7172 KB |
Output is correct |
18 |
Correct |
35 ms |
7204 KB |
Output is correct |
19 |
Correct |
38 ms |
7244 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
19 ms |
5156 KB |
Output is correct |
26 |
Correct |
22 ms |
6344 KB |
Output is correct |
27 |
Correct |
47 ms |
8380 KB |
Output is correct |
28 |
Correct |
36 ms |
8364 KB |
Output is correct |
29 |
Correct |
40 ms |
8320 KB |
Output is correct |
30 |
Correct |
46 ms |
8892 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
22 ms |
5436 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
27 ms |
7004 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
18 ms |
5204 KB |
Output is correct |
39 |
Correct |
20 ms |
6160 KB |
Output is correct |
40 |
Correct |
31 ms |
7980 KB |
Output is correct |
41 |
Correct |
23 ms |
8036 KB |
Output is correct |
42 |
Correct |
26 ms |
8108 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
316 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
32 ms |
8432 KB |
Output is correct |
50 |
Correct |
25 ms |
8040 KB |
Output is correct |
51 |
Correct |
23 ms |
8008 KB |
Output is correct |
52 |
Correct |
27 ms |
7892 KB |
Output is correct |
53 |
Correct |
34 ms |
7984 KB |
Output is correct |
54 |
Correct |
50 ms |
8256 KB |
Output is correct |
55 |
Correct |
41 ms |
8076 KB |
Output is correct |
56 |
Correct |
3 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
328 ms |
6512 KB |
Output is correct |
59 |
Correct |
386 ms |
6556 KB |
Output is correct |
60 |
Correct |
502 ms |
6524 KB |
Output is correct |
61 |
Correct |
338 ms |
6444 KB |
Output is correct |
62 |
Correct |
332 ms |
6452 KB |
Output is correct |
63 |
Correct |
611 ms |
6576 KB |
Output is correct |
64 |
Correct |
899 ms |
7344 KB |
Output is correct |
65 |
Execution timed out |
1073 ms |
7336 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |