Problem p03722 (C++)

Buggy submission (status: Runtime Error):
/* ***********************************************
Author        :yang12138
Created Time  :2017年05月18日 星期四 11时00分40秒
File Name     :D.cpp
************************************************ */
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef pair<int, int> pii;
#define lson (root << 1)
#define rson (root << 1 | 1)

const int N = 1005;
const ll INF = 1e18;

struct Edge {
  int to, nex, cost;
} e[N << 1];

int head[N], cnt;
void add_edge(int u, int v, int w) {
  e[cnt].to = v, e[cnt].nex = head[u], e[cnt].cost = w, head[u] = cnt++;
}

void init() { mem(head, -1), cnt = 0; }

ll dis[N];
bool vis[N];
int n;
bool find_negative_loop() {
  for (int i = 2; i <= n; i++)
    dis[i] = INF;
  dis[1] = 0;
  for (int k = 0; k < n; k++) {
    for (int u = 1; u <= n; u++) {
      for (int i = head[u]; ~i; i = e[i].nex) {
        int v = e[i].to;
        if (dis[v] > dis[u] + e[i].cost) {
          dis[v] = dis[u] + e[i].cost;
          if (k == n - 1)
            return 1;
        }
      }
    }
  }
  return 0;
}

ll spfa() {
  for (int i = 2; i <= n; i++)
    dis[i] = INF;
  dis[1] = 0;
  queue<int> que;
  que.push(1);
  mem(vis, 0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = e[i].nex) {
      int v = e[i].to;
      if (dis[v] > dis[u] + e[i].cost) {
        dis[v] = dis[u] + e[i].cost;
        if (!vis[v])
          que.push(v), vis[v] = 1;
      }
    }
  }
  return dis[n];
}

void bfs(int s, bool *can) {
  mem(can, 0);
  can[s] = 1;
  queue<int> que;
  que.push(s);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = head[u]; ~i; i = e[i].nex) {
      int v = e[i].to;
      if (can[v])
        continue;
      can[v] = 1, que.push(v);
    }
  }
}

int u[N], v[N], w[N];

void solve() {
  init();
  int m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u[i] >> v[i] >> w[i];
    add_edge(v[i], u[i], w[i]);
  }

  bfs(n, vis);
  mem(head, -1), cnt = 0;
  for (int i = 1; i <= m; i++) {
    if (vis[u[i]] && vis[v[i]]) {
      add_edge(u[i], v[i], -w[i]);
    }
  }

  if (find_negative_loop())
    printf("inf\n");
  else
    printf("%lld\n", -spfa());
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  solve();
  return 0;
}


Fixed submission:
/* ***********************************************
Author        :yang12138
Created Time  :2017年05月18日 星期四 11时00分40秒
File Name     :D.cpp
************************************************ */
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef pair<int, int> pii;
#define lson (root << 1)
#define rson (root << 1 | 1)

const int N = 1005;
const ll INF = 1e18;

struct Edge {
  int to, nex, cost;
} e[N << 1];

int head[N], cnt;
void add_edge(int u, int v, int w) {
  e[cnt].to = v, e[cnt].nex = head[u], e[cnt].cost = w, head[u] = cnt++;
}

void init() { mem(head, -1), cnt = 0; }

ll dis[N];
bool vis[N];
int n;
bool find_negative_loop() {
  for (int i = 2; i <= n; i++)
    dis[i] = INF;
  dis[1] = 0;
  for (int k = 0; k < n; k++) {
    for (int u = 1; u <= n; u++) {
      for (int i = head[u]; ~i; i = e[i].nex) {
        int v = e[i].to;
        if (dis[v] > dis[u] + e[i].cost) {
          dis[v] = dis[u] + e[i].cost;
          if (k == n - 1)
            return 1;
        }
      }
    }
  }
  return 0;
}

ll spfa() {
  for (int i = 2; i <= n; i++)
    dis[i] = INF;
  dis[1] = 0;
  queue<int> que;
  que.push(1);
  mem(vis, 0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = e[i].nex) {
      int v = e[i].to;
      if (dis[v] > dis[u] + e[i].cost) {
        dis[v] = dis[u] + e[i].cost;
        if (!vis[v])
          que.push(v), vis[v] = 1;
      }
    }
  }
  return dis[n];
}

void bfs(int s, bool *can) {
  mem(can, 0);
  can[s] = 1;
  queue<int> que;
  que.push(s);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = head[u]; ~i; i = e[i].nex) {
      int v = e[i].to;
      if (can[v])
        continue;
      can[v] = 1, que.push(v);
    }
  }
}

int u[N << 1], v[N << 1], w[N << 1];

void solve() {
  init();
  int m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u[i] >> v[i] >> w[i];
    add_edge(v[i], u[i], w[i]);
  }

  bfs(n, vis);
  mem(head, -1), cnt = 0;
  for (int i = 1; i <= m; i++) {
    if (vis[u[i]] && vis[v[i]]) {
      add_edge(u[i], v[i], -w[i]);
    }
  }

  if (find_negative_loop())
    printf("inf\n");
  else
    printf("%lld\n", -spfa());
}

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  solve();
  return 0;
}