/*Test instructions: give you a direction-free graph. Give a limit t. Ask from 1 to n points, in the case of no more than T, can visit a few points. Ensure that there is at least one road limit of not more than T. Ideas: 1. We can know by no-one (depending on the graph is a dag), this problem must be DP. 2.DP[I][J] represents, which reached the point I, and visited the shortest time of the J-City. WA point: No initialization of 0 in the array .*/#include<bits/stdc++.h>#defineN 5050using namespacestd;intinf=0x3f3f3f3f;intNum[n];BOOLVis[n];intDp[n][n];structedge{intID; intW; Edge*next;}; Edge Edges[n];edge*Adj[n];intEdnum;inlinevoidAddedge (intAintBintW) {Edge*tmp=&edges[ednum++]; TMP->id=b; TMP->w=W; TMP->next=Adj[a]; Adj[a]=tmp;}intFromx[n][n];intMain () {intn,m,t,a,b,w; scanf ("%d%d%d",&n,&m,&t); for(intI=0; i<=n;i++){ for(intj=1; j<=n;j++) {Dp[i][j]=inf; } } for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&W); Addedge (A,B,W); NUM[B]++; } dp[1][1]=0; for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ if(vis[j]| | NUM[J])Continue; VIS[J]=1; for(Edge *it=adj[j];it;it=it->next) {Num[it->id]--; for(intk=2; k<=n;k++){ if(dp[it->id][k]>dp[j][k-1]+it->W) {Dp[it->id][k]=dp[j][k-1]+it->W; Fromx[it->id][k]=J; } } } Break; } } for(inti=n;i>=2; i--){ if(dp[n][i]<=t) {printf ("%d\n", i); Stack<int>SS; intx=n,y=i; Ss.push (n); while(fromx[x][y]!=1) {Ss.push (fromx[x][y]); X=Fromx[x][y]; Y--; } Ss.push (1); while(!Ss.empty ()) { intpp=Ss.top (); Ss.pop (); printf ("%d", pp); } Break; } }}
Codeforces 721C [dp][topology sort]