Description
CS has n cells, and there are two unidirectional roads (a to b,b to a) connected between any cell. Because a lot of heavy rains recently, many roads have been flooded, different roads muddy degree different. Small A through the scientific analysis of the recent weather and topography, mapped out the time each road can pass smoothly and the length of the road.
Now small A in the cell 1, he hoped to be able to arrive at the destination cell very smoothly n, please help Xiao Ming to find a way from the cell 1 to reach the Community n all routes (total distance/total time) the largest route. Please tell him the value.
Input
The first line contains an integer n, which is the number of cells.
The next n*n of the matrix P, wherein the number of line I j indicates the length of the road from cell I to cell J is pi,j. The number of the first line I of the element is 0, the rest is guaranteed to be a positive integer.
Next to the N*n matrix T, the number of J of line I indicates the time ti,j required from cell I to cell J. The number of the first line I of the element is 0, the rest is guaranteed to be a positive integer.
Outputwrite a real number s, for cell 1 to reach the maximum answer of N, s accurate to 3 digits after the decimal point. Sample Input3
0 8 7
9 0 10
5 7 0
0 7 6
6 0 6
6 2 0
Sample Output2.125HINT30% of data, n<=20
100% of data, n<=100,p,t<=10000
Exercises
According to test instructions, (S1+S2+S3+...+SN)/(T1+T2+T3+...+TN) = ans
Ans (t1+t2+t3+...+tn) = (S1+S2+S3+...+SN)
S1-ANS*T1 + s2-ans*t2 + ... + s3-ans*t3 = 0;
Such ans is a solution.
For ans already obtained, if (S1+S2+S3+...+SN)/(T1+t2+t3+...+tn) > ans, i.e. s1-ans*t1 + s2-ans*t2 + ... + s3-ans*t3 > 0, then there is a better solution
Two-point answer, according to the edge of the right to build Si-ans*ti, the longest road, if greater than 0, there is a better solution.
Pay attention to the accuracy, pay attention to the positive ring (existence of positive loop must make the current answer set)
The array to open is large enough.
#include <iostream>#include<cstdio>#include<cstring>#defineN 110using namespacestd;intP[n][n],t[n][n];DoubleF[n][n];Doubledis[n*N];BOOLvis[n*N];intq[n*N];inttim[n*N];intN,head,tail;intSPFA (ints) {memset (DIS,-0x3f,sizeof(DIS)); memset (Vis,false,sizeof(VIS)); Memset (Tim,0,sizeof(Tim)); memset (q,0,sizeof(q)); Head=0, tail =0; Dis[s]=0; Vis[s] =true; Q[++tail] = s; tim[s]++; while(Head! =tail) { intnow = q[++Head]; for(intI=1; i<=n;i++) if(p[now][i]!=0&& dis[i]<dis[now]+F[now][i]) {Dis[i]= Dis[now] +F[now][i]; if(!Vis[i]) {Tim[i]++; q[++tail] =i; Vis[i]=true; if(tim[i]>n)return true; }} Vis[now]=false; } if(dis[n]>0)return true; return false;}intCheckDoublex) {memset (F,0,sizeof(f)); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) F[i][j]= P[i][j]-x*T[i][j]; if(SPFA (1)) return 1; return 0;}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) scanf ("%d",&P[i][j]); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) scanf ("%d",&T[i][j]); DoubleEPS =0.0001; DoubleLL =0, rr =10000; while(Rr-ll >EPS) { DoubleMid = (LL+RR)/2.0; if(check (mid)) ll=mid; ElseRR=mid; } printf ("%.3LF", ll);}
"CodeVS1183" Muddy Road