记录编号 352127 评测结果 AAAAAAAAAA
题目名称 [NOIP 2012]文化之旅 最终得分 100
用户昵称 Gravatar小e 是否通过 通过
代码语言 C++ 运行时间 0.015 s
提交时间 2016-11-16 21:28:20 内存使用 0.49 MiB
显示代码纯文本
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <set>
  6. using namespace std;
  7.  
  8. const int maxnumber = 110;
  9. int n, k, m, s, t;
  10. // 点数 文化数 边数 起点 终点
  11. struct Edge
  12. {
  13. int to, w, next;
  14. }edges[maxnumber * maxnumber];
  15. int head[maxnumber], tot;
  16. int type[maxnumber], G[maxnumber][maxnumber];
  17. struct Node
  18. {
  19. int pos;
  20. set <int> S;// 记录走过的文化
  21. inline Node(const int& a, const set <int>& t): pos(a), S(t) {}
  22. };
  23. queue <Node> Q;
  24. int dis[maxnumber];
  25.  
  26. inline void AddEdge(const int& from, const int& to, const int& w)
  27. {
  28. edges[++tot].to = to;
  29. edges[tot].w = w;
  30. edges[tot].next = head[from];
  31. head[from] = tot;
  32. }
  33.  
  34. inline void BFS()
  35. {
  36. memset(dis, 0x3f, sizeof dis);
  37. dis[1] = 0;
  38. set <int> S;
  39. S.insert(type[1]);
  40. Q.push(Node(s, S));
  41. while (!Q.empty()) {
  42. Node front = Q.front(); Q.pop();
  43. int pos = front.pos;
  44. set <int> S = front.S;
  45. for (int i = head[pos]; i; i = edges[i].next) {
  46. int to = edges[i].to, w = edges[i].w;
  47. bool flag = 0;
  48. for (int j = 1; j <= k; ++j) {
  49. if (G[type[to]][j] && S.count(j)) { flag = 1; break; }
  50. }// 文化冲突
  51. if (!flag) {
  52. if (dis[to] > dis[pos] + w) {
  53. dis[to] = dis[pos] + w;
  54. set <int> tmp = S;
  55. tmp.insert(type[to]);
  56. Q.push(Node(to, tmp));
  57. }
  58. }
  59. }
  60. }
  61. if (dis[t] == 0x3f3f3f3f) printf("-1\n");
  62. else printf("%d\n", dis[t]);
  63. }
  64.  
  65. #define SUBMIT
  66. int main(int argc, char const *argv[])
  67. {
  68. #ifdef SUBMIT
  69. freopen("culture.in", "r", stdin); freopen("culture.out", "w", stdout);
  70. #endif
  71.  
  72. int from, to, w;
  73. scanf("%d%d%d%d%d", &n, &k, &m, &s, &t);
  74. for (int i = 1; i <= n; ++i)
  75. scanf("%d", &type[i]);
  76. for (int i = 1; i <= k; ++i)
  77. for (int j = 1; j <= k; ++j)
  78. scanf("%d", &G[i][j]);
  79. for (int i = 1; i <= m; ++i) {
  80. scanf("%d%d%d", &from, &to, &w);
  81. AddEdge(from, to, w);
  82. AddEdge(to, from, w);
  83. }
  84. BFS();
  85.  
  86. #ifndef SUBMIT
  87. printf("\n----------\n");
  88. getchar(); getchar();
  89. #else
  90. fclose(stdin); fclose(stdout);
  91. #endif
  92.  
  93. return 0;
  94. }