https://www.luogu.org/problemnew/show/P3953

https://www.zybuluo.com/wsndy-xx/note/1134388

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cctype>
  4.  
  5. using namespace std;
  6. const int N=1e5+,K=;
  7.  
  8. int n,m,k,p,tot,ans=;
  9. int first[N],next[N<<],en[N<<],w[N<<];
  10. int first1[N*K],next1[N*K<<],en1[N*K<<],d[N*K];
  11. int q[N<<],dis[N],dis1[N],u[N<<],v[N<<],t[N<<],f[N*K];
  12. bool bz[N];
  13.  
  14. inline int read() {
  15. int X=,w=;
  16. char ch=;
  17. while(!isdigit(ch)) {
  18. if(ch=='-') w=-;
  19. ch=getchar();
  20. }
  21. while(isdigit(ch)) X=(X<<)+(X<<)+ch-'',ch=getchar();
  22. return X*w;
  23. }
  24.  
  25. inline void insert(int x,int y,int z) {
  26. next[++tot]=first[x];
  27. first[x]=tot;
  28. en[tot]=y;
  29. w[tot]=z;
  30. }
  31.  
  32. inline void insert1(int x,int y) {
  33. next1[++tot]=first1[x];
  34. first1[x]=tot;
  35. en1[tot]=y;
  36. d[y]++;
  37. }
  38.  
  39. inline int get(int x,int y) {
  40. return (x-)*(k+)+y+;
  41. }
  42.  
  43. int main() {
  44. int T=read();
  45. while(T--) {
  46. n=read(),m=read(),k=read(),p=read();
  47. memset(first,tot=,sizeof(first));
  48. for(int i=; i<=m; i++) {
  49. u[i]=read(),v[i]=read(),t[i]=read();
  50. insert(u[i],v[i],t[i]);
  51. }
  52. memset(dis,,sizeof(dis));
  53. int l=dis[]=,r=q[]=;
  54. while(l<r) {
  55. int x=q[++l];
  56. bz[x]=false;
  57. for(int i=first[x]; i; i=next[i])
  58. if(dis[x]+w[i]<dis[en[i]]) {
  59. dis[en[i]]=dis[x]+w[i];
  60. if(!bz[en[i]]) bz[q[++r]=en[i]]=true;
  61. }
  62. }
  63. memset(first,tot=,sizeof(first));
  64. for(int i=; i<=m; i++) insert(v[i],u[i],t[i]);
  65. memset(dis1,,sizeof(dis1));
  66. l=dis1[q[r=]=n]=;
  67. while(l<r) {
  68. int x=q[++l];
  69. bz[x]=false;
  70. for(int i=first[x]; i; i=next[i])
  71. if(dis1[x]+w[i]<dis1[en[i]]) {
  72. dis1[en[i]]=dis1[x]+w[i];
  73. if(!bz[en[i]]) bz[q[++r]=en[i]]=true;
  74. }
  75. }
  76. memset(first1,tot=,sizeof(first1));
  77. memset(d,,sizeof(d));
  78. for(int i=; i<=m; i++) {
  79. int x=get(u[i],),y=get(v[i],dis[u[i]]+t[i]-dis[v[i]]);
  80. for(int j=dis[u[i]]; j+t[i]+dis1[v[i]]<=dis[n]+k; j++,x++,y++) insert1(x,y);
  81. }
  82. int num=(k+)*n,sum=;
  83. l=r=ans=;
  84. memset(f,,sizeof(f));
  85. for(int i=; i<=num; i++)
  86. if(!d[i]) q[++r]=i;
  87. f[]=;
  88. while(l<r) {
  89. int x=q[++l];
  90. sum++;
  91. for(int i=first1[x]; i; i=next1[i]) {
  92. if(!--d[en1[i]]) q[++r]=en1[i];
  93. f[en1[i]]+=f[x];
  94. f[en1[i]]=f[en1[i]]>p?f[en1[i]]-p:f[en1[i]];
  95. }
  96. }
  97. if(sum<num) printf("-1\n");
  98. else {
  99. for(int i=; i<=k; i++) ans=(ans+f[get(n,i)])%p;
  100. printf("%d\n",ans);
  101. }
  102. }
  103. return ;
  104. }

[Luogu] 逛公园的更多相关文章

  1. [luogu P3953] [noip2017 d1t3] 逛公园

    [luogu P3953] [noip2017 d1t3] 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张$N$个点$M$条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,$N ...

  2. [Luogu P3953] 逛公园 (最短路+拓扑排序+DP)

    题面 传送门:https://www.luogu.org/problemnew/show/P3953 Solution 这是一道神题 首先,我们不妨想一下K=0,即求最短路方案数的部分分. 我们很容易 ...

  3. luogu 3953 逛公园

    noip2017 D1T3 逛公园 某zz选手看到数据范围直接就最短路计数了,结果写错了爆零 题目大意: N个点M条边构成的有向图,且没有自环和重边.其中1号点是起点,N号点是公园的终点,每条边有一个 ...

  4. Luogu P3953 逛公园(最短路+记忆化搜索)

    P3953 逛公园 题面 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 \(N\) 个点 \(M\) 条边构成的有向图,且没有自环和重边.其中 \(1\) 号点是公园的入口,\(N\) 号点是公 ...

  5. 【图论 动态规划拆点】luoguP3953 逛公园

    经典的动态规划拆点问题. 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 NN 个点 MM 条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口, NN 号点是公园的出口,每条边有一个非负 ...

  6. [vijos P1083] 小白逛公园

    不知怎地竟有种错觉此题最近做过= =目测是类似的?那道题貌似是纯动归? 本来今晚想做两道题的,一道是本题,一道是P1653疯狂的方格取数或NOI08 Employee,看看现在的时间目测这个目标又达不 ...

  7. Bzoj 1756: Vijos1083 小白逛公园 线段树

    1756: Vijos1083 小白逛公园 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1021  Solved: 326[Submit][Statu ...

  8. BZOJ 1756: Vijos1083 小白逛公园

    题目 1756: Vijos1083 小白逛公园 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 856  Solved: 264[Submit][Sta ...

  9. JDOJ-P1260 VIJOS-P1083 小白逛公园

    首先,在这里给大家推荐一个网站,https://neooj.com:8082,这是我母校的网站 言归正传,题目描述 VIJOS-P1083 小白逛公园 Time Limit: 1 Sec  Memor ...

随机推荐

  1. Devexpress xaf用代码打开菜单(Navigation Item)

    第一种:ViewController继承WindowController public abstract class MyWindowController : WindowController { p ...

  2. 后端排序,debug模式中map的顺序出错

    js中map遍历的顺序是按照插入的顺序来执行的.如果map的来源是字符串转换的,那么就会按照字符串中key值的顺序进行遍历.千万不要被debug中显示的顺序误导,这里应该是为了方便查看对key进行了字 ...

  3. ingress之tls和path使用

    ingress tls 上节课给大家展示了 traefik 的安装使用以及简单的 ingress 的配置方法,这节课我们来学习一下 ingress tls 以及 path 路径在 ingress 对象 ...

  4. Lua模除运算的大坑

    问题 对负数进行模除运算遇到的坑,Lua的%运算与C++的%有差异 实践 结论 Lua%运算的基本公式 a % b = a - ( ( a // b ) * b ) 1.在C,C++中 %运算符的取整 ...

  5. .Net C# Dictionary 和参数字符串互转

    #region Parse #region Dictionary Parse To String /// <summary> /// Dictionary Parse To String ...

  6. codeforce E - Minimal Labels+hdu 4857

    两个题目的意思差不多 都是希望得出的拓扑序如果有多种 要求输出字典序小的情况 这里引用一个大佬的博客 关于为什么不能直接建图然后用小根堆解决这个问题(http://blog.csdn.net/rgno ...

  7. VS 之github

    VS 代码发布到TFS上 1. 登录  visualstudio.com. 进入 https://qgb.visualstudio.com Create Project 这里是相当于新建了一个文件夹 ...

  8. [NOIP10.3模拟赛]3.w题解--神奇树形DP

    题目链接: 咕 闲扯: 这题考场上把子任务都敲满了,5个namespace,400行11k 结果爆0了哈哈,因为写了个假快读只能读入一位数,所以手测数据都过了,交上去全TLE了 把边分成三类:0. 需 ...

  9. c#如何使用MemoryStream和BinaryFormatter进行对象的序列化和返序列化

    1 下面是我写的一个序列化的类 public static class ObjSerialize { /// <summary> /// 将对象数组obj序列化,内存中的缓冲区的数据序列化 ...

  10. HashMap闭环(死循环)的详细原因(转)

    为何出现死循环简要说明 HashMap是非线程安全的,在并发场景中如果不保持足够的同步,就有可能在执行HashMap.get时进入死循环,将CPU的消耗到100%. HashMap采用链表解决Hash ...