結果

問題 No.134 走れ!サブロー君
ユーザー codershifthcodershifth
提出日時 2015-08-15 01:34:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,944 bytes
コンパイル時間 1,354 ms
コンパイル使用メモリ 160,888 KB
実行使用メモリ 5,828 KB
最終ジャッジ日時 2023-09-25 11:52:16
合計ジャッジ時間 2,213 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 13 ms
5,792 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class GoSabrou {
public:
    void solve(void) {
            // 巡回セールスマン問題の応用
            int x0,y0;
            cin>>x0>>y0;

            int N;
            cin>>N;

            // 配達先
            vector<tuple<int,int,double>> dest;

            // 出発地も登録しておく
            dest.emplace_back(x0,y0,0);
            REP(i,N)
            {
                int x,y;
                double w;
                cin>>x>>y>>w;
                dest.emplace_back(x,y,w);
            }
            ++N;

            // 距離を事前計算しておく
            const int inf = (1<<30);
            vector<vector<int>> dist(N,vector<int>(N,inf));
            REP(i,N)
            FOR(j,i+1,N)
            {
                const auto &p = dest[i];
                const auto &q = dest[j];

                // 距離はマンハッタン距離
                int d = abs(get<0>(p)-get<0>(q)) + abs(get<1>(p)-get<1>(q));
                dist[i][j] = dist[j][i] = d;
            }

            // 総荷重量を計算しておく
            // weights[unvis] := 未配達先が unvis であるときの総重量
            vector<double> weights(1<<N, 0);
            REP(unvis, (1<<N))
            {
                int w = 0;
                REP(i,N) if ( unvis & (1<<i) )
                    w += get<2>(dest[i]);
                weights[unvis] = w;
            }

            // dp[unvis][i] := 未配達先が unvis で現在地が v のときの配達終了までの最短時間
            vector<vector<double>> dp(1<<N, vector<double>(N,inf));

            // O((1<<N)*N^2)
            // 頂点 0 からスタートして全部巡回させる
            dp[(1<<N)-1][0] = 0;

            // 0 頂点を除いた全ビットからスタート
            for (int unvis = (1<<N)-2; unvis >= 0; --unvis)
            {
                // 次の移動先 v 毎
                REP(v,N)
                {
                    // 移動前 u 毎
                    REP(u,N)
                    {
                        // 移動前が未到達なら飛ばす
                        if ( unvis & (1<<u) )
                            continue;
                        double cost = (weights[unvis]+100.0)/120.0 * dist[u][v] + get<2>(dest[v]);
                        dp[unvis][v] = min(dp[unvis][v], dp[unvis|(1<<u)][u] + cost);
                    }
                }
            }
            // 一周してもどってきた
            cout<<setprecision(20)<<dp[0][0]<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new GoSabrou();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0