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

#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>

using namespace std;
typedef pair<int, int> mp;
int mins(int p, int q) {
  if (p == -1)
    return q;
  else if (q == -1 || p < q)
    return p;
  else
    return q;
}
int max(int p, int q) {
  if (p < q)
    return q;
  else
    return p;
}

int md[3000][3000];
int mm[3000];
int dt[3000];
bool ch[3000];
int m, n, k;

int main(void) {
  memset(md, -1, sizeof(md));
  memset(dt, -1, sizeof(dt));
  scanf("%d%d%d", &n, &m, &k);
  int i, j;
  int a, b, c;
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    md[a - 1][b - 1] = c;
    md[b - 1][a - 1] = c;
  }
  for (i = 0; i < k; i++) {
    scanf("%d", &a);
    mm[i] = a - 1;
  }
  int st, no, kyo, mk;
  mp pa;
  int ans = 0;
  priority_queue<mp, vector<mp>, greater<mp>> que;
  for (i = 0; i < k; i++) {
    st = mm[i];
    dt[st] = 0;
  }
  memset(ch, false, sizeof(ch));
  // ch[st]=true;
  que.push(make_pair(0, st));
  while (!que.empty()) {
    pa = que.top();
    que.pop();
    kyo = pa.first;
    no = pa.second;
    if (ch[no] == false) {
      for (j = 0; j < n; j++) {
        if (md[no][j] != -1 && ch[j] == false) {
          mk = md[no][j];
          dt[j] = mins(dt[j], kyo + mk);
          que.push(make_pair(dt[j], j));
        }
      }
      ch[no] = true;
    }
  }
  for (i = 0; i < n; i++) {
    ans = max(ans, dt[i]);
    // printf("%d\\n",dt[i]);
  }
  // printf("\\n");
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (md[i][j] != -1) {
        ans = max(ans, (int)((dt[i] + dt[j] + md[i][j] + 1) / 2));
        // printf("%d %d %d\\n",i+1,j+1,(int)((dt[i]+dt[j]+md[i][j]+1)/2));
      }
    }
  }

  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <queue>

using namespace std;
typedef pair<int, int> mp;
int mins(int p, int q) {
  if (p == -1)
    return q;
  else if (q == -1 || p < q)
    return p;
  else
    return q;
}
int max(int p, int q) {
  if (p < q)
    return q;
  else
    return p;
}

int md[3000][3000];
int mm[3000];
int dt[3000];
bool ch[3000];
int m, n, k;

int main(void) {
  memset(md, -1, sizeof(md));
  memset(dt, -1, sizeof(dt));
  scanf("%d%d%d", &n, &m, &k);
  int i, j;
  int a, b, c;
  for (i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    md[a - 1][b - 1] = c;
    md[b - 1][a - 1] = c;
  }
  for (i = 0; i < k; i++) {
    scanf("%d", &a);
    mm[i] = a - 1;
  }

  int st, no, kyo, mk;
  mp pa;
  int ans = 0;
  priority_queue<mp, vector<mp>, greater<mp>> que;
  for (i = 0; i < k; i++) {
    st = mm[i];
    memset(ch, false, sizeof(ch));
    dt[st] = 0;
    // ch[st]=true;
    que.push(make_pair(0, st));
    while (!que.empty()) {
      pa = que.top();
      que.pop();
      kyo = pa.first;
      no = pa.second;
      if (ch[no] == false) {
        for (j = 0; j < n; j++) {
          if (md[no][j] != -1 && ch[j] == false) {
            mk = md[no][j];
            dt[j] = mins(dt[j], kyo + mk);
            que.push(make_pair(dt[j], j));
          }
        }
        ch[no] = true;
      }
    }
  }
  for (i = 0; i < n; i++) {
    ans = max(ans, dt[i]);
    // printf("%d\\n",dt[i]);
  }
  // printf("\\n");
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (md[i][j] != -1) {
        ans = max(ans, (int)((dt[i] + dt[j] + md[i][j] + 1) / 2));
        // printf("%d %d %d\\n",i+1,j+1,(int)((dt[i]+dt[j]+md[i][j]+1)/2));
      }
    }
  }

  printf("%d\\n", ans);
  return 0;
}