QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#708077 | #4382. Path | hejinming983282# | AC ✓ | 345ms | 34204kb | C++23 | 2.7kb | 2024-11-03 19:22:08 | 2024-11-03 19:22:12 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define ls u << 1
#define rs u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const LL INF = 1e18;
const double eps = 1e-6;
const double PI = acos(-1);
inline int lowbit(int x) {return x & (-x);}
struct node {
int nex;
int val, ops;
};
struct edge {
LL dis;
int idx, ops;
bool operator < (const edge &B) const {
return dis > B.dis;
}
};
inline void solve() {
int n, m; cin >> n >> m;
int S, K; cin >> S >> K;
vector<node> g[n + 1];
while (m -- ) {
int u, v, w, ops;
cin >> u >> v >> w >> ops;
g[u].push_back({v, w, ops});
}
set<int> s;
for (int i = 1; i <= n; i ++ ) if (i != S) s.insert(i);
priority_queue<edge, vector<edge>> q;
vector<vector<LL>> dis(n + 1, vector<LL>(2, INF));
vector<vector<bool>> vis(n + 1, vector<bool>(2));
vector<bool> st(n + 1);
dis[S][0] = 0;
q.push({dis[S][0], S, 0});
LL last = 0;
while (!q.empty()) {
auto t = q.top(); q.pop();
int u = t.idx, ops = t.ops;
if (ops == 1) {
for (auto ite : g[u]) st[ite.nex] = true;
vector<int> tmp;
for (auto ite : s) {
if (st[ite]) continue;
dis[ite][0] = dis[u][1];
q.push({dis[ite][0], ite, 0});
tmp.push_back(ite);
}
for (auto ite : tmp) s.erase(ite);
for (auto ite : g[u]) st[ite.nex] = false;
} else s.erase(u);
if (vis[u][ops]) continue;
vis[u][ops] = true;
assert(dis[u][ops] >= last);
last = dis[u][ops];
for (auto ite : g[u]) {
int v = ite.nex, w = ite.val - (ops == 1 ? K : 0);
if (dis[v][ite.ops] > dis[u][ops] + w) {
dis[v][ite.ops] = dis[u][ops] + w;
q.push({dis[v][ite.ops], v, ite.ops});
}
}
}
for (int i = 1; i <= n; i ++ ) {
LL res = min(dis[i][0], dis[i][1]);
if (res == INF) cout << -1 << ' ';
else cout << res << ' ';
}
cout << endl;
}
signed main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
auto now = clock();
#endif
ios::sync_with_stdio(false), cin.tie(nullptr);
cout << fixed << setprecision(2);
int T; cin >> T;
while (T -- )
solve();
#ifdef DEBUG
cout << "============================" << endl;
cout << "Program run for " << (clock() - now) / (double)CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 345ms
memory: 34204kb
input:
13 10 30 2 18468 5 1 37637 0 9 9 45430 0 6 6 41749 0 2 2 21463 1 8 7 50859 0 3 4 18760 0 2 7 38186 0 8 7 33239 0 10 3 44135 0 6 5 47171 0 3 4 36141 0 2 2 46721 0 8 5 51130 0 8 10 27191 0 10 9 30784 0 1 3 18756 0 1 3 37732 0 7 6 34358 0 1 1 33474 0 4 9 38097 0 5 5 37224 0 7 7 32399 0 5 10 43094 0 8 9...
output:
21463 0 21463 21463 21463 45392 38186 21463 21463 21463 41923 0 45987 49920 18690 52316 71251 52316 25059 52316 57062 34860 18647 36256 49111 29152 32554 62744 0 38939 56122 64474 0 -1 84001 29542 39504 -1 -1 38273 46451 44825 44825 44825 57660 38488 44825 44825 44825 0 51281 91636 44602 39997 ...
result:
ok 13 lines