結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー umezoumezo
提出日時 2020-12-17 00:58:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 2,300 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 216,244 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 10:11:49
合計ジャッジ時間 7,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 398 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 256 ms
4,348 KB
testcase_12 AC 22 ms
4,348 KB
testcase_13 AC 100 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 15 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 67 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 388 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 75 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 14 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 172 ms
4,348 KB
testcase_34 AC 77 ms
4,348 KB
testcase_35 AC 9 ms
4,348 KB
testcase_36 AC 5 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 18 ms
4,348 KB
testcase_39 AC 5 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 6 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 312 ms
4,348 KB
testcase_46 AC 11 ms
4,348 KB
testcase_47 AC 70 ms
4,348 KB
testcase_48 AC 70 ms
4,348 KB
testcase_49 AC 8 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 25 ms
4,348 KB
testcase_53 AC 12 ms
4,348 KB
testcase_54 AC 110 ms
4,348 KB
testcase_55 AC 109 ms
4,348 KB
testcase_56 AC 109 ms
4,348 KB
testcase_57 AC 323 ms
4,348 KB
testcase_58 AC 325 ms
4,348 KB
testcase_59 AC 327 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<ll,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

int main(){
  int t,n,m;
  cin>>t>>n>>m;
  
  Graph G(n);

  if(t==0){
    rep(i,m){
      int u,v,w;
      cin>>u>>v>>w;
      u--,v--;
      G[u].push_back(Edge(v,w));
      G[v].push_back(Edge(u,w));
    }
    
    ll mi=INF;
    for(int i=0;i<G.size();i++){
      for(int j=0;j<G[i].size();j++){
        Edge e=G[i][j];
        G[i].erase(G[i].begin()+j);
        vector<ll> dist(n,INF);
        dist[i]=0;
  
        priority_queue<pli,vector<pli>,greater<pli>> que;
        que.push({dist[i],i});
  
        while(!que.empty()){
          int v=que.top().second;
          ll d=que.top().first;
          que.pop();
    
          if(d>dist[v]) continue;
    
          for(auto e:G[v]){
            if(chmin(dist[e.to],dist[v]+e.w)){
             que.push({dist[e.to],e.to});
            }
          }
        }
        
        mi=min(mi,dist[e.to]+e.w);
        G[i].insert(G[i].begin()+j,e);
      }
    }
    
    if(mi==INF) cout<<-1<<endl;
    else cout<<mi<<endl;
  }
  else{
    rep(i,m){
      int u,v,w;
      cin>>u>>v>>w;
      u--,v--;
      G[u].push_back(Edge(v,w));
    }
    
    ll mi=INF;
    for(int i=0;i<G.size();i++){
      for(int j=0;j<G[i].size();j++){
        Edge e=G[i][j];
        G[i].erase(G[i].begin()+j);
        vector<ll> dist(n,INF);
        dist[e.to]=0;
  
        priority_queue<pli,vector<pli>,greater<pli>> que;
        que.push({dist[e.to],e.to});
  
        while(!que.empty()){
          int v=que.top().second;
          ll d=que.top().first;
          que.pop();
    
          if(d>dist[v]) continue;
    
          for(auto e:G[v]){
            if(chmin(dist[e.to],dist[v]+e.w)){
              que.push({dist[e.to],e.to});
            }
          }
        }
        
        mi=min(mi,dist[i]+e.w);
        G[i].insert(G[i].begin()+j,e);
      }
    }
    
    if(mi==INF) cout<<-1<<endl;
    else cout<<mi<<endl;
  }

  return 0;
}
0