# View Code of Problem 14

 #include #include using namespace std; struct MyStruct { int a; int b; int dis; }; int main() { int n, t, w, e; int a, b, dis; int c, d, dis_; vector data; vector data_; int flag = 1; while (cin >> n >> t, n != 0 && t != 0) { int ret = 0; while (t--) { cin >> a >> b >> dis; if (data.size()!=0&&a == data[data.size() - 1].a && b == data[data.size() - 1].b) { if (dis < data[data.size() - 1].dis) data[data.size() - 1].dis = dis; } else data.push_back({ a,b,dis }); } cin >> w >> e; while (w--) { cin >> c >> d; int i = 0; for (;i < data.size();i++) { if ((c == data[i].a || c == data[i].b) && (d == data[i].b || d == data[i].a)) { dis_ = data[i].dis; break; } } data_.push_back({ c,d,dis_ }); } for (int i = 0;i < data_.size();i++) { int fg = 1; for (int j = 0;j < i;j++) { if ((data_[i].a == data_[j].a && data_[i].b == data_[j].b) || (data_[i].a == data_[j].b && data_[i].b == data_[j].a)) { fg = 0; break; } } if (fg) { ret += data_[i].dis; } if (data_[i].b == e) break; } cout << "Case #" << flag++ << ": " << ret << endl; data.clear(); data_.clear(); } return 0; }

Double click to view unformatted code.

Back to problem 14