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

#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

typedef double DB;
typedef long long LL;
#define pb push_back
#define mp make_pair

const int maxN = 444;
const int maxE = maxN * maxN * 2;
int n, m, c[maxN];
int h[maxN], to[maxE], nxt[maxE], ec;

struct Node {
  int u, w;
  LL p;
  bool operator<(const Node &rhs) const { return w > rhs.w; }
  Node() {}
  Node(int u, int w, LL p) : u(u), w(w), p(p) {}
};

priority_queue<Node> que;

vector<int> adj[maxN];
int dis[maxN];

int main() {
#ifdef lol
  freopen("hd.in", "r", stdin);
  freopen("hd.out", "w", stdout);
#endif

  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i - 1]);
  ec = 0;
  memset(h, -1, sizeof h);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;
    adj[u].pb(v);
    adj[v].pb(u);
    to[ec] = v, nxt[ec] = h[u], h[u] = ec++;
    to[ec] = u, nxt[ec] = h[v], h[v] = ec++;
  }

  que.push(Node(0, c[0], 1LL));
  int ans = INF;

  while (!que.empty()) {
    int u = que.top().u;
    int w = que.top().w;
    LL p = que.top().p;
    que.pop();
    for (int i = h[u]; i != -1; i = nxt[i]) {
      int v = to[i];
      int vw = w;
      LL vp = p;
      if (!((vp >> v) & 1)) {
        for (int j = 0; j < (int)adj[u].size(); ++j) {
          if (!((vp >> adj[u][j]) & 1)) {
            vp |= 1LL << adj[u][j];
            vw += c[adj[u][j]];
          }
        }
        que.push(Node(v, vw, vp));
        if (v == n - 1) {
          for (int j = 0; j < (int)adj[v].size(); ++j) {
            if (!((vp >> adj[v][j]) & 1)) {
              vp |= 1LL << adj[v][j];
              vw += c[adj[v][j]];
            }
          }
          ans = min(ans, vw);
        }
      }
    }
  }
  printf("%d\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace ::std;

#define ls t << 1
#define rs (t << 1) | 1
#define sqr(x) ((x) * (x))
#define mem(x, a) memset(x, a, sizeof(x))

typedef long long ll;
// typedef __int64 ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int maxn = 40 + 10;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
int n, m, cost[maxn], vis[maxn], d[maxn][maxn];
int ans;

void DFS(int u) {
  if (u == n) {
    int tmp = 0;
    int vv[maxn];
    mem(vv, 0);
    for (int i = 1; i <= n; i++) {
      if (vis[i]) {
        tmp += cost[i];
        for (int j = 1; j <= n; j++)
          if (d[i][j] && !vis[j] && !vv[j]) {
            tmp += cost[j];
            vv[j] = 1;
          }
      }
    }
    ans = min(tmp, ans);
    return;
  }
  for (int i = 2; i <= n; i++) {
    if (!vis[i] && d[u][i]) {
      vis[i] = 1;
      DFS(i);
      vis[i] = 0;
    }
  }
}

int main() {
  while (~scanf("%d%d", &n, &m)) {
    mem(d, 0);
    mem(vis, 0);
    for (int i = 1; i <= n; i++)
      scanf("%d", &cost[i]);
    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      d[x][y] = d[y][x] = 1;
    }
    vis[1] = 1;
    ans = inf;
    DFS(1);
    printf("%d\\n", ans);
  }
}