[NC14700]追债之旅
作者:互联网
一、题目
二、思路
这题因为有天数的限制,所以需要用到分层图来做,以天数来作为层数,每层都连上前一天的这个城市和下个城市的路径,然后直接背一下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;
}
标签:dist,NC14700,之旅,int,追债,idx,heap,ver,st 来源: https://www.cnblogs.com/ZIENxc/p/15112364.html