[NC14700]追債之旅
阿新 • • 發佈:2021-08-07
一、題目
二、思路
這題因為有天數的限制,所以需要用到分層圖來做,以天數來作為層數,每層都連上前一天的這個城市和下個城市的路徑,然後直接背一下dijkstra的板子,最後在每層裡的n號城市找到最小值即可
三、程式碼
#include<bits/stdc++.h> using namespace std; const int N = 202000; typedef pair<int, int> PII; int w[N], e[N], ne[N], h[N], idx; int dist[N]; bool st[N]; int p[20]; struct node{ int a, b, c; }x[N]; void add(int x, int y, int z){ w[idx] = z; e[idx] = y; ne[idx] = h[x]; h[x] = idx ++; } void dijkstra(){ memset(dist, 0x3f, sizeof dist); dist[1] = 0; priority_queue<PII, vector<PII>, greater<PII> > heap; heap.push({1, 0}); while(heap.size()){ PII k = heap.top(); heap.pop(); int ver = k.first, distance = k.second; if(st[ver]) continue; st[ver] = true; for(int i = h[ver]; i != -1; i = ne[i]){ int j = e[i]; if(dist[j] > w[i] + distance){ dist[j] = w[i] + distance; heap.push({j, dist[j]}); } } } } int main(){ int n, m, k; memset(h, -1, sizeof h); cin >> n >> m >> k; int a, b, c; for(int i = 1; i <= m; i ++){ cin >> x[i].a >> x[i].b >> x[i].c; } for(int i = 1; i <= k; i ++){ cin >> p[i]; } for(int i = 1; i <= m; i ++){ for(int j = 0; j < k; j ++){ //連邊 add(x[i].a + j * n, x[i].b + (j + 1) * n, x[i].c + p[j + 1]); add(x[i].b + j * n, x[i].a + (j + 1) * n, x[i].c + p[j + 1]); } } dijkstra(); int minn = 1e9; for(int i = 1; i <= k + 1; i ++){ //找最小值 if(minn > dist[i * n]) minn = dist[i * n]; //cout << dist[n][i] << endl; } if(minn == 1e9) cout << "-1" << endl; else cout << minn << endl; return 0; }