#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii,int> i3;
main(){
int n,m;
cin >> n >> m;
if (m <= 1000){
int ans = 1e18;
i3 edges[m];
int invert[m+1];
for (int i = 0; i < m; i++) {
cin >> edges[i].first.first >> edges[i].first.second >> edges[i].second;
cin >> invert[i];
}
invert[m] = 0;
for (int i = 0; i < m+1; i++){
int temp = invert[i];
vector<ii> tempadj[n+1];
for (int j = 0; j < m; j++){
if (j == i) tempadj[edges[j].first.second].push_back(ii(edges[j].first.first,edges[j].second));
else tempadj[edges[j].first.first].push_back(ii(edges[j].first.second,edges[j].second));
}
int dist1[n+1],dist2[n+1];
for (int j = 1; j <= n; j++) dist1[j] = dist2[j] = 1e18;
priority_queue<ii,vector<ii>,greater<ii> >pq;
pq.push(ii(0,1));
dist1[1] = 0;
while (!pq.empty()){
ii f = pq.top();
pq.pop();
int d = f.first, u = f.second;
if (d > dist1[u]) continue;
for (int j = 0; j < tempadj[u].size(); j++){
ii v = tempadj[u][j];
if (dist1[u]+v.second < dist1[v.first]){
dist1[v.first] = dist1[u]+v.second;
pq.push(ii(dist1[v.first],v.first));
}
}
}
pq.push(ii(0,n));
dist2[n] = 0;
while (!pq.empty()){
ii f = pq.top();
pq.pop();
int d = f.first, u = f.second;
if (d > dist2[u]) continue;
for (int j = 0; j < tempadj[u].size(); j++){
ii v = tempadj[u][j];
if (dist2[u]+v.second < dist2[v.first]){
dist2[v.first] = dist2[u]+v.second;
pq.push(ii(dist2[v.first],v.first));
}
}
}
ans = min(ans,temp+dist1[n]+dist2[1]);
}
if (ans == 1e18) cout << -1;
else cout << ans;
}
else{
i3 edges[m];
int invert[m+1];
vector<ii> adj[n+1];
vector<ii> radj[n+1];
for (int i = 0; i < m; i++) {
cin >> edges[i].first.first >> edges[i].first.second >> edges[i].second;
cin >> invert[i];
adj[edges[i].first.first].push_back(ii(edges[i].first.second,edges[i].second));
radj[edges[i].first.second].push_back(ii(edges[i].first.first,edges[i].second));
}
int dist1[n+1],dist2[n+1];
for (int j = 1; j <= n; j++) dist1[j] = dist2[j] = 1e18;
priority_queue<ii,vector<ii>,greater<ii> >pq;
pq.push(ii(0,1));
dist1[1] = 0;
while (!pq.empty()){
ii f = pq.top();
pq.pop();
int d = f.first, u = f.second;
if (d > dist1[u]) continue;
for (int j = 0; j < adj[u].size(); j++){
ii v = adj[u][j];
if (dist1[u]+v.second < dist1[v.first]){
dist1[v.first] = dist1[u]+v.second;
pq.push(ii(dist1[v.first],v.first));
}
}
}
pq.push(ii(0,n));
dist2[n] = 0;
while (!pq.empty()){
ii f = pq.top();
pq.pop();
int d = f.first, u = f.second;
if (d > dist2[u]) continue;
for (int j = 0; j < adj[u].size(); j++){
ii v = adj[u][j];
if (dist2[u]+v.second < dist2[v.first]){
dist2[v.first] = dist2[u]+v.second;
pq.push(ii(dist2[v.first],v.first));
}
}
}
int rdist1[n+1],rdist2[n+1];
for (int j = 1; j <= n; j++) rdist1[j] = rdist2[j] = 1e18;
pq.push(ii(0,1));
rdist1[1] = 0;
while (!pq.empty()){
ii f = pq.top();
pq.pop();
int d = f.first, u = f.second;
if (d > rdist1[u]) continue;
for (int j = 0; j < radj[u].size(); j++){
ii v = radj[u][j];
if (rdist1[u]+v.second < rdist1[v.first]){
rdist1[v.first] = rdist1[u]+v.second;
pq.push(ii(rdist1[v.first],v.first));
}
}
}
pq.push(ii(0,n));
rdist2[n] = 0;
while (!pq.empty()){
ii f = pq.top();
pq.pop();
int d = f.first, u = f.second;
if (d > rdist2[u]) continue;
for (int j = 0; j < radj[u].size(); j++){
ii v = radj[u][j];
if (rdist2[u]+v.second < rdist2[v.first]){
rdist2[v.first] = rdist2[u]+v.second;
pq.push(ii(rdist2[v.first],v.first));
}
}
}
//for (int i = 1; i <= n; i++) cout << dist2[i] <<" ";
//cout << "\n";
//for (int i = 1; i <= n; i++) cout << rdist2[i] <<" ";
//cout << "\n";
int ans = 1e18;
ans = min(ans,dist1[n]+dist2[1]);
for (int i = 0; i < m; i++){
int a = edges[i].first.first,b = edges[i].first.second,w = edges[i].second;
// cout << a << " " << b << " " << w<< "hi\n";
int temp1 = min(dist1[n],dist1[b]+w+rdist2[a]);
int temp2 = min(dist2[1],dist2[b]+w+rdist1[a]);
//cout << temp1 << " " << temp2 << " " << invert[i] << "\n";
ans = min(ans,temp1+temp2+invert[i]);
}
if (ans == 1e18) cout << -1;
else cout << ans;
}
}
Compilation message
ho_t4.cpp:6:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:35:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < tempadj[u].size(); j++){
~~^~~~~~~~~~~~~~~~~~~
ho_t4.cpp:50:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < tempadj[u].size(); j++){
~~^~~~~~~~~~~~~~~~~~~
ho_t4.cpp:84:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < adj[u].size(); j++){
~~^~~~~~~~~~~~~~~
ho_t4.cpp:99:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < adj[u].size(); j++){
~~^~~~~~~~~~~~~~~
ho_t4.cpp:116:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < radj[u].size(); j++){
~~^~~~~~~~~~~~~~~~
ho_t4.cpp:131:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < radj[u].size(); j++){
~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
384 KB |
Output is correct |
2 |
Correct |
8 ms |
384 KB |
Output is correct |
3 |
Correct |
106 ms |
504 KB |
Output is correct |
4 |
Correct |
111 ms |
384 KB |
Output is correct |
5 |
Correct |
43 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
21 ms |
384 KB |
Output is correct |
10 |
Correct |
135 ms |
384 KB |
Output is correct |
11 |
Correct |
133 ms |
384 KB |
Output is correct |
12 |
Correct |
136 ms |
504 KB |
Output is correct |
13 |
Correct |
81 ms |
384 KB |
Output is correct |
14 |
Correct |
94 ms |
504 KB |
Output is correct |
15 |
Correct |
89 ms |
384 KB |
Output is correct |
16 |
Correct |
90 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
4728 KB |
Output is correct |
2 |
Correct |
80 ms |
4780 KB |
Output is correct |
3 |
Correct |
90 ms |
4760 KB |
Output is correct |
4 |
Correct |
8 ms |
512 KB |
Output is correct |
5 |
Correct |
75 ms |
384 KB |
Output is correct |
6 |
Correct |
19 ms |
384 KB |
Output is correct |
7 |
Correct |
12 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
91 ms |
4600 KB |
Output is correct |
10 |
Correct |
88 ms |
4544 KB |
Output is correct |
11 |
Correct |
90 ms |
4728 KB |
Output is correct |
12 |
Correct |
91 ms |
4600 KB |
Output is correct |
13 |
Correct |
89 ms |
4596 KB |
Output is correct |
14 |
Correct |
89 ms |
4876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
384 KB |
Output is correct |
2 |
Correct |
9 ms |
384 KB |
Output is correct |
3 |
Correct |
61 ms |
3448 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
77 ms |
4600 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
81 ms |
4472 KB |
Output is correct |
9 |
Correct |
80 ms |
4472 KB |
Output is correct |
10 |
Incorrect |
77 ms |
4472 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
384 KB |
Output is correct |
2 |
Correct |
8 ms |
384 KB |
Output is correct |
3 |
Correct |
106 ms |
504 KB |
Output is correct |
4 |
Correct |
111 ms |
384 KB |
Output is correct |
5 |
Correct |
43 ms |
384 KB |
Output is correct |
6 |
Correct |
8 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
21 ms |
384 KB |
Output is correct |
10 |
Correct |
135 ms |
384 KB |
Output is correct |
11 |
Correct |
133 ms |
384 KB |
Output is correct |
12 |
Correct |
136 ms |
504 KB |
Output is correct |
13 |
Correct |
81 ms |
384 KB |
Output is correct |
14 |
Correct |
94 ms |
504 KB |
Output is correct |
15 |
Correct |
89 ms |
384 KB |
Output is correct |
16 |
Correct |
90 ms |
384 KB |
Output is correct |
17 |
Correct |
85 ms |
4728 KB |
Output is correct |
18 |
Correct |
80 ms |
4780 KB |
Output is correct |
19 |
Correct |
90 ms |
4760 KB |
Output is correct |
20 |
Correct |
8 ms |
512 KB |
Output is correct |
21 |
Correct |
75 ms |
384 KB |
Output is correct |
22 |
Correct |
19 ms |
384 KB |
Output is correct |
23 |
Correct |
12 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
91 ms |
4600 KB |
Output is correct |
26 |
Correct |
88 ms |
4544 KB |
Output is correct |
27 |
Correct |
90 ms |
4728 KB |
Output is correct |
28 |
Correct |
91 ms |
4600 KB |
Output is correct |
29 |
Correct |
89 ms |
4596 KB |
Output is correct |
30 |
Correct |
89 ms |
4876 KB |
Output is correct |
31 |
Correct |
89 ms |
384 KB |
Output is correct |
32 |
Correct |
9 ms |
384 KB |
Output is correct |
33 |
Correct |
61 ms |
3448 KB |
Output is correct |
34 |
Correct |
8 ms |
384 KB |
Output is correct |
35 |
Correct |
77 ms |
4600 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
81 ms |
4472 KB |
Output is correct |
39 |
Correct |
80 ms |
4472 KB |
Output is correct |
40 |
Incorrect |
77 ms |
4472 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |