이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <utility>
using namespace std;
#define int long long
#define pi pair<int, int>
const int inf = 1e17;
const int N = 203;
vector<vector<int>> cost(N, vector<int>(N, inf));
vector<vector<int>> wt(N, vector<int>(N, inf));
vector<vector<int>> dist(N, vector<int>(N, inf));
multiset<pi> g[N];
int n, m;
int dijkstra(int x, int y)
{
vector<int> dis(n + 1, inf);
vector<bool> vis(n + 1, 0);
multiset<pi> ms;
ms.insert({0, x});
dis[x] = 0;
while (!ms.empty())
{
pi p = *ms.begin();
int v = p.second;
if (vis[v])
{
continue;
}
vis[v] = true;
for (auto ch : g[v])
{
int ch_v = ch.first;
int ch_wt = ch.second;
if (dis[v] + ch_wt < dis[ch_v])
{
dis[ch_v] = dis[v] + ch_wt;
ms.insert({dis[ch_v], ch_v});
}
}
}
return dis[y];
}
signed main()
{
cin >> n >> m;
vector<pi> edges(m);
for (int i = 0; i < m; i++)
{
int u, v, w, c;
cin >> u >> v >> w >> c;
if (g[u].size() < 2)
g[u].insert({v, w});
edges[i] = {u, v};
cost[u][v] = min(cost[u][v], c);
wt[u][v] = min(wt[u][v], w);
}
int ans = min(inf, dijkstra(1, n) + dijkstra(n, 1));
for (auto p : edges)
{
int i = p.first;
int j = p.second;
g[i].erase({j, wt[i][j]});
bool yes = g[j].count({i, wt[i][j]});
if (yes)
{
g[j].insert({i, wt[i][j]});
}
ans = min(ans, dijkstra(1, n) + dijkstra(n, 1) + cost[i][j]);
g[i].insert({j, wt[i][j]});
if (yes)
{
g[j].erase({i, wt[i][j]});
}
}
if (ans == inf)
{
cout << -1;
return 0;
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |