質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

アルゴリズム

アルゴリズムとは、定められた目的を達成するために、プログラムの理論的な動作を定義するものです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

0回答

1519閲覧

なぜかWAとREが連発する

s8079

総合スコア36

C++11

C++11は2011年に容認されたC++のISO標準です。以前のC++03に代わるもので、中枢の言語の変更・修正、標準ライブラリの拡張・改善を加えたものです。

アルゴリズム

アルゴリズムとは、定められた目的を達成するために、プログラムの理論的な動作を定義するものです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2019/08/19 10:03

前提・実現したいこと

AtCoderのヤマト運輸プログラミングコンテストに参加しました.
しかしながら,問題1でWAとREを連発してしまい,自分の未熟さを感じています.
ただ,コンテスト期間が終了した今になっても,なぜWAとREが出たのかが不明です.
問題文に記載されていたサンプルケースでは正しい値を出力しました.
オーバーフローなども考え,型を変えてみたりしましたが見当違いなようです.
間違っている箇所あるいはこのプログラムでは正しく動かないテストケースなどを教えていただけると助かります.
問題文を転載して良いのか不明なため,問題文はここには記載していません.
問題文を読める環境の方はぜひ解答をよろしくお願いします.

ソースコードの概略

各情報を管理するためのクラスを用意する.
各情報から重み付き有向グラフを作成し,動的計画法を用いて任意の地点から任意の地点への最短距離を求める.
出発地点と到着地点について,車両と台車の違いに注意しながら,それぞれ最寄りの交差点を探す.
出発地点と到着地点が同じ道路上に存在する場合は,車両と台車の違いに注意しながら,出発地点と到着地点の距離を求める.
出発地点の最寄りの交差点から到着地点の最寄りの交差点への最短距離を求める.
出発地点から到着地点への最短距離を出力する.

該当のソースコード

C++

1#include <algorithm> 2#include <climits> 3#include <iostream> 4#include <map> 5#include <set> 6#include <vector> 7class R { 8public: 9 std::string id; 10 uint32_t length; 11 bool flag; 12}; 13std::istream &operator>>(std::istream &in, R &r) { 14 in >> r.id >> r.length >> r.flag; 15 return in; 16} 17class C { 18public: 19 std::string id; 20 std::string r_in; 21 std::string r_out; 22}; 23std::istream &operator>>(std::istream &in, C &c) { 24 in >> c.id >> c.r_in >> c.r_out; 25 return in; 26} 27class PD { 28public: 29 std::string id; 30 std::string r; 31 uint32_t dist; 32}; 33std::istream &operator>>(std::istream &in, PD &pd) { 34 in >> pd.id >> pd.r >> pd.dist; 35 return in; 36} 37class Q { 38public: 39 bool type; 40 std::string x; 41 std::string y; 42}; 43std::istream &operator>>(std::istream &in, Q &q) { 44 in >> q.type >> q.x >> q.y; 45 return in; 46} 47class Info { 48public: 49 std::string id; 50 uint32_t dist; 51}; 52class Search { 53private: 54 std::map<std::pair<std::string, std::string>, uint32_t> dist; 55public: 56 std::vector<R> rs; 57 std::vector<C> cs; 58 std::vector<PD> ps; 59 std::vector<PD> ds; 60 std::vector<Q> qs; 61 std::vector<std::string> get_all() { 62 std::vector<std::string> all; 63 std::set<std::string> tmp; 64 for (const C &c : this->cs) { 65 tmp.emplace(c.id); 66 } 67 all.resize(tmp.size()); 68 std::copy(tmp.cbegin(), tmp.cend(), all.begin()); 69 return all; 70 } 71 uint32_t get_length(std::pair<std::string, std::string> key) { 72 std::vector<C> cs_in, cs_out; 73 std::copy_if(this->cs.cbegin(), this->cs.cend(), std::back_inserter(cs_in), [&](const C &c) { 74 return c.id == key.first ? true : false; 75 }); 76 std::copy_if(this->cs.cbegin(), this->cs.cend(), std::back_inserter(cs_out), [&](const C &c) { 77 return c.id == key.second ? true : false; 78 }); 79 for (const C &c_in : cs_in) { 80 const std::vector<C>::const_iterator itr = find_if(cs_out.cbegin(), cs_out.cend(), [&](const C &c_out) { 81 return c_in.r_out == c_out.r_in ? true : false; 82 }); 83 if (itr != cs_out.cend()) { 84 const std::string tmp = itr->r_in.substr(0, itr->r_in.size() - 2); 85 return std::find_if(this->rs.cbegin(), this->rs.cend(), [&](const R &r) { 86 return r.id == tmp; 87 })->length; 88 } 89 } 90 return UINT_MAX; 91 } 92 void make_graph() { 93 const std::vector<std::string> all = get_all(); 94 for (size_t i = 0; i < all.size(); ++i) { 95 for (size_t j = 0; j < all.size(); ++j) { 96 if (i == j) { 97 dist.emplace(std::make_pair(all.at(i), all.at(j)), 0); 98 } 99 else { 100 std::pair<std::string, std::string> key = std::make_pair(all.at(i), all.at(j)); 101 dist.emplace(key, this->get_length(key)); 102 } 103 } 104 } 105 for(size_t k = 0; k < all.size(); ++k) { 106 for(size_t i = 0; i < all.size(); ++i) { 107 for(size_t j = 0; j < all.size(); ++j) { 108 uint32_t dist_ik = dist.at(std::make_pair(all.at(i), all.at(k))); 109 uint32_t dist_kj = dist.at(std::make_pair(all.at(k), all.at(j))); 110 if (dist_ik < UINT_MAX && dist_kj < UINT_MAX) { 111 dist.at(std::make_pair(all.at(i), all.at(j))) = std::min(dist.at(std::make_pair(all.at(i), all.at(j))), dist_ik + dist_kj); 112 } 113 } 114 } 115 } 116 } 117 std::vector<Info> get_nearest(const std::string &id, const int32_t &flag) { 118 std::vector<Info> nearest; 119 if (id.front() == 'D') { 120 const std::vector<PD>::const_iterator x = find_if(this->ds.cbegin(), this->ds.cend(), [&](const PD &d) { 121 return d.id == id ? true : false; 122 }); 123 const std::vector<C>::const_iterator c_in = find_if(cs.cbegin(), cs.cend(), [&](const C &c) { 124 return c.r_in.substr(0, c.r_in.size() - 2) == x->r ? true : false; 125 }); 126 const std::vector<C>::const_iterator c_out = find_if(cs.cbegin(), cs.cend(), [&](const C &c) { 127 return c.r_out.substr(0, c.r_out.size() - 2) == x->r && c.id != c_in->id ? true : false; 128 }); 129 Info info; 130 info.id = c_in->id; 131 if (c_in->r_in.substr(c_in->r_in.size() - 2) == "SE") { 132 std::string tmp = c_in->r_in.substr(0, c_in->r_in.size() - 2); 133 info.dist = find_if(rs.cbegin(), rs.cend(), [&](const R &r) { 134 return r.id == tmp ? true : false; 135 })->length - x->dist; 136 } 137 else { 138 info.dist = x->dist; 139 } 140 nearest.emplace_back(info); 141 info.id = c_out->id; 142 if (c_out->r_out.substr(c_out->r_out.size() - 2) == "SE") { 143 info.dist = x->dist; 144 } 145 else { 146 std::string tmp = c_out->r_out.substr(0, c_out->r_out.size() - 2); 147 info.dist = find_if(rs.cbegin(), rs.cend(), [&](const R &r) { 148 return r.id == tmp ? true : false; 149 })->length - x->dist; 150 } 151 nearest.emplace_back(info); 152 } 153 else { 154 const std::vector<PD>::const_iterator x = find_if(this->ps.cbegin(), this->ps.cend(), [&](const PD &p) { 155 return p.id == id ? true : false; 156 }); 157 if (flag <= 0) { 158 Info info; 159 const std::vector<C>::const_iterator c_in = find_if(cs.cbegin(), cs.cend(), [&](const C &c) { 160 return c.r_in == x->r + x->id.substr(x->id.size() - 2) ? true : false; 161 }); 162 info.id = c_in->id; 163 if (c_in->r_in.substr(c_in->r_in.size() - 2) == "SE") { 164 std::string tmp = c_in->r_in.substr(0, c_in->r_in.size() - 2); 165 info.dist = find_if(rs.cbegin(), rs.cend(), [&](const R &r) { 166 return r.id == tmp ? true : false; 167 })->length - x->dist; 168 } 169 else { 170 info.dist = x->dist; 171 } 172 nearest.emplace_back(info); 173 } 174 if (flag >= 0) { 175 Info info; 176 const std::vector<C>::const_iterator c_out = find_if(cs.cbegin(), cs.cend(), [&](const C &c) { 177 return c.r_out == x->r + x->id.substr(x->id.size() - 2) ? true : false; 178 }); 179 info.id = c_out->id; 180 if (c_out->r_out.substr(c_out->r_out.size() - 2) == "SE") { 181 info.dist = x->dist; 182 } 183 else { 184 std::string tmp = c_out->r_out.substr(0, c_out->r_out.size() - 2); 185 info.dist = find_if(rs.cbegin(), rs.cend(), [&](const R &r) { 186 return r.id == tmp ? true : false; 187 })->length - x->dist; 188 } 189 nearest.emplace_back(info); 190 } 191 } 192 return nearest; 193 } 194 uint32_t get_distance(const std::string &x_id, const std::string &y_id) { 195 std::vector<PD>::const_iterator x = x_id.front() == 'D' ? find_if(this->ds.cbegin(), this->ds.cend(), [&](const PD &d) { 196 return d.id == x_id ? true : false; 197 }) : find_if(this->ps.cbegin(), this->ps.cend(), [&](const PD &p) { 198 return p.id == x_id ? true : false; 199 }); 200 std::vector<PD>::const_iterator y = y_id.front() == 'D' ? find_if(this->ds.cbegin(), this->ds.cend(), [&](const PD &d) { 201 return d.id == y_id ? true : false; 202 }) : find_if(this->ps.cbegin(), this->ps.cend(), [&](const PD &p) { 203 return p.id == y_id ? true : false; 204 }); 205 if (x->r == y->r) { 206 if (x_id.front() == 'D') { 207 if (x->dist <= y->dist) { 208 return y->dist - x->dist; 209 } 210 else { 211 return x->dist - y->dist; 212 } 213 } 214 else { 215 const std::string x_dir = x->r.substr(x->r.size() - 2); 216 const std::string y_dir = y->r.substr(y->r.size() - 2); 217 if (x_dir == "SE" && y_dir == "SE" && x->dist <= y->dist) { 218 return y->dist - x->dist; 219 } 220 else if (x_dir == "ES" && y_dir == "ES" && x->dist >= y->dist) { 221 return x->dist - y->dist; 222 } 223 } 224 } 225 return UINT_MAX; 226 } 227 uint32_t search(const Q &q) { 228 std::vector<Info> ss = this->get_nearest(q.x, q.type ? 0 : -1); 229 std::vector<Info> es = this->get_nearest(q.y, q.type ? 0 : 1); 230 uint32_t min = this->get_distance(q.x, q.y); 231 for (const Info &s : ss) { 232 for (const Info &e : es) { 233 uint32_t tmp = s.dist + this->dist.at(std::make_pair(s.id, e.id)) + e.dist; 234 if (tmp < min) { 235 min = tmp; 236 } 237 } 238 } 239 return min < UINT_MAX ? min : -1; 240 } 241}; 242std::istream &operator>>(std::istream &in, Search &s) { 243 uint32_t alpha, beta, gamma, delta, eta; 244 in >> alpha >> beta >> gamma >> delta >> eta; 245 s.rs.resize(alpha); 246 s.cs.resize(beta); 247 s.ps.resize(gamma); 248 s.ds.resize(delta); 249 s.qs.resize(eta); 250 for (size_t i = 0; i < s.rs.size(); ++i) { 251 in >> s.rs.at(i); 252 } 253 for (size_t i = 0; i < s.cs.size(); ++i) { 254 in >> s.cs.at(i); 255 } 256 for (size_t i = 0; i < s.ps.size(); ++i) { 257 in >> s.ps.at(i); 258 } 259 for (size_t i = 0; i < s.ds.size(); ++i) { 260 in >> s.ds.at(i); 261 } 262 for (size_t i = 0; i < s.qs.size(); ++i) { 263 in >> s.qs.at(i); 264 } 265 return in; 266} 267int main() { 268 Search s; 269 std::cin >> s; 270 s.make_graph(); 271 for (const Q &q : s.qs) { 272 std::cout << s.search(q) << std::endl; 273 } 274 return EXIT_SUCCESS; 275}

補足情報(FW/ツールのバージョンなど)

C++14(GCC 5.4.1)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問