#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3SJjfN1 ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
signed main(){
_3SJjfN1;
int n,m;
cin>>n>>m;
using T=pair<pii,int>;
vec(vec(T)) adj(n);
vec(pii) es;
rep(i,m){
int u,v,c0,c1;
cin>>u>>v>>c0>>c1;
u-=1,v-=1;
adj[u].pb(T(pii(c0,c1),v));
adj[v].pb(T(pii(c0,c1),u));
es.pb(pii(c0,c1));
}
const int inf=1e18;
vi pns(n,inf);
auto dijkstra=[&](int p,int q)->void{
struct state{
int x,y,p,q,v;
state(int _x=0,int _y=0,int _p=0,int _q=0,int _v=0):
x(_x),y(_y),p(_p),q(_q),v(_v){};
bool operator<(const state&a)const{
return pii((x+p)*(y+q),v)>pii((a.x+p)*(a.y+q),a.v);
}
};
priority_queue<state> pq;
pq.push(state(0,0,p,q,0));
vec(pii) d(n,{inf,inf});
d[0]=pii(0,0);
while(sz(pq)){
auto top=pq.top();
int v=top.v,x=top.x,y=top.y;
pq.pop();
if(d[v]!=pii(x,y)) continue;
for(auto e:adj[v]){
int c0=e.fi.fi,c1=e.fi.se,u=e.se;
if(d[u].fi==inf or (d[u].fi+p)*(d[u].se+q)>(x+c0+p)*(y+c1+q)){
d[u]={x+c0,y+c1};
pq.push(state(x+c0,y+c1,p,q,u));
}
}
}
rep(v,n){
pns[v]=min(pns[v],d[v].fi*d[v].se);
}
};
for(auto p:es){
dijkstra(p.fi,p.se);
}
rng(v,1,n){
cout<<(pns[v]==inf?-1:pns[v])<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
236 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
436 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
283 ms |
548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
211 ms |
536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
484 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |