2323
2026-03-19 10:40:52
发布于:河北
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define eps 1e-3
#define inf 1e9
#define N 5005
int n,m;
inline int read() {
int tmp=0,w=1;
char ch=0;
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) tmp=(tmp<<1)+(tmp<<3)+ch-'0',ch=getchar();
return tmp*w;
}
struct node {
int v,f,nex;
}e[N<<1];
int tot,h[N];
void add(int u,int v,int f) {
e[++tot].v=v,e[tot].f=f,e[tot].nex=h[u],h[u]=tot;
}
bool vis[N];
double dis[N];
bool spfa(int x,double mid) {
vis[x]=1;
int xx;
for(int i=h[x];i;i=e[i].nex) {
xx=e[i].v;
if(dis[xx]>dis[x]+e[i].f+mid) {
dis[xx]=dis[x]+e[i].f+mid;
if(vis[xx]) return 1;
if(spfa(xx,mid)) return 1;
}
}
vis[x]=0;
return 0;
}
bool pd(double mid) {
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
for(int i=1;i<=n;++i) if(spfa(i,mid)) return 1;
return 0;
}
void Bsearch() {
double l=0,r=inf,mid;
while(r-l>=eps) {
mid=(l+r)/2;
if(pd(mid)) l=mid;
else r=mid;
}
printf("%.2lf",l);
}
int main()
{
n=read()+2,m=read();
int u,v,a,b,c,d;
for(int i=1;i<=m;++i) {
u=read(),v=read(),a=read(),b=read(),c=read(),d=read();
if(c!=0) add(v,u,a-d);
add(u,v,b+d);
}
Bsearch();
return 0;
}
这里空空如也







有帮助,赞一个