結果
| 問題 | No.3087 University Coloring |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-13 10:01:42 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 339 ms / 2,000 ms |
| コード長 | 1,375 bytes |
| 記録 | |
| コンパイル時間 | 1,125 ms |
| コンパイル使用メモリ | 114,916 KB |
| 実行使用メモリ | 24,560 KB |
| 最終ジャッジ日時 | 2025-04-04 20:33:42 |
| 合計ジャッジ時間 | 10,945 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
//プリム法による解法
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
using ll = long long;
int main(){
int N,M;
cin >> N >> M;
vector edge(N,vector<pair<int,ll>> (0));
for(int i = 0; i < M; i++){
int a,b;
ll c;
cin >> a >> b >> c;
a--,b--;
edge[a].push_back({b,c});
edge[b].push_back({a,c});
}
//チェック済み広場から1つの通路で移動可能な未チェック広場のリスト
//(その通る通路の長さも情報としてもち、常にその長さで降順ソート)
priority_queue<pair<ll,int>> que;
que.push({0,0});
vector<bool> checked(N,0);//チェック済みかどうか
ll ans = 0;
while(que.size()){
ll nowc,here;
tie(nowc,here) = que.top();
que.pop();
if(checked[here])continue;//すでにチェック済みなら何もしない
checked[here] = 1;
ans += nowc;
//新しくチェック済み広場が増えたので、そこから移動可能な広場を探索する
for(auto j : edge[here]){
ll to,toc;
tie(to,toc) = j;
if(!checked[to])que.push({toc,to});
}
}
cout << ans * 2 << endl;//答えは最大全域木の長さの総和の2倍である点に注意
return 0;
}