結果

問題 No.1344 Typical Shortest Path Sum
ユーザー Sooh317
提出日時 2021-01-16 13:31:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,276 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 205,236 KB
最終ジャッジ日時 2025-01-17 22:07:20
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 72 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#pragma region atcoder
//#include <atcoder/modint>
//using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#pragma endregion
#pragma region debug for var, v, vv
#define debug(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){cerr << endl;int cnt = 0;for(const auto& v : vv){cerr << cnt << "th : "; view(v); cnt++;} cerr << endl;}
#pragma endregion
using ll = long long;
const ll mod = 1000000007;
const int inf = 1001001001;
const ll INF = 1001001001001001001ll; 
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
template<class T, class K>bool chmax(T &a, const K b) { if (a<b) { a=b; return 1; } return 0; }
template<class T, class K>bool chmin(T &a, const K b) { if (b<a) { a=b; return 1; } return 0; }
ll rudiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } //  20 / 3 == 7
ll rddiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // -20 / 3 == -7
ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;}
ll modpow(ll a, ll p, ll m){ll ret = 1; while(p){if(p & 1){ret = ret * a % m;} a = a * a % m; p >>= 1;} return ret;}
/*---------------------------------------------------------------------------------------------------------------------------------*/
struct Graph{
    struct Edge{
        int to; long long cost;
        Edge(int to, long long cost) : to(to), cost(cost) {}
    };
    ll num;
    vector<vector<Edge>> G;
    vector<ll> dist;
    // constructor for initialization
    Graph(int n) : num(n){
        G.resize(n), dist.resize(n);
    }
    // assembling a graph whose edge is coming from s to t
    void add_edge(ll s, ll t, ll cost, bool directed){
        G[s].emplace_back(t, cost);
        if(!directed) G[t].emplace_back(s, cost);
    }
    // dijkstra algorithm
    ll dijkstra(int s, int g){
        fill(dist.begin(), dist.end(), INF);
        using P = pair<ll, int>;
        priority_queue<P, vector<P>, greater<P>> que;
        que.push({dist[s] = 0, s});
        while(!que.empty()){
            P p = que.top(); que.pop();
            int v = p.second;
            if(dist[v] < p.first) continue;
            for(auto e : G[v]){
                if(chmin(dist[e.to], dist[v] + e.cost)) que.push({dist[e.to], e.to});
            }
        }
        return dist[g];
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(20);
    int n, m; cin >> n >> m;
    Graph g(n);
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;
        a--, --b;
        ll d; cin >> d;
        g.add_edge(a, b, d, true);
    }

    for(int i = 0; i < n; i++){
        ll ans = 0;
        g.dijkstra(i, (i + 1) % n);
        for(int j = 0; j < n; j++){
            if(i == j) continue;
            ans += (g.dist[j] == INF ? 0 : g.dist[j]);
        }
        cout << ans << endl;
    }
}       
/*
   * review you code when you get WA (typo? index?)
   * int overflow, array bounds
   * special cases (n=1?)
*/
0