結果
問題 |
No.1 道のショートカット
|
ユーザー |
|
提出日時 | 2016-10-09 23:56:03 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 2,219 bytes |
コンパイル時間 | 1,824 ms |
コンパイル使用メモリ | 178,540 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-20 16:27:06 |
合計ジャッジ時間 | 2,987 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> using namespace std; struct Node { int at; int time; int cost; Node(int at, int time, int cost) : at(at), time(time), cost(cost) {} bool operator>(const Node &s) const { if (time != s.time) return time > s.time; if (cost != s.cost) return cost > s.cost; return at > s.at; } }; struct Edge { int to; int time; int cost; Edge(int to, int time, int cost) : to(to), time(time), cost(cost) {} }; typedef vector<vector<Edge> > AdjList; //隣接リスト typedef vector<Edge>::iterator Edge_it; const int INF = 100000000; const int NONE = -1; AdjList graph; int c; void dijkstra(int s, int t, vector<int> &mint){ priority_queue<Node, vector<Node>, greater<Node> > pq; pq.push(Node(s, 0, 0)); while(!pq.empty()) { Node x = pq.top(); pq.pop(); if (x.cost > c) continue; mint[x.at] = min(mint[x.at], x.time); if (x.at == t) break; for(Edge_it i = graph[x.at].begin(), e = graph[x.at].end(); i != e; ++i) { int tmp_t = x.time + i->time; int tmp_c = x.cost + i->cost; if (tmp_c <= c) { pq.push(Node(i->to, tmp_t, tmp_c)); } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; int v; cin >> n >> c >> v; vector<int> s(v), t(v), y(v), m(v); for (int i = 0; i < v; i++) cin >> s[i]; for (int i = 0; i < v; i++) cin >> t[i]; for (int i = 0; i < v; i++) cin >> y[i]; for (int i = 0; i < v; i++) cin >> m[i]; graph = AdjList(n); for (int i = 0; i < v; i++) { s[i]--; t[i]--; graph[s[i]].emplace_back(t[i], m[i], y[i]); } int dp[50][301] = {}; for (int i = 1; i < n; i++) { for (int j = 0; j <= c; j++) { dp[i][j] = INF; } } for (int i = 0; i < n; i++) { for (auto it = graph[i].begin(), e = graph[i].end(); it != e; ++it) { for (int j = it->cost; j <= c; j++) { dp[it->to][j] = min(dp[it->to][j], dp[i][j - it->cost] + it->time); } } } cout << (dp[n - 1][c] == INF ? -1 : dp[n - 1][c]) << endl; return 0; }