#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 2e9;
const int N = 2e2 + 15;
int n, m, a[N];
int d[3][N][N];
int cost[N];
int curd[N], p[N];
vector <int> g[N];
vector <int> path1, path2;
priority_queue <pair <int, int>> q;
bool is[2][N];
struct edge {
int u, v, c, d;
};
vector <edge> e;
void recover(int v, vector <int> &path) {
path.push_back(p[v]);
v = e[p[v]].u;
int cnt = 0;
while(v != 1 && v != n) {
path.push_back(p[v]);
v = e[p[v]].u;
if(++cnt > 1000) {
cout << "asdasdasd" << endl;
exit(0);
}
}
reverse(path.begin(), path.end());
}
void dxtra(int s) {
fill(curd, curd + N, inf);
q.push(make_pair(0, s));
curd[s] = p[s] = 0;
while(!q.empty()) {
int d = -q.top().first, v = q.top().second;
q.pop();
if(d > curd[v])
continue;
for(int ind : g[v]) {
int to = e[ind].v, w = e[ind].c;
if(curd[to] > curd[v] + w) {
p[to] = ind;
curd[to] = curd[v] + w;
q.push(make_pair(-curd[to], to));
}
}
}
}
void calc_distance() {
for(int i = 0; i < 2; ++i)
for(int j = 0; j < N; ++j)
for(int k = 0; k < N; ++k) {
if(j != k)
d[i][j][k] = inf;
}
for(int i = 0; i < m; ++i)
d[0][e[i].u][e[i].v] = min(d[0][e[i].u][e[i].v], e[i].c);
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
d[0][i][j] = min(d[0][i][j], d[0][i][k] + d[0][k][j]);
for(int t = 0; t < 2; ++t) {
for(int i = 0; i < m; ++i)
if(!is[t][i])
d[t+1][e[i].u][e[i].v] = min(d[t+1][e[i].u][e[i].v], e[i].c);
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
d[t+1][i][j] = min(d[t+1][i][j], d[t+1][i][k] + d[t+1][k][j]);
}
}
main() {
cin >> n >> m;
for(int i = 0; i < m; ++i) {
int u, v, cc, dd;
cin >> u >> v >> cc >> dd;
g[u].push_back(i);
e.push_back({u, v, cc, dd});
}
dxtra(1);
if(curd[n] != inf)
recover(n, path1);
dxtra(n);
if(curd[1] != inf)
recover(1, path2);
for(int ind : path1)
is[0][ind] = true;
for(int ind : path2)
is[1][ind] = true;
calc_distance();
for(int i = 0; i < m; ++i) {
int u = e[i].u, v = e[i].v, c = e[i].c;
if(!is[0][i]) {
cost[i] += min(d[0][1][n], d[0][1][v] + d[0][u][n]);
}
else {
int j = 0;
for(j = 0; j < path1.size(); ++j)
if(i == path1[j])
break;
int now = inf;
for(int i = 0; i <= j; ++i) {
for(int k = j; k < path1.size(); ++k) {
int u = e[i].u, v = e[k].v;
now = min(now, d[0][1][u] + d[1][u][v] + d[0][v][n]);
}
}
cost[i] += now;
}
}
for(int i = 0; i < m; ++i) {
int u = e[i].u, v = e[i].v, c = e[i].c;
if(!is[1][i]) {
cost[i] += min(d[0][n][1], d[0][n][v] + d[0][u][1]);
}
else {
int j = 0;
for(j = 0; j < path2.size(); ++j)
if(i == path2[j])
break;
int now = inf;
for(int i = 0; i <= j; ++i)
for(int k = j; k < path2.size(); ++k) {
int u = e[i].u, v = e[k].v;
now = min(now, d[0][n][u] + d[2][u][v] + d[0][v][1]);
}
cost[i] += now;
}
}
int ans = d[0][1][n] + d[0][n][1];
for(int i = 0; i < m; ++i)
ans = min(ans, cost[i] + e[i].c + e[i].d);
if(ans >= inf)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
Compilation message
ho_t4.cpp:86:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:117:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < path1.size(); ++j)
~~^~~~~~~~~~~~~~
ho_t4.cpp:122:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = j; k < path1.size(); ++k) {
~~^~~~~~~~~~~~~~
ho_t4.cpp:111:31: warning: unused variable 'c' [-Wunused-variable]
int u = e[i].u, v = e[i].v, c = e[i].c;
^
ho_t4.cpp:139:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j = 0; j < path2.size(); ++j)
~~^~~~~~~~~~~~~~
ho_t4.cpp:144:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = j; k < path2.size(); ++k) {
~~^~~~~~~~~~~~~~
ho_t4.cpp:133:31: warning: unused variable 'c' [-Wunused-variable]
int u = e[i].u, v = e[i].v, c = e[i].c;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
2680 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
110 ms |
3684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
40 ms |
2680 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
2680 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |