QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#681265 | #5613. Road To Savings | vic233333# | TL | 0ms | 3636kb | C++20 | 2.0kb | 2024-10-27 03:55:12 | 2024-10-27 03:55:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 110, M = N * N;
int n, m, s, t;
int hed[N], to[M], nxt[M], wt[M], edcnt;
inline void ADD(int x, int y, int z) {
nxt[++edcnt] = hed[x];
hed[x] = edcnt;
to[edcnt] = y;
wt[edcnt] = z;
}
int ans = 0;
int dis[N], vis[N];
int colored[M];
int curdis = 0;
int onpath[M], pathcnt = 0;
void dfs(int x) {
if (x == t) {
if (curdis == dis[t]) {
for (int i = 1; i <= pathcnt; ++i) {
if (!colored[onpath[i]]) {
colored[onpath[i]] = 1;
if (onpath[i] % 2 == 0) colored[onpath[i] - 1] = 1;
else colored[onpath[i] + 1] = 1;
ans -= 2 * wt[onpath[i]];
}
}
}
return;
}
vis[x] = 1;
for (int i = hed[x]; i; i = nxt[i]) {
int y = to[i];
if (vis[y]) continue;
onpath[++pathcnt] = i;
curdis += wt[i];
dfs(y);
curdis -= wt[i];
--pathcnt;
}
vis[x] = 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> s >> t;
for (int i = 1; i <= m; ++i) {
int x, y, z;
cin >> x >> y >> z;
ADD(x, y, z);
ADD(y, x, z);
}
priority_queue<pair<int, int>> q;
q.emplace(0, s);
fill(dis + 1, dis + n + 1, INT_MAX);
while (!q.empty()) {
auto [d, u] = q.top();
q.pop();
d = -d;
if (vis[u]) continue;
vis[u] = 1;
dis[u] = d;
for (int e = hed[u]; e; e = nxt[e]) {
int v = to[e];
if (dis[v] > dis[u] + wt[e]) {
q.emplace(-(dis[u] + wt[e]), v);
}
}
}
for (int i = 1; i <= edcnt; ++i) {
ans += wt[i];
}
fill(vis + 1, vis + n + 1, 0);
dfs(s);
cout << ans / 2 << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3540kb
input:
4 5 1 4 1 2 1 1 3 2 1 4 2 4 2 1 3 4 1
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
4 5 1 4 1 2 1 1 3 2 1 4 1 4 2 1 3 4 1
output:
5
result:
ok single line: '5'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
2 1 1 2 1 2 10
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
3 3 1 2 1 2 10 2 3 10 3 1 10
output:
20
result:
ok single line: '20'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
3 3 1 2 1 2 10 2 3 5 3 1 5
output:
0
result:
ok single line: '0'
Test #6:
score: -100
Time Limit Exceeded
input:
100 4950 74 24 1 2 8 1 3 6 1 4 5 1 5 6 1 6 2 1 7 6 1 8 1 1 9 10 1 10 1 1 11 10 1 12 9 1 13 4 1 14 4 1 15 1 1 16 4 1 17 7 1 18 8 1 19 1 1 20 7 1 21 2 1 22 9 1 23 9 1 24 7 1 25 6 1 26 5 1 27 4 1 28 3 1 29 4 1 30 6 1 31 8 1 32 10 1 33 10 1 34 3 1 35 6 1 36 10 1 37 5 1 38 4 1 39 1 1 40 10 1 41 9 1 42 7 ...