結果
| 問題 |
No.1320 Two Type Min Cost Cycle
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2020-12-17 02:42:15 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 252 ms / 2,000 ms |
| コード長 | 2,277 bytes |
| コンパイル時間 | 994 ms |
| コンパイル使用メモリ | 99,572 KB |
| 最終ジャッジ日時 | 2025-01-17 02:19:21 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 57 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
const ll INF = 1e+15;
typedef pair<ll, int> P;
typedef pair<int, int> Pii;
struct edge{
int to;
ll cost;
};
int t, n, m;
vector<edge> G[2000];
ll d[2000];
int v[2000], u[2000];
ll w[2000];
ll ans = INF;
void dijkstra(int s){
priority_queue<P, vector<P>, greater<P>> que;
fill(d, d+n, INF);
d[s] = 0;
que.push(P(0, s));
while(!que.empty()){
P p = que.top();que.pop();
int v = p.second;
if(d[v] < p.first) continue;
for(edge e : G[v]){
if(d[v] + e.cost < d[e.to]){
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
if(t == 1 && e.to == s){
chmin(ans, d[v]+e.cost);
}
}
}
}
void solve_di(){
for(int i = 0; i < n; i++){
dijkstra(i);
}
if(ans == INF){
cout << -1 << endl;
}else{
cout << ans << endl;
}
}
void solve_undi(){
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++) G[j].clear();
for(int j = 0; j < m; j++) {
if(i == j) continue;
G[u[j]].push_back((edge){v[j], w[j]});
G[v[j]].push_back((edge){u[j], w[j]});
}
dijkstra(v[i]);
chmin(ans, d[u[i]]+w[i]);
}
if(ans == INF){
cout << -1 << endl;
}else{
cout << ans << endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> t >> n >> m;
for(int i = 0; i < m; i++){
cin >> u[i] >> v[i] >> w[i];
u[i]--; v[i]--;
if(t == 1)G[u[i]].push_back((edge){v[i], w[i]});
}
if(t == 0) solve_undi();
else solve_di();
}
milanis48663220