이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=2e5+5;
const ll INF=1e18;
int n,m;
int l[mxn], r[mxn];
ll c[mxn], d[mxn];
vector<int> adj[mxn];
bool blocked[mxn] = {};
bool used[mxn] = {};
void dijkstra(int s, vector<ll> &d, vector<int> & p) {
d.assign(n + 1, INF);
p.assign(n + 1, -1);
vector<bool> u(n, false);
d[s] = 0;
for(int i = 1; i <= n; i++) {
int v = -1;
for (int j = 1; j <= n; j++) {
if (!u[j] && (v == -1 || d[j] < d[v]))
v = j;
}
if(d[v] == INF)
break;
u[v] = true;
for (auto edge : adj[v]) {
if(blocked[edge]) continue;
int to = l[edge] ^ r[edge] ^ v;
ll len = c[edge];
if (d[v] + len < d[to]) {
d[to] = d[v] + len;
p[to] = edge;
}
}
}
}
int main() {
cin >> n >> m;
ll w[n + 1][n + 1];
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) {
if(i == j) w[i][j] = 0;
else w[i][j] = INF;
}
for(int i = 1; i <= m; i++) {
scanf("%d%d%lld%lld",&l[i],&r[i],&c[i],&d[i]);
adj[l[i]].push_back(i);
// adj[1][r[i]].push_back(i);
w[l[i]][r[i]] = min(w[l[i]][r[i]], c[i]);
}
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
w[i][j] = min(w[i][j], w[i][k] + w[k][j]);
}
}
}
ll ans = w[1][n] + w[n][1];
int source = 1;
int target = n;
if(w[1][n] >= INF) {
swap(source, target);
if(w[source][target] >= INF) {
for(int i = 1; i <= m; i++) {
ans = min(ans, d[i] + w[1][r[i]] + c[i] + w[l[i]][n] + w[n][r[i]] + c[i] + w[l[i]][1]);
}
if(ans >= INF) cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
}
vector<ll> dist1, dist2;
vector<int> last1, last2;
dijkstra(source, dist1, last1);
vector<int> sedg;
int nd = target;
while(nd != source) {
used[last1[nd]] = true;
sedg.push_back(last1[nd]);
nd = l[last1[nd]];
}
for(int i = 1; i <= m; i++) {
if(used[i]) continue;
ans = min(ans, w[source][target] + w[target][r[i]] + d[i] + c[i] + w[l[i]][source]);
}
for(int i : sedg) {
blocked[i] = true;
vector<int> cl;
l[m + 1] = r[i];
r[m + 1] = l[i];
c[m + 1] = c[i];
adj[l[m + 1]].push_back(m + 1);
dijkstra(source, dist1, cl);
dijkstra(target, dist2, cl);
ans = min(ans, dist1[target] + dist2[source]);
blocked[i] = false;
adj[l[m + 1]].pop_back();
}
cout<<ans<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:57:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | scanf("%d%d%lld%lld",&l[i],&r[i],&c[i],&d[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |