QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#528227 | #4382. Path | xuzhihaodedie | AC ✓ | 360ms | 46464kb | C++20 | 1.6kb | 2024-08-23 11:47:51 | 2024-08-23 11:47:53 |
Judging History
answer
//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define lson 2*p
#define rson 2*p+1
#define x first
#define y second
#define lowbit(x) x&-x
//#define endl "\n"
const int N=1e6+10;
const int mod=998244353;
struct node {
int x,w,op;
};
vector<node> g[N];
struct node1 {
int x,d,st;
bool operator < (const node1 &t) const {
return d>t.d;
}
};
int dist[N][2],vis[N][2];
void solve() {
int n,m,s,k;
cin>>n>>m>>s>>k;
for(int i=1;i<=n;i++) g[i].clear();
for(int i=1;i<=m;i++) {
int u,v,w,op;
cin>>u>>v>>w>>op;
g[u].push_back({v,w,op});
}
priority_queue<node1> q;
for(int i=0;i<=n;i++) {
dist[i][0]=dist[i][1]=1e18;
vis[i][0]=vis[i][1]=0;
}
dist[s][0]=0;
q.push({s,0,0});
set<int> S;
for(int i=1;i<=n;i++) if(i!=s) S.insert(i);
while(q.size()) {
auto p=q.top();
q.pop();
int x=p.x;
int st=p.st;
if(!st) S.erase(x);
else {
set<int> S1=S;
for(auto it:g[x]) {
int y=it.x;
S1.erase(y);
}
for(int y:S1) {
dist[y][0]=dist[x][1];
q.push({y,dist[y][0],0});
S.erase(y);
}
}
if(vis[x][st]) continue;
vis[x][st]=1;
for(auto it:g[x]) {
int y=it.x,op=it.op,w=it.w;
if(dist[y][op]>dist[x][st]+w-k*(st==1)) {
dist[y][op]=dist[x][st]+w-k*(st==1);
q.push({y,dist[y][op],op});
}
}
}
for(int i=1;i<=n;i++) {
if(min(dist[i][0],dist[i][1])==1e18) cout<<-1<<" ";
else cout<<min(dist[i][0],dist[i][1])<<" ";
}
cout<<endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
cin>>T;
while(T--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 360ms
memory: 46464kb
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