#include <bits/stdc++.h>
using namespace std;
long long inf = (1LL << 56LL);
typedef pair<long long, long long> ii;
vector<ii> adj[205];
vector<ii> adjR[205];
long long dis[205];
long long dis1[205];
long long disn[205];
long long dis1R[205];
long long disnR[205];
int n, m;
long long dij(int s, int t){
fill(dis, dis + (n+3), inf);
priority_queue<ii, vector<ii>, greater<ii> > dtr; /// use greater<int>
dtr.push(ii(0,s));
dis[s] = 0;
///first is minpath, second is vertex
while(!dtr.empty()){
ii cur = dtr.top();
dtr.pop();
for(int j = 0;j < adj[cur.second].size();j++){
ii nei = adj[cur.second][j];
if(nei.first + cur.first < dis[nei.second]){
dis[nei.second] = nei.first + cur.first;
dtr.push(ii(dis[nei.second],nei.second));
}
}
}
return dis[t];
}
long long dijR(int s, int t){
fill(dis, dis + (n+3), inf);
priority_queue<ii, vector<ii>, greater<ii> > dtr; /// use greater<int>
dtr.push(ii(0,s));
dis[s] = 0;
///first is minpath, second is vertex
while(!dtr.empty()){
ii cur = dtr.top();
dtr.pop();
for(int j = 0;j < adjR[cur.second].size();j++){
ii nei = adjR[cur.second][j];
if(nei.first + cur.first < dis[nei.second]){
dis[nei.second] = nei.first + cur.first;
dtr.push(ii(dis[nei.second],nei.second));
}
}
}
return dis[t];
}
struct edge{
long long u; long long v; long long c; long long d;
};
vector<edge> edges;
void prin(long long ans){
if(ans >= inf) ans = -1;
cout << ans;
exit(0);
}
void subtask1(){
long long ans = dij(1, n)+ dij(n, 1);
for(int i = 0;i < m;i++){
for(int i = 1;i <= n;i++) adj[i].clear();
long long res = 0;
for(int j = 0;j < m;j++){
edge e = edges[j];
if(i == j){
adj[e.v].push_back(ii(e.c,e.u));
res += e.d;
}
else adj[e.u].push_back(ii(e.c,e.v));
}
//cout << dij(1,n) + dij(n,1) + res << "\n";
ans = min(ans, dij(1,n) + dij(n,1) + res);
}
prin(ans);
}
int main(){
//freopen("i.txt","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0;i < m;i++){
long long u, v, c, d; cin >> u >> v >> c >> d;
edges.push_back({u,v,c,d});
adj[u].push_back(ii(c,v));
adjR[v].push_back(ii(c,u));
}
if(m <= 1000) subtask1();
dij(1,n);
for(int i = 1;i <= n;i++) dis1[i] = dis[i];
dij(n,1);
for(int i = 1;i <= n;i++) disn[i] = dis[i];
dijR(1,n);
for(int i = 1;i <= n;i++) dis1R[i] = dis[i];
dijR(n,1);
for(int i = 1;i <= n;i++) disnR[i] = dis[i];
long long ans = dis1[n] + disn[1];
for(edge e : edges){
///edge goes v -> u
ans = min(ans, e.d + dis1[n] + disn[e.v] + dis1R[e.u] + e.c);
ans = min(ans, e.d + disn[1] + dis1[e.v] + disnR[e.u] + e.c);
ans = min(ans, e.d + dis1[e.v] + disnR[e.u] + disn[e.v] + dis1R[e.u] + 2 * e.c);
}
prin(ans);
}
Compilation message
ho_t4.cpp: In function 'long long int dij(int, int)':
ho_t4.cpp:27:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0;j < adj[cur.second].size();j++){
~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp: In function 'long long int dijR(int, int)':
ho_t4.cpp:48:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0;j < adjR[cur.second].size();j++){
~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
68 ms |
384 KB |
Output is correct |
4 |
Correct |
72 ms |
384 KB |
Output is correct |
5 |
Correct |
36 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
19 ms |
384 KB |
Output is correct |
10 |
Correct |
101 ms |
384 KB |
Output is correct |
11 |
Correct |
102 ms |
384 KB |
Output is correct |
12 |
Correct |
99 ms |
504 KB |
Output is correct |
13 |
Correct |
41 ms |
384 KB |
Output is correct |
14 |
Correct |
54 ms |
384 KB |
Output is correct |
15 |
Correct |
50 ms |
384 KB |
Output is correct |
16 |
Correct |
51 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
4444 KB |
Output is correct |
2 |
Correct |
30 ms |
4592 KB |
Output is correct |
3 |
Correct |
31 ms |
4592 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
46 ms |
384 KB |
Output is correct |
6 |
Correct |
9 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
29 ms |
4464 KB |
Output is correct |
10 |
Correct |
33 ms |
4584 KB |
Output is correct |
11 |
Correct |
31 ms |
4592 KB |
Output is correct |
12 |
Correct |
31 ms |
4456 KB |
Output is correct |
13 |
Correct |
32 ms |
4456 KB |
Output is correct |
14 |
Correct |
33 ms |
4840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
384 KB |
Output is correct |
3 |
Correct |
23 ms |
4240 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
26 ms |
4464 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
31 ms |
4464 KB |
Output is correct |
9 |
Correct |
30 ms |
4452 KB |
Output is correct |
10 |
Incorrect |
27 ms |
4456 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
68 ms |
384 KB |
Output is correct |
4 |
Correct |
72 ms |
384 KB |
Output is correct |
5 |
Correct |
36 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
19 ms |
384 KB |
Output is correct |
10 |
Correct |
101 ms |
384 KB |
Output is correct |
11 |
Correct |
102 ms |
384 KB |
Output is correct |
12 |
Correct |
99 ms |
504 KB |
Output is correct |
13 |
Correct |
41 ms |
384 KB |
Output is correct |
14 |
Correct |
54 ms |
384 KB |
Output is correct |
15 |
Correct |
50 ms |
384 KB |
Output is correct |
16 |
Correct |
51 ms |
512 KB |
Output is correct |
17 |
Correct |
30 ms |
4444 KB |
Output is correct |
18 |
Correct |
30 ms |
4592 KB |
Output is correct |
19 |
Correct |
31 ms |
4592 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
46 ms |
384 KB |
Output is correct |
22 |
Correct |
9 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
29 ms |
4464 KB |
Output is correct |
26 |
Correct |
33 ms |
4584 KB |
Output is correct |
27 |
Correct |
31 ms |
4592 KB |
Output is correct |
28 |
Correct |
31 ms |
4456 KB |
Output is correct |
29 |
Correct |
32 ms |
4456 KB |
Output is correct |
30 |
Correct |
33 ms |
4840 KB |
Output is correct |
31 |
Correct |
49 ms |
384 KB |
Output is correct |
32 |
Correct |
7 ms |
384 KB |
Output is correct |
33 |
Correct |
23 ms |
4240 KB |
Output is correct |
34 |
Correct |
6 ms |
384 KB |
Output is correct |
35 |
Correct |
26 ms |
4464 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
31 ms |
4464 KB |
Output is correct |
39 |
Correct |
30 ms |
4452 KB |
Output is correct |
40 |
Incorrect |
27 ms |
4456 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |