Codeforces 721C [dp][topology sort]

Source: Internet
Author: User

/*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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.