Problem p03450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <vector>

#define UNSET 100000000000

using namespace std;

typedef long long int place;

class inf {
public:
  place d;
  place l, r;
  bool operator<(const inf &obj) const { return l < obj.l; }
  bool operator==(const inf &obj) const { return l == obj.l; }
};

inf temp;
place p[100010];
int n, m, l, r, d;
vector<inf> is;
queue<place> proc;
set<place> noproc;
set<place> noyet;

void input() {
  is.clear();
  while (!proc.empty())
    proc.pop();
  noyet.clear();
  for (int i = 0; i < 100010; i++)
    p[i] = UNSET;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> d;
    noyet.insert(l);
    noyet.insert(r);
    temp.l = l;
    temp.r = r;
    temp.d = d;
    is.push_back(temp);
    temp.l = r;
    temp.r = l;
    temp.d = d * (-1);
    is.push_back(temp);
  }
}

void input_f(string fn) {
  fstream fs;
  is.clear();
  while (!proc.empty())
    proc.pop();
  noyet.clear();
  fs.open(fn, std::fstream::in);
  for (int i = 0; i < 100010; i++)
    p[i] = UNSET;
  fs >> n >> m;
  for (int i = 0; i < m; i++) {
    fs >> l >> r >> d;
    noyet.insert(l);
    noyet.insert(r);
    temp.l = l;
    temp.r = r;
    temp.d = d;
    is.push_back(temp);
    temp.l = r;
    temp.r = l;
    temp.d = d * (-1);
    is.push_back(temp);
  }
}

string solve() {
  sort(is.begin(), is.end());
  proc.push(is.front().l);
  while (!noyet.empty()) {
    if (proc.empty())
      proc.push(*(noyet.begin()));
    temp.l = proc.front();
    proc.pop();
    if (noyet.count(temp.l) == 1) {
      noyet.erase(temp.l);
      if (p[temp.l] == UNSET)
        p[temp.l] = 0;
      auto range = equal_range(is.begin(), is.end(), temp);
      for (auto itr = range.first; itr != range.second; itr++) {
        proc.push(itr->r);
        if (p[itr->r] == UNSET)
          p[itr->r] = p[itr->l] + itr->d;
        else if (p[itr->r] != p[itr->l] + itr->d)
          goto ret_no;
      }
    }
  }
  return "Yes";
ret_no:
  return "No";
}

int main(void) {
  /*
  for(int i = 1;i <= 9;i++) {
  input_f("/Users/linare/Downloads/D/in/0" + std::to_string(i) + ".txt");
  cout << solve() << endl;
  }
   */

  input();
  cout << solve() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <vector>

#define UNSET 100000000000

using namespace std;

typedef long long int place;

class inf {
public:
  place d;
  place l, r;
  bool operator<(const inf &obj) const { return l < obj.l; }
  bool operator==(const inf &obj) const { return l == obj.l; }
};

inf temp;
place p[100010];
int n, m, l, r, d;
vector<inf> is;
queue<place> proc;
set<place> noproc;
set<place> noyet;

void input() {
  is.clear();
  while (!proc.empty())
    proc.pop();
  noyet.clear();
  for (int i = 0; i < 100010; i++)
    p[i] = UNSET;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> d;
    noyet.insert(l);
    noyet.insert(r);
    temp.l = l;
    temp.r = r;
    temp.d = d;
    is.push_back(temp);
    temp.l = r;
    temp.r = l;
    temp.d = d * (-1);
    is.push_back(temp);
  }
}

void input_f(string fn) {
  fstream fs;
  is.clear();
  while (!proc.empty())
    proc.pop();
  noyet.clear();
  fs.open(fn, std::fstream::in);
  for (int i = 0; i < 100010; i++)
    p[i] = UNSET;
  fs >> n >> m;
  for (int i = 0; i < m; i++) {
    fs >> l >> r >> d;
    noyet.insert(l);
    noyet.insert(r);
    temp.l = l;
    temp.r = r;
    temp.d = d;
    is.push_back(temp);
    temp.l = r;
    temp.r = l;
    temp.d = d * (-1);
    is.push_back(temp);
  }
}

string solve() {
  sort(is.begin(), is.end());
  if (is.size() != 0)
    proc.push(is.front().l);
  while (!noyet.empty()) {
    if (proc.empty())
      proc.push(*(noyet.begin()));
    temp.l = proc.front();
    proc.pop();
    if (noyet.count(temp.l) == 1) {
      noyet.erase(temp.l);
      if (p[temp.l] == UNSET)
        p[temp.l] = 0;
      auto range = equal_range(is.begin(), is.end(), temp);
      for (auto itr = range.first; itr != range.second; itr++) {
        proc.push(itr->r);
        if (p[itr->r] == UNSET)
          p[itr->r] = p[itr->l] + itr->d;
        else if (p[itr->r] != p[itr->l] + itr->d)
          goto ret_no;
      }
    }
  }
  return "Yes";
ret_no:
  return "No";
}

int main(void) {
  /*
  for(int i = 1;i <= 9;i++) {
  input_f("/Users/linare/Downloads/D/in/0" + std::to_string(i) + ".txt");
  cout << solve() << endl;
  }
   */

  input();
  cout << solve() << endl;

  return 0;
}