#include <bits/stdc++.h>
#define int __int128
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
inline void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)write(x/10);
putchar(x%10+'0');
}
struct Edge{
int to;
int lenx,leny;
Edge(int x,int y,int z){to=x;lenx=y;leny=z;}
};
int n,m;
vector<Edge>edges[2000005];
int a,b,x,y;
pair<int,int>ans[2000005];
set<pair<int,int>>st;
bool used[2000005];
inline void dfs(int root){
for(auto x:edges[root]){
if(!used[x.to]){
ans[x.to].first=ans[root].first+x.lenx;
ans[x.to].second=ans[root].second+x.leny;
used[x.to]=true;
dfs(x.to);
}
}
}
signed main(){
n=read();m=read();
for(int i=1;i<=n;i++){
ans[i].first=1e18;
ans[i].second=1e18;
}
ans[1].first=0;ans[1].second=0;
for(int i=1;i<=m;i++){
a=read();b=read();x=read();y=read();
int len=(int)st.size();
st.insert({a,b});
if((int)st.size()==len)continue;
if(a>b){x=-x;y=-y;}
edges[a].push_back(Edge(b,x,y));
edges[b].push_back(Edge(a,x,y));
}
used[1]=true;
dfs(1);
for(int i=1;i<=n;i++){
if(ans[i].first==1e18&&ans[i].second==1e18)puts("undecidable");
else{
write(ans[i].first);putchar(' ');write(ans[i].second);
putchar('\n');
}
}
return 0;
}
萌新,刚学会存图和遍历,突发奇想想了个新算法,有错轻喷。