Codeforces Gym 100269 Dwarf Tower (最短路)
题目连接:
http://codeforces.com/gym/100269/attachments
Description
Little Vasya is playing a new game named “Dwarf Tower”. In this game there are n different items,
which you can put on your dwarf character. Items are numbered from 1 to n. Vasya wants to get the
item with number 1.
There are two ways to obtain an item:
• You can buy an item. The i-th item costs ci money.
• You can craft an item. This game supports only m types of crafting. To craft an item, you give
two particular different items and get another one as a result.
Help Vasya to spend the least amount of money to get the item number 1.
Input
The first line of input contains two integers n and m (1 ≤ n ≤ 10 000; 0 ≤ m ≤ 100 000) — the number
of different items and the number of crafting types.
The second line contains n integers ci — values of the items (0 ≤ ci ≤ 109
).
The following m lines describe crafting types, each line contains three distinct integers ai, xi, yi — ai is the item that can be crafted from items xi and yi (1 ≤ ai , xi , yi ≤ n; ai ̸= xi ; xi ̸= yi ; yi ̸= ai).
Output
The output should contain a single integer — the least amount of money to spend.
Sample Input
5 3
5 0 1 2 5
5 2 3
4 2 3
1 4 5
Sample Output
2
题意:
对与一个物品,你可以选择购买获得,但是要花费ci , 或者是通过 xi yi 合成。
要你用最小的花费得到物品1.
题解:
我们对于每一个物品都应该花最小的花费的到。 a可以通过x, y合成。那么从x去到a的费用就是 c[y] 。(因为你已经跑到了x点,表示你已经有了x了)。
在建一个大原点 0, 0到每个点的费用为c[i] 。然后用0这里跑一次Dij 。这样你就可以得到了获得物品i 的最小花费。
在跑一下m次合成,找到最小的花费。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 #include <stack> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 typedef unsigned long long uLL; 15 #define ms(a, b) memset(a, b, sizeof(a)) 16 #define rep(a, b) for(int a = 0;a<b;a++) 17 #define rep1(a, b) for(int a = 1;a<=b;a++) 18 #define pb push_back 19 #define mp make_pair 20 #define eps 0.0000000001 21 #define IOS ios::sync_with_stdio(0);cin.tie(0); 22 const LL INF = 0x3f3f3f3f3f3f3f3f; 23 const int inf = 0x3f3f3f3f; 24 const int mod = 1e9+7; 25 const int maxn = 10000+10; 26 int c[maxn]; 27 struct qnode { 28 int v; 29 LL c; 30 qnode(int _v=0, LL _c =0):v(_v), c(_c) {} 31 bool operator < (const qnode &r) const { 32 return c > r.c; 33 } 34 }; 35 struct Edge { 36 int v, cost; 37 Edge(int _v=0, int _cost =0):v(_v), cost(_cost) {} 38 }; 39 vector <Edge> E[10*maxn]; 40 bool vis[maxn]; 41 LL dist[maxn]; 42 void Dij(int n, int start) { 43 memset(vis,false,sizeof(vis)); 44 for(int i=1; i<=n; i++)dist[i]=INF; 45 priority_queue<qnode>que; 46 while(!que.empty())que.pop(); 47 dist[start]=0; 48 que.push(qnode(start,0)); 49 qnode tmp; 50 while(!que.empty()) { 51 tmp=que.top(); 52 que.pop(); 53 int u=tmp.v; 54 if(vis[u])continue; 55 vis[u]=true; 56 for(int i=0; i<E[u].size(); i++) { 57 int v=E[tmp.v][i].v; 58 int cost=E[u][i].cost; 59 if(!vis[v]&&dist[v]>dist[u]+cost) { 60 dist[v]=dist[u]+cost; 61 que.push(qnode(v,dist[v])); 62 } 63 } 64 } 65 } 66 void addedge(int u, int v, int w) { 67 E[u].pb(Edge(v, w)); 68 } 69 vector<pair<int, int> > One; 70 void solve() { 71 int n, m, x, a, b; 72 scanf("%d%d", &n, &m); 73 for(int i = 1; i<=n; i++) { 74 scanf("%d", &c[i]); 75 addedge(0, i, c[i]);//0指向物品i,表示直接购买的花费 76 } 77 for(int i = 1; i<=m; i++) { 78 scanf("%d%d%d", &x, &a, &b); 79 addedge(a, x, c[b]);//a指向物品x,表示需要在花费c[b]的花费就可以合成x 80 addedge(b, x, c[a]); 81 if(x==1){//记录一下物品1的合成 82 One.pb(mp(a, b)); 83 } 84 } 85 Dij(n, 0); 86 LL ans = dist[1];//得到1的花费 87 for(int i = 0;i<One.size();i++){ 88 ans = min(ans, dist[One[i].first]+dist[One[i].second]);//和通过合成的花费比较 89 } 90 printf("%lld\n", ans); 91 } 92 int main() { 93 #ifdef LOCAL 94 freopen("input.txt", "r", stdin); 95 // freopen("output.txt", "w", stdout); 96 #endif 97 freopen("dwarf.in", "r", stdin); 98 freopen("dwarf.out", "w", stdout); 99 solve(); 100 return 0; 101 }
View Code
转载于:https://www.cnblogs.com/denghaiquan/p/7271307.html
Codeforces Gym 100269 Dwarf Tower (最短路)相关推荐
- Codeforces Gym 100269G Garage 数学
Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...
- dwarf tower
[问题描述] Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品, 它们的编号为1到n.现在Vasya想得到编号为1的物品. 获得一个物品有两种方式 ...
- Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks
Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...
- Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)
Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...
- [Codeforces Gym 101651/100725B] Banal Tickets
Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...
- Codeforces Gym 101630J Travelling from Petersburg to Moscow (最短路)
题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...
- Codeforces Gym 101630J Journey from Petersburg to Moscow (最短路)
题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...
- 【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)
题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...
- 【最短路】NEERC15 F Froggy Ford (Codeforces GYM 100851)
题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...
最新文章
- 12月21 vs2012 数据类型
- 【实验】小型网络WLAN架构实战案例
- oracle tax 中国税,oracle_TAX_税基础设置操作手册.doc
- 推荐一个github上万star的机器学习资料整理贴
- JNI中的内存管理(转)
- 计算机网络-VRRP
- pythonlambda回调函数_Python中如何借助lambda来给回调函数传参
- python random函数_python随机模块random的22种函数(小结)
- linux shell 命令批量杀死进程
- windows跳转端口
- 浅析ERP系统--研发
- html如何根据颜色排序,Excel技巧:按颜色排序或筛选
- mysql文本类型_mysql里存大量文本的数据类型是text吗?请详细说明一下
- 亿晟科技人脸识别门禁系统方案整体解决办法
- ES选举:Elasticsearch中Master选举完全解读
- 数据库为啥查询那么慢?
- houdini之blast
- 高盛AI生态报告:美国仍是主导,中国正高速成长
- 可喜可贺,又一ThinkPHP 5.1开源多用户商城系统上架了商家客户端
- Dalvik虚拟机详解(上)
热门文章
- php 浅复制 和 深复制(clone)
- Matlab中计算程序运行时间的三种方法
- HTML的BODY内标签介绍
- 16 BasicHashTable基本哈希表类(三)——Live555源码阅读(一)基本组件类
- C语言函数sscanf:从一个字符串中读进与指定格式相符的数据
- [转载]内存分配 知识,全局,局部,静态变量
- 数据科学和机器学习中使用的最多的20个R语言包
- hbase查看表结构_HBase
- css面试基础知识,CSS知识点与面试题解析
- 修改class文件_VM实战(六) - 通过案例深入学习class文件结构原理