#include<bits/stdc++.h>
#define N 1003
using namespace std;
int i, j, n, m, k, x, y, o, p, s;
int z, l, ha[N], u[N], ans, f[N][N];
struct A{
int x, y, n, l, z;
} d[2001];
void cun(int x,int y,int z,int l){
d[++k].x=x, d[k].y=y, d[k].z=z,d[k].l=l;
d[k].n=ha[x], ha[x]=k;
}
void dfs(int k,int s,int l){
if(f[k][l]<=s)return;
f[k][l]=s;
if(k==n){
int op=1.0*l/s*1000000;
ans=max(ans,op);
return;
}
if(u[k])return;
u[k]=1;
for(int i=ha[k];i;i=d[i].n){
dfs(d[i].y,s+d[i].z,min(l,d[i].l));
}
u[k]=0;
return;
}
int main(){
scanf("%d%d",&n,&m);
for(i=1; i<=m; i++){
scanf("%d%d%d%d",&x,&y,&z,&l);
cun(x,y,z,l);
cun(y,x,z,l);
}
memset(f,1,sizeof f);
dfs(1,0,1002);
printf("%d",ans);
return 0;
}
//
#include<bits/stdc++.h>
#define N 1003
using namespace std;
int i, j, n, m, k, x, y, o, p, s;
int z, l, ha[N], u[N], ans, f[N][N];
struct A{
int x, y, n, l, z;
} d[200001];
void cun(int x,int y,int z,int l){
d[++k].x=x, d[k].y=y, d[k].z=z,d[k].l=l;
d[k].n=ha[x], ha[x]=k;
}
void dfs(int k,int s,int l){
if(f[k][l]<=s)return;
f[k][l]=s;
if(k==n){
int op=1.0*l/s*1000000;
ans=max(ans,op);
return;
}
if(u[k])return;
u[k]=1;
for(int i=ha[k];i;i=d[i].n){
dfs(d[i].y,s+d[i].z,min(l,d[i].l));
}
u[k]=0;
return;
}
int main(){
scanf("%d%d",&n,&m);
for(i=1; i<=m; i++){
scanf("%d%d%d%d",&x,&y,&z,&l);
cun(x,y,z,l);
cun(y,x,z,l);
}
memset(f,1,sizeof f);
dfs(1,0,1002);
printf("%d",ans);
return 0;
}
上面两份代码是开O(2)优化提交的(毕竟我不会别的方法了),第一个90分,第二个100分,就d数组开大了,为啥第八个点从TLE变成了AC???(第一份代码第八个点1.02秒,第二分代码第八个点0.960秒)