結果
| 問題 | No.1320 Two Type Min Cost Cycle |
| コンテスト | |
| ユーザー |
umezo
|
| 提出日時 | 2020-12-17 00:58:43 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 286 ms / 2,000 ms |
| コード長 | 2,300 bytes |
| 記録 | |
| コンパイル時間 | 1,547 ms |
| コンパイル使用メモリ | 224,796 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-06-16 01:15:49 |
| 合計ジャッジ時間 | 5,016 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
}
umezo