追债之旅(Dijkstra最短路)
生活随笔
收集整理的這篇文章主要介紹了
追债之旅(Dijkstra最短路)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
追債之旅
思路
最短路問題,考慮DijkstraDijkstraDijkstra,用一個二維dis[i][j]dis[i][j]dis[i][j]數組,表示第iii天到達jjj號點的最小花費,disdisdis數組的更新方式改為if(dis[day][to]>dis[day?1][now]+value[to]+cost[day])if(dis[day][to] > dis[day - 1][now] + value[to] + cost[day])if(dis[day][to]>dis[day?1][now]+value[to]+cost[day])則更新disdisdis數組,所以我們最后只要遍歷iii天到達nnn號節點,也就是dis[i][n]dis[i][n]dis[i][n]數組,最后取其最小值就行。
DijkstraDijkstraDijkstra的關鍵就是一個有能夠記錄day,value,posday, value, posday,value,pos當前天數,這個狀態的最小值,當前位置,這樣的結構體,然后重載一下小于號運算符就可以跑個DijkstraDijkstraDijkstra板子了。
代碼
/*Author : lifehappy */ #pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h>#define mp make_pair #define pb push_back #define endl '\n' #define mid (l + r >> 1) #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r #define ls rt << 1 #define rs rt << 1 | 1using namespace std;typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii;const double pi = acos(-1.0); const double eps = 1e-7; const int inf = 0x3f3f3f3f;inline ll read() {ll x = 0, f = 1; char c = getchar();while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return x * f; }const int N1 = 1e3 + 10, N2 = 2e4 + 10;int head[N1], to[N2], nex[N2], value[N2], cnt = 1; int visit[20][N1], dis[20][N1], cost[20], n, m, k;struct Node {int day, pos, value;Node(int _day = 0, int _pos = 0, int _value = 0) : day(_day), pos(_pos), value(_value) {}bool operator < (const Node & t) const {return value > t.value;} };void add(int x, int y, int w) {to[cnt] = y;nex[cnt] = head[x];value[cnt] = w;head[x] = cnt++; }void Dijkstra() {for(int i = 0; i <= k; i++)for(int j = 0; j <= n; j++)dis[i][j] = inf;priority_queue<Node> q;q.push(Node(0, 1, 0));dis[0][1] = 0;while(!q.empty()) {Node temp = q.top();q.pop();if(visit[temp.day][temp.pos]) continue;visit[temp.day][temp.pos];int u = temp.pos, day = temp.day, w = temp.value;for(int i = head[u]; i; i = nex[i]) {if(day + 1 > k) continue;if(dis[day + 1][to[i]] > w + value[i] + cost[day + 1]) {dis[day + 1][to[i]] = w + value[i] + cost[day + 1];q.push(Node(day + 1, to[i], dis[day + 1][to[i]]));}}} }int main () {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);n = read(), m = read(), k = read();for(int i = 1; i <= m; i++) {int x = read(), y = read(), w = read();add(x, y, w);add(y, x, w);}for(int i = 1; i <= k; i++)cost[i] = read();Dijkstra();int ans = inf;for(int i = 1; i <= k; i++)ans = min(ans, dis[i][n]);printf("%d\n", ans == inf ? -1 : ans);return 0; }總結
以上是生活随笔為你收集整理的追债之旅(Dijkstra最短路)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天天大便有助于减肥吗
- 下一篇: 一个月减肥多少斤比较合理