链接:https://ac.nowcoder.com/acm/contest/6927/A
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
题目描述
小明现在要追讨一笔债务,已知有n座城市,每个城市都有编号,城市与城市之间存在道路相连(每条道路都是双向的),经过任意一条道路需要支付费用。小明一开始位于编号为1的城市,欠债人位于编号为n的城市。小明每次从一个城市到达另一个城市需要耗时1天,而欠债人每天都会挥霍一定的钱,等到第k天后(即第k+1天)他就会离开城n并再也找不到了。小明必须要在他离开前抓到他(最开始为第0天),同时希望自己的行程花费和欠债人挥霍的钱的总和最小,你能帮他计算一下最小总和吗?
输入描述:
第1行输入三个整数n,m,k,代表城市数量,道路数量和指定天数
第2-m+1行,每行输入三个整数u,v,w,代表起点城市,终点城市和支付费用。(数据保证无重边,自环)
第m+2行输入k个整数,第i个整数ai代表第i天欠债人会挥霍的钱。
数据保证:0<n≤1000,0<m≤10000,0<k≤10,1≤u,v≤n,0<w,ai≤1000
输出描述:
复制代码
1输出一行,一个整数,代表小明的行程花费和欠债人挥霍的钱的最小总和,如果小明不能抓住欠债人(即不能在第k天及之前到达城n),则输出-1。
示例1
输入
复制代码
1
2
3
4
53 3 2 1 3 10 1 2 2 2 3 2 3 7
输出
复制代码
113
说明
复制代码
1小明从1-3,总共费用=10(行程)+3(挥霍费用)=13,是方案中最小的(另一条方案花费14)。
示例2
输入
复制代码
1
2
3
43 2 1 1 2 3 2 3 3 10
输出
复制代码
1-1
说明
复制代码
1小明无法在第1天赶到城3,所以输出-1。
思路:堆优化版迪杰斯特拉+定长最短路。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86#include<iostream> #include<algorithm> #include<set> #include<vector> #include<cstring> #include<queue> #define ll long long using namespace std; const int N = 10000+10; int idx,h[N],e[N*2],ne[N*2],w[N*2]; int n,m,k; int cost[N]; int dis[1010][20]; int ans = 0x3f3f3f3f; void add(int a, int b, int c) { e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx++; } struct node{ int u,len,step; bool operator < (const node &tem)const { return len < tem.len; } }; void dij() { dis[1][0] = 0; priority_queue<node> q; q.push({1,0,0}); while(q.size()) { node a = q.top(); q.pop(); int u = a.u; int len = a.len; int step = a.step; if(step >= k || u == n || len > dis[u][step]) continue; for(int i=h[u]; i!=-1; i=ne[i]) { int v = e[i]; int ww = w[i]; if(dis[v][step+1] > dis[u][step] + ww + cost[step+1]) { dis[v][step+1] = dis[u][step] + ww + cost[step+1]; q.push({v, dis[v][step+1], step+1}); } } } for(int i=1; i<=k; i++) ans = min(ans, dis[n][i]); } int main() { memset(dis, 0x3f, sizeof dis); memset(h, -1, sizeof h); cin>>n>>m>>k; while(m--) { int a,b,c; cin>>a>>b>>c; add(a,b,c); add(b,a,c); } for(int i=1; i<=k; i++) cin>>cost[i]; dij(); if(ans == 0x3f3f3f3f) puts("-1"); else cout<<ans; return 0; }
最后
以上就是务实大门最近收集整理的关于追债之旅的全部内容,更多相关追债之旅内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复