#include <bits/stdc++.h>
using namespace std;
#define int long long
int INF = 0x3f3f3f3f3f3f3f3f;
struct Edge {
int to, c, d;
Edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), d(z) {}
};
const int MAXN = 205;
int gnode[MAXN][MAXN];
int gc[MAXN][MAXN];
int comp[MAXN];
bool vis[MAXN];
int currC = 0;
vector<Edge> adj1[MAXN], adj[MAXN];
int n, m;
void dfs1(int node, int p) {
vis[node] = 1;
gnode[p][node] = 1;
for (auto u : adj1[node]) {
if (!vis[u.to]) dfs1(u.to, p);
}
}
void calcComp(int node) {
if (comp[node]) return;
comp[node] = currC;
for (int i = 1; i <= n; i++) {
if (gnode[node][i] && gnode[i][node]) calcComp(i);
}
}
int32_t main() {
//freopen("in", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
adj1[a].push_back(Edge(b, c, d));
}
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof vis);
dfs1(i, i);
}
for (int i = 1; i <= n; i++) {
if (comp[i]) continue;
currC++;
calcComp(i);
}
if (comp[1] == comp[n]) {
cout << "0\n";
return 0;
}
if (!(gnode[1][n] || gnode[n][1])) {
cout << "-1\n";
return 0;
}
for (int i = 1; i <= n; i++) {
for (auto u : adj1[i]) {
adj[comp[i]].push_back(Edge(comp[u.to], u.c, u.d));
}
}
int ans = INF;
int qtd = 0, mi = INF;
for (auto u : adj[comp[1]]) {
if (u.to == comp[n]) {
qtd++;
mi = min(mi, u.d);
}
}
if (qtd >= 2) ans = min(ans, mi);
// duas saindo do N
qtd = 0, mi = INF;
for (auto u : adj[comp[n]]) {
if (u.to == comp[1]) {
qtd++;
mi = min(mi, u.d);
}
}
if (qtd >= 2) ans = min(ans, mi);
// componente intermediaria
for (int i = 1; i <= currC; i++) {
for (int j = 1; j <= currC; j++) {
gc[i][j] = INF;
}
for (auto u : adj[i]) {
gc[i][u.to] = min(gc[i][u.to], u.d);
}
}
int s = comp[1], t = comp[n];
if (!gnode[1][n]) swap(s, t);
for (int i = 1; i <= currC; i++) {
if ((i == s) || (i == t)) continue;
if (gc[t][i] != INF) {
ans = min(ans, gc[s][i]);
}
if (gc[i][s] != INF) {
ans = min(ans, gc[i][t]);
}
}
// cout resposta
if (ans == INF) cout << "-1\n";
else cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
712 KB |
Output is correct |
3 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
2760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
30 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
37 ms |
3372 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
448 KB |
Output is correct |
8 |
Incorrect |
35 ms |
3544 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
712 KB |
Output is correct |
3 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |