結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー Slephy
提出日時 2023-07-02 03:45:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 799 ms / 2,000 ms
コード長 4,982 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 210,344 KB
最終ジャッジ日時 2025-02-15 05:33:04
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://yukicoder.me/problems/no/1320
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = (int)1e9 + 1001010;
constexpr ll llINF = (ll)4e18 + 22000020;
const string endn = "\n";
template <class T> inline vector<vector<T>> vector2(size_t i, size_t j, const T &init = T()) {return vector<vector<T>>(i, vector<T>(j, init));}
const string ELEM_SEPARATION = " ", VEC_SEPARATION = endn;
template<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}
template<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? VEC_SEPARATION : "");} return o;}
template<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(auto &I : A){o << I << (--i ? ELEM_SEPARATION : "");} return o;}
template<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}
template<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}
template<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}
template<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}
ll floor(ll a, ll b) {assert(b != 0); return((a%b != 0 && ((a>0) != (b>0))) ? a/b-1 : a/b);}
ll ceil (ll a, ll b) {assert(b != 0); return((a%b != 0 && ((a>0) == (b>0))) ? a/b+1 : a/b);}
// ================================== ここまでテンプレ ==================================

template<class T> using priority_queue_greater = priority_queue<T, vector<T>, greater<T>>;

template<class T>
class CostEdge{
public:
    int to;
    T cost;

    CostEdge(int to, T cost) : to(to), cost(cost){};

    bool operator <(const CostEdge &other) const {
        if(cost != other.cost) return (cost < other.cost);
        else return (to < other.to);
    }
};
 
template<class CostType>
class WeightedGraph{
public:
    const int size;
    const CostType INF = numeric_limits<CostType>::max();
    vector<vector<CostEdge<CostType>>> edges;

    WeightedGraph(int size) : size(size), edges(size){}

    vector<CostEdge<CostType>>& operator [](size_t index){
        return edges.at(index);
    }
    const vector<CostEdge<CostType>>& operator [](size_t index)const{
        return edges.at(index);
    }

    void add_edge(size_t from, size_t to, CostType cost){
        edges[from].emplace_back(to, cost);
    }
};


enum class graph_direction{
    directed,
    undirected,
};

// 閉路が存在しない場合はwg.INFを返す
template<class T>
T shortest_cycle(const WeightedGraph<T> &wg, graph_direction graph_dirc){
    auto solve = [&](int root) -> ll {
        // rootを根とする最短路木を作る
        vector<T> distance(wg.size, wg.INF);
        vector<int> label(wg.size); // label[root] := root, label[i] := (最短路木でiの祖先である頂点(iを含む)のうち、根に最も近いもの)
        {
            priority_queue_greater<tuple<ll, int, int>> pq; // {dist, vIndex}
            pq.emplace(0, root, root);
            while(!pq.empty()){
                auto [d, now, pre] = pq.top(); pq.pop();
                if(distance[now] < d) continue;
                distance[now] = d;
                if(pre == root) label[now] = now;
                else label[now] = label[pre];

                for(auto [nxt, cost] : wg[now]){
                    pq.emplace(d+cost, nxt, now);
                }
            }
        }


        // 最短閉路を見つける
        T min_cost = wg.INF;
        for(int from = 0; from < wg.size; from++){
            if(from == root) continue;
            if(distance[from] == wg.INF) continue;
            for(auto [to, cost] : wg[from]){
                if(distance[to] == wg.INF) continue;
                if(graph_dirc == graph_direction::undirected){
                    if(label[from] == label[to]) continue;
                    if(to == root && label[from] == from) continue;
                    else chmin(min_cost, distance[from] + distance[to] + cost);
                }
                if(graph_dirc == graph_direction::directed){
                    if(to != root) continue;
                    else chmin(min_cost, distance[from] + cost);
                }
            }
        }

        return min_cost;
    };

    T ans = wg.INF;
    for(int v = 0; v < wg.size; v++){
        chmin(ans, solve(v));
    }
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; cin >> t;
    int n, m; cin >> n >> m;
    auto wg = WeightedGraph<ll>(n);
    for(int i = 0; i < m; i++){
        int u, v, w; cin >> u >> v >> w;
        u--; v--;
        wg[u].emplace_back(v, w);
        if(t == 0) wg[v].emplace_back(u, w);
    }

    auto ans = shortest_cycle(wg,
    (t==0) ? graph_direction::undirected : graph_direction::directed);
    if(ans == wg.INF) cout << -1 << endl;
    else cout << ans << endl;
    return 0;
}
0