记录编号 39409 评测结果 AAAAAAAAAA
题目名称 三元限制最短路 最终得分 100
用户昵称 GravatarZhouHang 是否通过 通过
代码语言 C++ 运行时间 1.201 s
提交时间 2012-07-10 15:03:12 内存使用 87.25 MiB
显示代码纯文本
  1. /**
  2. *Prob : patha
  3. *Data : 2012-7-10
  4. *Sol : SPFA+Hash
  5. */
  6. #include <set>
  7. #include <cstdio>
  8. #include <cstring>
  9. #define MaxN 3010
  10. #define MaxE 401000
  11. #define oo 20000000
  12. using namespace std;
  13. //cannot
  14. struct node1 {
  15. int y,last,next;
  16. } e2[MaxE];
  17. int s[MaxN];
  18. //cannot
  19. struct node2 {
  20. int y,next;
  21. } e[MaxE];
  22. int n,m,k,totk=0,tot=0;
  23. int a[MaxN];
  24. bool v[MaxN][MaxN];
  25. int pre[MaxN][MaxN];
  26. int dis[MaxN][MaxN];
  27. struct {
  28. int x,l;
  29. }list[200000];
  30. //从x到y不能到z
  31. void insert(int x,int y,int z) {
  32. e2[totk].y = z; e2[totk].last = x;
  33. e2[totk].next = s[y]; s[y] = totk;
  34. }
  35. void insert1(int x,int y) {
  36. e[tot].y = y;
  37. e[tot].next = a[x]; a[x] = tot;
  38. }
  39. //x到y后能不能到c
  40. bool can(int x,int y,int c)
  41. {
  42. int tmp = s[y];
  43. for (;tmp;tmp=e2[tmp].next) {
  44. if (e2[tmp].last==x&&e2[tmp].y==c)
  45. return false;
  46. }
  47. return true;
  48. }
  49. void spfa()
  50. {
  51. memset(dis,127,sizeof(dis));
  52. memset(v,false,sizeof(v));
  53. int open = 1, closd = 0;
  54. list[1].x = 1; list[1].l = 0;
  55. dis[0][1] = 0; pre[0][1] = 0;
  56. v[0][1] = true;
  57. while (closd<open) {
  58. int now = list[++closd].x;
  59. int last = list[closd].l;
  60. v[last][now] = false;
  61. int te = a[now];
  62. for (;te;te=e[te].next) {
  63. //可以走
  64. int i = e[te].y;
  65. if (can(last,now,i)) {
  66. if (dis[last][now]+1<dis[now][i]) {
  67. dis[now][i] = dis[last][now]+1;
  68. pre[now][i] = last;
  69. if (!v[now][i]) {
  70. v[now][i] = false;
  71. list[++open].x = i;
  72. list[open].l = now;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. void print(int x,int y)
  80. {
  81. if (pre[x][y]!=0) print(pre[x][y],x);
  82. printf("%d ",x);
  83. }
  84. int main()
  85. {
  86. freopen("patha.in","r",stdin);
  87. freopen("patha.out","w",stdout);
  88. scanf("%d%d%d",&n,&m,&k);
  89. int x,y,z;
  90. for (int i=1; i<=m; i++) {
  91. scanf("%d%d",&x,&y);
  92. tot++; insert1(x,y);
  93. tot++; insert1(y,x);
  94. }
  95. for (int i=1; i<=k; i++) {
  96. scanf("%d%d%d",&x,&y,&z);
  97. totk++; insert(x,y,z);
  98. }
  99. spfa();
  100. int ans = oo;
  101. for (int i=1; i<=n; i++)
  102. if (dis[i][n]<ans) {
  103. k = i;
  104. ans = dis[i][n];
  105. }
  106. printf("%d\n",ans);
  107. print(k,n);
  108. printf("%d\n",n);
  109. fclose(stdin); fclose(stdout);
  110. return 0;
  111. }