#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+1), 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 < (int) 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+1), 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 < (int) 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, v, c, 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.v));
res += e.d;
}
else adj[e.u].push_back(ii(e.c,e.v));
}
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){
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);
}
prin(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
4464 KB |
Output is correct |
2 |
Correct |
31 ms |
4584 KB |
Output is correct |
3 |
Correct |
33 ms |
4464 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Incorrect |
34 ms |
4464 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
21 ms |
4212 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
27 ms |
4464 KB |
Output is correct |
6 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |