CJB一直觉得自己很高,其实我们都懂的。他总爱跟别人比身高,可是这个悲伤的事实我就不说了吧。CJB所在班级有n个人,CJB的标号为0,其他人的标号为1到n-1。我们知道某些人之间的身高差距的范围。比如CJB和老雷的身高差距为[-3, 2],意思是:-3≤老雷的身高-CJB的身高≤2。 求CJB所在班级中比CJB高最多的人与CJB的身高差的最大值。如果没有人比CJB高,输出0,如果最高的人最高可以无限大或者输入数据有矛盾的,输出-1。
Input 第一行输入两个整数n和m。 然后输入有m行,每行有4个整数u, v, a, b,表示u和v之间的身高差距为[a, b]。 Output 输出CJB所在班级中比CJB高最多的人与CJB的身高差的最大值。如果没有人比CJB高,输出0,如果最高的人最高可以无限大或者输入数据有矛盾的,输出-1。
Sample Input
3 5
0 1 2 5
1 2 -2 0
2 0 -2 -1
2 0 -2 0
0 2 1 4
Sample Output
4
HINT
对于20%的数据,1≤n≤3,-10≤a,b≤10; 对于100%的数据,1≤n≤1000,1≤m≤10000,0≤u,v
#include<bits/stdc++.h>
using namespace std;
const int maxn=10100;
const int maxm=500500;
const int inf=0x3f3f3f3f;
int n,m,u,v,a,b,cnt;
struct Edge{
int to,next,w;
}edge[maxm];
int using_v[maxn],using_times[maxn];
int head[maxn],dis[maxn];
void addedge(int u,int v,int w){
edge[++cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt;
}
int spfa(int start){
queue <int> q;
dis[start]=0;
using_v[start]=1;
q.push(start);
while(!q.empty()){
int top=q.front();
q.pop();
using_v[top]=0;
using_times[top]++;
if(using_times[top]>n) return 0;
for(int i=head[top];i;i=edge[i].next){
if(dis[edge[i].to]>dis[top]+edge[i].w){
dis[edge[i].to]=dis[top]+edge[i].w;
if(!using_v[edge[i].to]){
using_v[edge[i].to]=1;
q.push(edge[i].to);
}
}
}
}
return 1;
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>u>>v>>a>>b;
addedge(v,u,-b);
addedge(u,v,a);
}
int maxx=-1e9;
if(!spfa(0)) cout<<-1;
else{
for(int i=0;i<n;i++){
maxx=max(maxx,dis[i]);
}
cout<<maxx-dis[0]*2;
}
return 0;
}