1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5,INF=1e18;
int n,m;
int ans[N];
struct edge
{
int a,b,r,p;
bool operator<(const edge &T)const{
return r>T.r;
}
}E[N];
int deg[N];
vector<int> G[N];
bool vis[N];
signed main()
{
scanf("%lld%lld",&n,&m);
fill(ans+1,ans+n+1,INF);
for(int i=1;i<=m;i++){
scanf("%lld%lld%lld%lld",&E[i].a,&E[i].b,&E[i].r,&E[i].p);
++deg[E[i].a];
}
sort(E+1,E+m+1);
for(int i=1;i<=m;i++)
G[E[i].b].push_back(i);
queue<int> q;
for(int i=1;i<=n;i++)
if(!deg[i])
q.push(i),deg[i]=-1;
for(int i=1;i<=m;i++){
while(!q.empty()){
int u=q.front();q.pop();
for(int j:G[u]){
if(vis[j])
continue;
vis[j]=1;
deg[E[j].a]--;
if(!deg[E[j].a])
q.push(E[j].a),deg[E[j].a]=-1;
if(ans[u]!=INF)
ans[E[j].a]=min(ans[E[j].a],max(E[j].r,ans[u]-E[j].p));
}
}
if(!vis[i]){
vis[i]=1;
deg[E[i].a]--;
if(!deg[E[i].a])
q.push(E[i].a),deg[E[i].a]=-1;
ans[E[i].a]=min(ans[E[i].a],E[i].r);
}
}
for(int i=1;i<=n;i++){
if(ans[i]==INF)
ans[i]=-1;
printf("%lld ",ans[i]);
}
return 0;
}
|