結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー butsurizukibutsurizuki
提出日時 2022-03-13 03:53:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 549 ms / 2,000 ms
コード長 1,507 bytes
コンパイル時間 3,571 ms
コンパイル使用メモリ 263,344 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 15:08:18
合計ジャッジ時間 9,454 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 7 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 5 ms
4,348 KB
testcase_16 AC 11 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 5 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 6 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 52 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 10 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 61 ms
4,348 KB
testcase_34 AC 29 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 4 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 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 7 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 171 ms
4,348 KB
testcase_46 AC 7 ms
4,348 KB
testcase_47 AC 25 ms
4,348 KB
testcase_48 AC 5 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 548 ms
4,348 KB
testcase_55 AC 549 ms
4,348 KB
testcase_56 AC 547 ms
4,348 KB
testcase_57 AC 29 ms
4,348 KB
testcase_58 AC 30 ms
4,348 KB
testcase_59 AC 29 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;
using pl=pair<long long,long long>;
using Graph=vector<vector<long long>>;

long long shortest_cycle(long long n,long long m,vector<long long> &u,vector<long long> &v,vector<long long> &w,bool isdirected){
  long long res=8e18;
  Graph g(n);
  vector<pl> vp;
  for(int i=0;i<m;i++){
    g[u[i]].push_back(i);
    if(!isdirected){
      g[v[i]].push_back(i);
    }
    vp.push_back({w[i],i});
  }
  sort(vp.begin(),vp.end());

  for(int ec=0;ec<m;ec++){
    long long eid=vp[ec].second;
    if(w[eid]>=res){break;}
    priority_queue<pl,vector<pl>,greater<pl>> pq;
    pq.push({w[eid],v[eid]});
    vector<long long> d(n,8e18);
    d[v[eid]]=w[eid];
    while(!pq.empty()){
      pl od=pq.top();pq.pop();
      if(od.first>=res){break;}
      if(od.second==u[eid]){res=min(res,od.first);break;}
      for(auto &nx : g[od.second]){
        if(nx==eid){continue;}
        long long nv=(u[nx]^v[nx]^od.second);
        long long nd=od.first+w[nx];
        if(d[nv]>nd){
          d[nv]=nd;
          pq.push({nd,nv});
        }
      }
    }
  }
  if(res>7e18){res=-1;}
  return res;
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  bool isdirected;
  if(t==0){isdirected=false;}
  else{isdirected=true;}
  long long n,m;
  cin >> n >> m;
  vector<long long> u(m),v(m),w(m);
  for(int i=0;i<m;i++){
    cin >> u[i] >> v[i] >> w[i];
    u[i]--;v[i]--;
  }
  cout << shortest_cycle(n,m,u,v,w,isdirected) << '\n';
  return 0;
}
0