HNU 12847 Dwarf Tower(最短路+队列优化)

本文介绍了一种利用最短路径算法解决特定问题的方法。通过将问题转化为图上的最短路径问题,使用邻接表存储构造方法,并借助队列进行节点更新,实现了物品价值最小化的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847

解题报告:有n样物品,编号从1到n第i样物品可以通过金币vi买到,同时有m种方法,方法的内容是由两种物品可以构造出另一种物品,现在要你求出得到1物品的价值最小是多少?

当成最短路来解,用邻接表存好m种构造方法,然后用队列里面的点去尝试通过构造的方法使得得到i物品所花的价值更小,如果更新成功,再把更新成功的那个点又加入到队列中。

同时要标记一下这个点是不是正在队列中,如果是的话就不要重复加了。直到队列为空结束。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<cmath>
 7 #include<deque>
 8 using namespace std;
 9 const int maxn = 10000+5;
10 struct node
11 {
12     int a,b;
13 };
14 int w[maxn],visit[maxn];
15 vector<node> vt[maxn];
16 vector<node>::iterator iter;
17 deque<int> de;
18 int main()
19 {
20     int n,m;
21     while(scanf("%d%d",&n,&m)!=EOF)
22     {
23         de.clear();
24         for(int i = 1;i <= n;++i)
25         vt[i].clear();
26         memset(visit,0,sizeof(visit));
27         for(int i = 1;i <= n;++i)
28         {
29             scanf("%d",&w[i]);
30             de.push_back(i);
31             visit[i] = 1;
32         }
33         int a,b,c;
34         while(m--)
35         {
36             scanf("%d%d%d",&a,&b,&c);
37             node temp = {c,a};
38             vt[b].push_back(temp);
39             temp.a = b;
40             temp.b = a;
41             vt[c].push_back(temp);
42         }
43         while(!de.empty())
44         {
45             int ss = *de.begin();
46             de.pop_front();
47             visit[ss] = 0;
48             iter = vt[ss].begin();
49             for(;iter != vt[ss].end();++iter)
50             if(w[ss] + w[iter->a] < w[iter->b])
51             {
52                 w[iter->b] = w[ss] + w[iter->a];
53                 if(visit[iter->b] == 0)
54                 {
55                     visit[iter->b] = 1;
56                     de.push_back(iter->b);
57                 }
58             }
59         }
60         printf("%d\n",w[1]);
61     }
62     return 0;
63 }
View Code

 

转载于:https://www.cnblogs.com/xiaxiaosheng/p/3873631.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值