Problem p00750 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, k, n) for (int i = k; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = k; i <= (int)(n); ++i)

#define MEMSET(v, h) memset((v), (h), sizeof(v))

struct Node {
  int index;
  string str;
  Node(int index, string str) : index(index), str(str) { ; }
  bool operator<(const Node &rhs) const {
    if (str.size() != rhs.str.size()) {
      return str.size() > rhs.str.size();
    }
    return str > rhs.str;
  }
};

int n, a, s, g;
vector<pair<int, string>> graph[50];
char str[100];
bool visit[500][50];
string strs[500][50];

int main() {
  while (scanf("%d %d %d %d", &n, &a, &s, &g), n) {
    REP(i, 500) {
      REP(j, n) { strs[i][j] = "}"; }
    }
    REP(i, n) { graph[i].clear(); }
    REP(i, a) {
      int x, y;
      scanf("%d %d %s", &x, &y, str);
      graph[x].push_back(make_pair(y, str));
    }
    MEMSET(visit, false);
    string ans = "}";
    priority_queue<Node> que;
    que.push(Node(s, ""));
    while (!que.empty()) {
      Node node = que.top();
      que.pop();
      // cout << node.index << " " << node.str << endl;
      if (visit[node.str.size()][node.index]) {
        continue;
      }
      visit[node.str.size()][node.index] = true;
      if (node.index == g) {
        ans = min(ans, node.str);
      }
      REP(i, graph[node.index].size()) {
        int to = graph[node.index][i].first;
        string nstr = node.str + graph[node.index][i].second;
        if (nstr.size() > 12 * n) {
          continue;
        }
        if (visit[nstr.size()][to]) {
          continue;
        }
        if (strs[nstr.size()][to] < nstr) {
          continue;
        }
        strs[nstr.size()][to] = nstr;
        que.push(Node(to, nstr));
      }
    }
    if (ans != "}" && ans.size() <= 6 * n) {
      cout << ans << endl;
    } else {
      puts("NO");
    }
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, k, n) for (int i = k; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = k; i <= (int)(n); ++i)

#define MEMSET(v, h) memset((v), (h), sizeof(v))

struct Node {
  int index;
  string str;
  Node(int index, string str) : index(index), str(str) { ; }
  bool operator<(const Node &rhs) const {
    if (str.size() != rhs.str.size()) {
      return str.size() > rhs.str.size();
    }
    return str > rhs.str;
  }
};

int n, a, s, g;
vector<pair<int, string>> graph[50];
char str[100];
bool visit[500][50];
string strs[500][50];

int main() {
  while (scanf("%d %d %d %d", &n, &a, &s, &g), n) {
    REP(i, 500) {
      REP(j, n) { strs[i][j] = "}"; }
    }
    REP(i, n) { graph[i].clear(); }
    REP(i, a) {
      int x, y;
      scanf("%d %d %s", &x, &y, str);
      graph[x].push_back(make_pair(y, str));
    }
    MEMSET(visit, false);
    string ans = "}";
    priority_queue<Node> que;
    que.push(Node(s, ""));
    while (!que.empty()) {
      Node node = que.top();
      que.pop();
      // cout << node.index << " " << node.str << endl;
      if (visit[node.str.size()][node.index]) {
        continue;
      }
      visit[node.str.size()][node.index] = true;
      if (node.index == g) {
        ans = min(ans, node.str);
      }
      REP(i, graph[node.index].size()) {
        int to = graph[node.index][i].first;
        string nstr = node.str + graph[node.index][i].second;
        if (nstr.size() > 480) {
          continue;
        }
        if (visit[nstr.size()][to]) {
          continue;
        }
        if (strs[nstr.size()][to] < nstr) {
          continue;
        }
        strs[nstr.size()][to] = nstr;
        que.push(Node(to, nstr));
      }
    }
    if (ans != "}" && ans.size() <= 240) {
      cout << ans << endl;
    } else {
      puts("NO");
    }
  }
}