比赛 20160412 评测结果 AAAAAAAAAAAAAEEAEEEA
题目名称 正则表达式 最终得分 75
用户昵称 KZNS 运行时间 2.719 s
代码语言 C++ 内存使用 4.82 MiB
提交时间 2016-04-12 10:22:31
显示代码纯文本
  1. //KZNS
  2. #include <fstream>
  3. #include <vector>
  4. #include <utility>
  5. #include <stack>
  6. #include <queue>
  7. using namespace std;
  8. //
  9. ifstream fin ("regexp.in");
  10. ofstream fout ("regexp.out");
  11. typedef pair<int, int> pr;
  12. const int Nmax = 200003;
  13. const int Mmax = 1000003;
  14. //
  15. int N, M;
  16. vector<pr> gp[Nmax];
  17. int dtm[Nmax] = {0};
  18. int mtm[Nmax] = {0};
  19. int ttmm = 1;
  20. stack<int> tju;
  21. int ed[Nmax];
  22. bool lsd[Nmax] = {0};
  23. queue<int> ls;
  24. //
  25. void fir() {
  26. fin >> N >> M;
  27. int a, b, c;
  28. for (int i = 0; i < M; i++) {
  29. fin >> a >> b >> c;
  30. gp[a].push_back(make_pair(b, c));
  31. }
  32. }
  33. void tj(int x) {
  34. dtm[x] = mtm[x] = ttmm++;
  35. tju.push(x);
  36. for (int i = 0; i < gp[x].size(); i++) {
  37. if (!dtm[gp[x][i].first])
  38. tj(gp[x][i].first);
  39. mtm[x] = min(mtm[x], mtm[gp[x][i].first]);
  40. }
  41. if (dtm[x] == mtm[x]) {
  42. int u;
  43. while (x != tju.top()) {
  44. u = tju.top();
  45. tju.pop();
  46. mtm[u] = x;
  47. }
  48. tju.pop();
  49. }
  50. }
  51. void SPFA() {
  52. for (int i = 2; i <= N; i++)
  53. ed[i] = 0x7fffffff;
  54. ed[1] = 0;
  55. ls.push(1);
  56. lsd[1] = true;
  57. int u, k;
  58. while (!ls.empty()) {
  59. u = ls.front();
  60. ls.pop();
  61. lsd[u] = false;
  62. for (int i = 0; i < gp[u].size(); i++) {
  63. k = gp[u][i].first;
  64. if (mtm[u] == mtm[k]) {
  65. if (ed[u] < ed[k]) {
  66. ed[k] = ed[u];
  67. if (!lsd[k]) {
  68. lsd[k] = true;
  69. ls.push(k);
  70. }
  71. }
  72. }
  73. else {
  74. if (ed[u] + gp[u][i].second < ed[k]) {
  75. ed[k] = ed[u] + gp[u][i].second;
  76. if (!lsd[k]) {
  77. lsd[k] = true;
  78. ls.push(k);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. //
  86. int main() {
  87. fir();
  88. tj(1);
  89. SPFA();
  90. fout << ed[N] << endl;
  91. return 0;
  92. }
  93. //UBWH