이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <climits>
using namespace std;
int n, m, s, t, u, v;
vector<vector<int>> parents;
vector<vector<pair<int, int>>> adj;
vector<bool> visited;
vector<vector<int>>paths;
vector<int>path;
void dfs(int node){
path.push_back(node);
if(node == s){
paths.push_back(path);
}
for(auto gp : parents[node])
dfs(gp);
path.pop_back();
}
void dijkstras_ST(){
priority_queue<pair<long long, int>> q;
vector<long long>dist(n+1,LONG_LONG_MAX);
q.push({0,s});
dist[s] = 0;
while(!q.empty()){
int node = q.top().second;
long long dist_node = -q.top().first;
q.pop();
if(visited[node])
continue;
visited[node] = true;
for(auto edge : adj[node]){
int next = edge.first;
int weight = edge.second;
if(dist[node] + weight < dist[next]){
dist[next] = dist[node] + weight;
parents[next].clear();
parents[next].push_back(node);
q.push({-dist[next],next});
}else if(dist[node] + weight == dist[next]){
parents[next].push_back(node);
}
}
}
}
void dijkstras_UV(int start, vector<long long> &dist){
priority_queue<pair<long long, int>> q;
q.push({0,start});
dist[start] = 0;
while(!q.empty()){
int node = q.top().second;
long long dist_node = -q.top().first;
q.pop();
if(visited[node])
continue;
visited[node] = true;
for(auto edge : adj[node]){
int next = edge.first;
int weight = edge.second;
if(dist[node] + weight < dist[next]){
dist[next] = dist[node] + weight;
q.push({-dist[next],next});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("in.txt","r",stdin);
cin >> n >> m >> s >> t >> u >> v;
adj = vector<vector<pair<int, int>>>(n+1);
parents = vector<vector<int>>(n+1);
visited = vector<bool>(n+1);
for(int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
dijkstras_ST();
fill(visited.begin(), visited.end(),false);
dfs(t);
fill(visited.begin(), visited.end(), false);
vector<long long> distU(n+1, LONG_LONG_MAX);
dijkstras_UV(u, distU);
fill(visited.begin(), visited.end(), false);
vector<long long> distV(n+1, LONG_LONG_MAX);
dijkstras_UV(v, distV);
long long ans = LONG_LONG_MAX;
for(int i = 0; i < paths.size(); i++){
vector<int> sp = paths[i];
vector<bool> on_sp(n+1);
for(int j = 0; j < sp.size(); j++)
on_sp[sp[j]] = true;
long long minu = LONG_LONG_MAX, minv = LONG_LONG_MAX;
for(int k = 1; k <= n; k++)
if(on_sp[k])
minu = min(minu, distU[k]);
for(int k = 1; k <= n; k++)
if(on_sp[k])
minv = min(minv, distV[k]);
ans = min(ans, minu+minv);
}
ans = min(ans,distU[v]);
cout << ans << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
commuter_pass.cpp: In function 'void dijkstras_ST()':
commuter_pass.cpp:29:19: warning: unused variable 'dist_node' [-Wunused-variable]
29 | long long dist_node = -q.top().first;
| ^~~~~~~~~
commuter_pass.cpp: In function 'void dijkstras_UV(int, std::vector<long long int>&)':
commuter_pass.cpp:54:19: warning: unused variable 'dist_node' [-Wunused-variable]
54 | long long dist_node = -q.top().first;
| ^~~~~~~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:95:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i = 0; i < paths.size(); i++){
| ~~^~~~~~~~~~~~~~
commuter_pass.cpp:98:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int j = 0; j < sp.size(); j++)
| ~~^~~~~~~~~~~
# | 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... |