結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
小指が強い人
|
| 提出日時 | 2016-01-04 23:13:02 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,853 bytes |
| コンパイル時間 | 880 ms |
| コンパイル使用メモリ | 83,308 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-19 11:18:14 |
| 合計ジャッジ時間 | 2,155 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | WA * 27 |
コンパイルメッセージ
main.cpp:16:20: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘9223372036854775807’ to ‘-1’ [-Woverflow]
16 | int cost = LONG_MAX;
| ^~~~~~~~
main.cpp: In function ‘int min_cost(int, int)’:
main.cpp:26:24: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘9223372036854775807’ to ‘-1’ [-Woverflow]
26 | root[i].cost = LONG_MAX;
| ^~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:68:15: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘9223372036854775807’ to ‘-1’ [-Woverflow]
68 | int res = LONG_MAX;
| ^~~~~~~~
ソースコード
#include <iostream>
#include <queue>
#include <unordered_map>
#include <climits>
using namespace std;
struct Map1
{
int id;
int cost;
bool operator<(const Map1& m) const {
return cost > m.cost;
}
};
struct Map2
{
int cost = LONG_MAX;
int prev = -1;
};
int s[50] = {0};
unordered_map<int, unordered_map<int, int>> cost;
int n, m;
int min_cost(int start, int end) {
Map2 root[50];
for(int i = 0; i < n; i++) {
root[i].cost = LONG_MAX;
root[i].prev = -1;
}
priority_queue<Map1> q;
root[start].cost = 0;
for(auto c : cost[start]) {
Map1 m = {c.first, c.second};
root[c.first].prev = start;
root[c.first].cost = c.second;
q.push(m);
}
while(!q.empty()) {
int cur = q.top().id;
if(cur == end)
return root[end].cost;
q.pop();
for(auto c : cost[cur]) {
int d = root[cur].cost + c.second;
if(d < root[c.first].cost) {
if(root[c.first].cost >= LONG_MAX) {
Map1 m = {c.first, d};
q.push(m);
}
root[c.first].prev = cur;
root[c.first].cost = d;
}
}
}
return root[end].cost;
}
int main(void)
{
cin >> n;
for(int i = 0; i < n; i++)
cin >> s[i];
cin >> m;
for(int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
cost[b][a] = cost[a][b] = c;
}
int res = LONG_MAX;
for(int i = 1; i < n - 1; i++)
for(int j = 1; j < n - 1; j++) {
if(i == j) continue;
int t = 0;
t += min_cost(0, i) + s[i];
t += min_cost(i, j) + s[j];
t += min_cost(j, n - 1);
if(t < res)
res = t;
}
cout << res << endl;
return 0;
}
小指が強い人