結果
| 問題 | No.845 最長の切符 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-08-05 13:34:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 356 ms / 3,000 ms |
| コード長 | 1,956 bytes |
| 記録 | |
| コンパイル時間 | 860 ms |
| コンパイル使用メモリ | 79,224 KB |
| 最終ジャッジ日時 | 2025-01-12 14:53:21 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#include <iostream>
#include <vector>
template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }
template <class Cost = int>
struct Edge {
int src, dst;
Cost cost;
Edge(int src = -1, int dst = -1, Cost cost = 1)
: src(src), dst(dst), cost(cost){};
bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};
template <class Cost = int>
struct Graph {
std::vector<std::vector<Edge<Cost>>> graph;
Graph(int n = 0) : graph(n) {}
void span(bool direct, int src, int dst, Cost cost = 1) {
graph[src].emplace_back(src, dst, cost);
if (!direct) graph[dst].emplace_back(dst, src, cost);
}
int size() const { return graph.size(); }
void clear() { graph.clear(); }
void resize(int n) { graph.resize(n); }
std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};
void solve() {
int n, m;
std::cin >> n >> m;
Graph<int> graph(n);
while (m--) {
int u, v, c;
std::cin >> u >> v >> c;
graph.span(false, --u, --v, c);
}
auto dp = vec(n, vec(1 << n, -1));
for (int v = 0; v < n; ++v) {
dp[v][1 << v] = 0;
}
int ans = 0;
for (int b = 0; b < (1 << n); ++b) {
for (int v = 0; v < n; ++v) {
if (dp[v][b] == -1) continue;
ans = std::max(ans, dp[v][b]);
for (auto e : graph[v]) {
int u = e.dst;
if ((b >> u) & 1) continue;
int nd = dp[v][b] + e.cost;
int nb = b | (1 << u);
dp[u][nb] = std::max(dp[u][nb], nd);
}
}
}
std::cout << ans << "\n";
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
return 0;
}