#define ONLINE_JUDGE
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
#define int long long
#define all(x) x.begin(), x.end()
typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef pair<int, int> pii; typedef map<int, int> mii; typedef map<int, vector<int>> mivi;
const int mod = 1000000007; // 10^9 + 7
const int INF = 1e17;
#define MAXN 200
#define MAXM 50000
struct Edge {
int u, v, c, d;
};
int n, m;
Edge edges[MAXM];
multiset<pii> adj[MAXN];
int dist[MAXN][MAXN];
int cost(){
priority_queue<pii> pq;
pq.push({0, 0});
vi dis(n, INF);
dis[0] = 0;
while (!pq.empty()){
auto [d, u] = pq.top();
pq.pop();
if (dis[u] != d) continue;
for (auto [v, cd] : adj[u]){
if (d + cd < dis[v]){
dis[v] = d + cd;
pq.push({dis[v], v});
}
}
}
int ret = dis[n-1];
fill(dis.begin(), dis.end(), INF);
dis[n-1] = 0;
pq.push({0, n-1});
while (!pq.empty()){
auto [d, u] = pq.top();
pq.pop();
if (dis[u] != d) continue;
for (auto [v, cd] : adj[u]){
if (d + cd < dis[v]){
dis[v] = d + cd;
pq.push({dis[v], v});
}
}
}
return ret + dis[0];
}
void REM(int u, int v, int c){ adj[u].erase(adj[u].find({v, c})); }
void ADD(int u, int v, int c){ adj[u].insert({v, c}); }
void solve(){
cin >> n >> m;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
dist[i][j] = INF;
dist[i][i] = 0;
adj[i].clear();
}
for (int i = 0; i < m; i++){
Edge &e = edges[i];
cin >> e.u >> e.v >> e.c >> e.d;
e.u--, e.v--;
ADD(e.u, e.v, e.c);
dist[e.u][e.v] = min(dist[e.u][e.v], e.c);
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
int ans = cost();
for (int i = 0; i < m; i++){
auto [u, v, c, d] = edges[i];
int p = min(dist[0][n-1], dist[0][v] + c + dist[u][n-1])
+ min(dist[n-1][0], dist[n-1][v] + c + dist[u][0])
+ d;
if (p < ans){
REM(u, v, c);
ADD(v, u, c);
ans = min(ans, cost() + d);
REM(v, u, c);
ADD(u, v, c);
}
}
cout << (ans >= INF ? -1 : ans) << endl;
}
int32_t main(){
#ifndef ONLINE_JUDGE
auto begin = chrono::high_resolution_clock::now();
freopen("sol.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;// cin >> T;
for (int i = 1; i <= T; i++){
#ifndef ONLINE_JUDGE
if (T != 1)
cout << "Test Case #" << i << endl;
#endif
solve();
}
#ifndef ONLINE_JUDGE
auto elapsed = chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now() - begin);
cout << "Time Taken : " << (elapsed.count() * 1e-9) << " seconds" << endl;
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
904 KB |
Output is correct |
4 |
Correct |
9 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
736 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
127 ms |
892 KB |
Output is correct |
11 |
Correct |
554 ms |
880 KB |
Output is correct |
12 |
Correct |
29 ms |
856 KB |
Output is correct |
13 |
Correct |
12 ms |
1112 KB |
Output is correct |
14 |
Correct |
9 ms |
908 KB |
Output is correct |
15 |
Correct |
11 ms |
904 KB |
Output is correct |
16 |
Correct |
18 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
6492 KB |
Output is correct |
2 |
Correct |
403 ms |
6428 KB |
Output is correct |
3 |
Correct |
163 ms |
6620 KB |
Output is correct |
4 |
Correct |
15 ms |
1000 KB |
Output is correct |
5 |
Correct |
11 ms |
892 KB |
Output is correct |
6 |
Correct |
7 ms |
600 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Execution timed out |
1033 ms |
6480 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
856 KB |
Output is correct |
2 |
Correct |
12 ms |
604 KB |
Output is correct |
3 |
Correct |
25 ms |
4956 KB |
Output is correct |
4 |
Correct |
7 ms |
604 KB |
Output is correct |
5 |
Correct |
31 ms |
6236 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
53 ms |
6236 KB |
Output is correct |
9 |
Correct |
73 ms |
6372 KB |
Output is correct |
10 |
Correct |
37 ms |
6324 KB |
Output is correct |
11 |
Correct |
46 ms |
6304 KB |
Output is correct |
12 |
Correct |
33 ms |
6248 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
34 ms |
6160 KB |
Output is correct |
20 |
Correct |
50 ms |
6388 KB |
Output is correct |
21 |
Correct |
33 ms |
6228 KB |
Output is correct |
22 |
Correct |
34 ms |
6188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
7 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
904 KB |
Output is correct |
4 |
Correct |
9 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
7 ms |
736 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
127 ms |
892 KB |
Output is correct |
11 |
Correct |
554 ms |
880 KB |
Output is correct |
12 |
Correct |
29 ms |
856 KB |
Output is correct |
13 |
Correct |
12 ms |
1112 KB |
Output is correct |
14 |
Correct |
9 ms |
908 KB |
Output is correct |
15 |
Correct |
11 ms |
904 KB |
Output is correct |
16 |
Correct |
18 ms |
1112 KB |
Output is correct |
17 |
Correct |
159 ms |
6492 KB |
Output is correct |
18 |
Correct |
403 ms |
6428 KB |
Output is correct |
19 |
Correct |
163 ms |
6620 KB |
Output is correct |
20 |
Correct |
15 ms |
1000 KB |
Output is correct |
21 |
Correct |
11 ms |
892 KB |
Output is correct |
22 |
Correct |
7 ms |
600 KB |
Output is correct |
23 |
Correct |
8 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
1033 ms |
6480 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |