QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#98919 | #4382. Path | 3360550356 | AC ✓ | 440ms | 42380kb | C++14 | 2.9kb | 2023-04-20 22:06:07 | 2023-04-20 22:06:34 |
Judging History
answer
/****************
*@description:for the Escape Project
*@author: Nebula_xuan
* @Date: 2022-07-21 20:49:34
*************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
using namespace std;
#define rep(h, t) for (int i = h; i <= t; i++)
#define dep(t, h) for (int i = t; i >= h; i--)
typedef pair<int, int> PII;
typedef long long ll;
const int N = 5e5 + 10;
const int M = N * 2;
int n, m, s, k;
ll dist[N][2];
bool st[N][2];
int vis[N];
int h[N], ne[M], e[M], w[M], p[M], idx;
struct node
{
ll a, b, p;
bool operator<(const node &w) const
{
return b > w.b;
}
};
void add(int a, int b, int c, int d)
{
e[idx] = b, ne[idx] = h[a], w[idx] = c, p[idx] = d, h[a] = idx++;
}
void dijkstra()
{
set<int> S;
rep(1, n)
{
if (i != s)
S.insert(i);
st[i][0] = st[i][1] = false;
dist[i][0] = dist[i][1] = 1e18;
}
dist[s][0] = 0;
priority_queue<node> q;
q.push({s, 0, 0});
int cnt = 0;
while (q.size())
{
auto t = q.top();
q.pop();
int ver = t.a, tp = t.p;
cnt++;
if (!tp)
S.erase(ver);
else
{
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
vis[j] = cnt;
}
vector<int> v;
for (auto it : S)
{
if (vis[it] != cnt)
{
dist[it][0] = dist[ver][1];
q.push({it, dist[it][0], 0});
v.push_back(it);
}
}
for (auto it : v)
S.erase(it);
}
int y = 0;
if (tp)
y -= k;
if (st[ver][tp])
continue;
else
st[ver][tp] = 1;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j][p[i]] > dist[ver][tp] + w[i] + y)
{
dist[j][p[i]] = dist[ver][tp] + w[i] + y;
q.push({j, dist[j][p[i]], p[i]});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
idx = 0;
cin >> n >> m >> s >> k;
rep(0, n)
{
h[i] = -1, vis[i] = 0;
}
rep(1, m)
{
int x, y, z, zz;
cin >> x >> y >> z >> zz;
add(x, y, z, zz);
}
dijkstra();
rep(1, n)
{
if (min(dist[i][0], dist[i][1]) != 1e18)
cout << min(dist[i][0], dist[i][1]) << " ";
else
cout << "-1 ";
}
cout << endl;
// puts("");
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 440ms
memory: 42380kb
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