This problem means there are some points on the plane, these points from west to East distribution, now choose a route from the west to the east in turn, asked to choose a route to go as many cities as possible, we can regard this issue as two people from the west to the east, the definition of f[i][j] 1 toward I and 2 to J of the largest city number , f[j][i] = f[i][j] = max (f[i][k]) + 1, 1<=k<j, the result is Max (F[i][n]), the code is as follows:
/*id:m1500293 lang:c++ Prog:tour*/#include<cstdio>#include<algorithm>#include<cstring>#include<map>#include<string>#include<iostream>using namespacestd;intN, V;//number of vertices and number of routesintd[ the][ the];intf[ the][ the];intMain () {Freopen ("tour.in","R", stdin); Freopen ("Tour.out","W", stdout); CIN>>N>>V; Map<string,int>Rep; intnum =1; for(intI=0; i<n; i++) { stringstr; CIN>>str; if(Rep.find (str) = =rep.end ()) Rep[str]= num++; } for(intI=0; i<v; i++) { stringu, v; CIN>>u>>v; D[REP[U]][REP[V]]=1; D[rep[v]][rep[u]]=1; } f[1][1] =1; for(intI=1; i<=n; i++) for(intj=i+1; j<=n; J + +) {F[i][j]= -0x3fffffff; for(intk=1; k<j; k++)//F[i][k]-> F[i][j] if(D[k][j] && f[i][k]>0&& f[i][k]>F[i][j]) f[i][j]=F[i][k]; F[j][i]= ++F[i][j]; } intAns =1; for(intI=1; i<n; i++)//F[i][n] D[i][n] if(D[i][n] && f[i][n]>ans) ans=F[i][n]; printf ("%d\n", ans); return 0;}
Usaco Canada Tour