NOIP2017 Day1 T3 Park (Shortest path + topology sort +DP)

Source: Internet
Author: User

God TM Game when more than 0 of the 60 T T

First run out of 1 starting point and n the shortest point, because K is only 50, so can DP. Set F[i][j] Represents the length of the shortest path I, and the number of schemes to J.

We found that if the shortest road and 0 sides will have a posterior sex, how to do? Topology ordering.

The shortest way point and 0 sides of the point out to run topology sequencing, if there are 0 ring words must not be 0, and note that 0 ring must be in the shortest path of the <= +k output-1, this is just run out of the 1 starting point to n the shortest way to the beginning to judge the good.

Then the topological sequence DP out I the same, and then DP is not on the shortest path or 0 sides.

#include <iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<queue>#defineMOD (x) ((x) >=mod? ( x)-mod: (x))using namespacestd;Const intmaxn=1000010;structtjm{inttoo, dis, pre;} e[2][MAXN];structpoi{intx, dis;}; Priority_queue<poi>Q;BOOL operator< (poi A, poi b) {returnA.dis>B.dis;}intT, x, Y, Z, N, M, K, mod, top, tot[2], ans;intf[ -][MAXN], dist[2][MAXN], last[2][MAXN], D[MAXN], ST[MAXN];voidReadint&k) {    intf=1; k=0;CharC=GetChar ();  while(c<'0'|| C>'9') c=='-'&& (f=-1), c=GetChar ();  while(c<='9'&& c>='0') k=k*Ten+c-'0', c=GetChar (); K*=F;} InlinevoidAddintXintYintZintTy) {e[ty][++tot[ty]]= (TJM) {y, Z, Last[ty][x]}; last[ty][x]=tot[ty];} InlinevoidDij (intXintty) {memset (dist[ty], +,sizeof(Dist[ty])); DIST[TY][X]=0; Q.push (POI) {x,0});  while(!Q.empty ()) {POI now=q.top (); Q.pop (); if(Dist[ty][now.x]!=now.dis)Continue;  for(intI=last[ty][now.x], too;i;i=e[ty][i].pre)if(dist[ty][too=e[ty][i].too]>dist[ty][now.x]+E[ty][i].dis) {Dist[ty][too]=dist[ty][now.x]+E[ty][i].dis;        Q.push (POI) {too, Dist[ty][too]}); }}}inlineBOOLtopo () {memset (d,0,sizeof(d));  for(intI=1; i<=n;i++)     for(intj=last[0][i], too;j;j=e[0][j].pre)if(dist[0][i]+e[0][j].dis==dist[0][too=e[0][j].too]) d[too]++; Top=0; for(intI=1; i<=n;i++)if(!d[i]) st[++top]=i;  for(intI=1; i<=top;i++)     for(intj=last[0][st[i]], too;j;j=e[0][j].pre)if(dist[0][st[i]]+e[0][j].dis==dist[0][too=e[0][j].too]) {D[too]--; if(!d[too]) st[++top]=too; }     for(intI=1; i<=n;i++)if(D[i] && dist[0][i]+dist[i][n]<=dist[0][N]+K)return 0; return 1;}intMain () {read (T);  while(t--) {memset (last,0,sizeof(last)); tot[0]=tot[1]=0; Read (n); Read (m); Read (K);        Read (mod);  for(intI=1; i<=m;i++) Read (x), read (y), read (z), add (x, Y, Z,0), add (y, X, Z,1); Dij (1,0); DIJ (N,1); if(!topo ()) {Puts ("-1");Continue;} Memset (F,0,sizeof(f)); f[0][1]=1; ans=0;  for(intI=0; i<=k;i++)        {             for(intj=1; j<=top;j++)              for(intk=last[0][ST[J]], too;k;k=e[0][k].pre)if(e[0][k].dis+dist[0][st[j]]==dist[0][too=e[0][k].too]) F[i][too]+=F[I][ST[J]], f[i][too]=MOD (F[i][too]);  for(intj=1; j<=n;j++)             for(intk=last[0][J], too, tmp;k;k=e[0][k].pre)if((tmp=i+e[0][k].dis+dist[0][j]-dist[0][too=e[0][k].too]) <=k && i!=tmp) F[tmp][too]+=F[I][J], f[tmp][too]=MOD (F[tmp][too]); Ans+=f[i][n]; ans=MOD (ans); } printf ("%d\n", ans); }}
View Code

NOIP2017 Day1 T3 Park (Shortest path + topology sort +DP)

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.