結果
問題 | No.1320 Two Type Min Cost Cycle |
ユーザー | umezo |
提出日時 | 2020-12-17 00:58:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 409 ms / 2,000 ms |
コード長 | 2,300 bytes |
コンパイル時間 | 2,289 ms |
コンパイル使用メモリ | 206,968 KB |
最終ジャッジ日時 | 2025-01-17 02:13:25 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 57 |
ソースコード
#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; }