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

Hint

题意

有n个物品,每个物品的价格是pi,现在你有m种交换方式,就是ai+bi可以换得一个ci

然后问你最便宜得到第一个物品的价钱是多少

题解:

跑最短路,m就相当于建了m条边。

一开始把所有点都压进队列然后跑spfa就好了

至于中途怎么买过来买过去的我不知道,反正暴力出来就是答案咯

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
int d[maxn];
struct edge{
int x,y;
edge() {}
edge(int X,int Y):x(X),y(Y){}
};
vector<edge>E[maxn];
int inq[maxn];
int main()
{
freopen("dwarf.in","r",stdin);
freopen("dwarf.out","w",stdout);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&d[i]);
for(int i=1;i<=m;i++)
{
int x,y,z;scanf("%d%d%d",&x,&y,&z);
E[y].push_back(edge(z,x));
E[z].push_back(edge(y,x));
}
queue<int> Q;
for(int i=1;i<=n;i++)
Q.push(i),inq[i]=1;
while(!Q.empty())
{
int now = Q.front();
Q.pop();
inq[now]=0;
for(int i=0;i<E[now].size();i++)
{
int v = E[now][i].x;
int u = E[now][i].y;
if(d[v]+d[now]<d[u])
{
d[u]=d[now]+d[v];
if(!inq[u])
inq[u]=1,Q.push(u);
}
}
}
cout<<d[1]<<endl;
}

Codeforces Gym 100269D Dwarf Tower spfa的更多相关文章

  1. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  2. Problem D. Dwarf Tower spfa

    http://codeforces.com/gym/100269/attachments 首先建图,然后图中每条边的权值是会变化的,是由dis[x] + dis[y]  --->   dis[m ...

  3. Codeforces Gym 100269A Arrangement of Contest 水题

    Problem A. Arrangement of Contest 题目连接: http://codeforces.com/gym/100269/attachments Description Lit ...

  4. noip模拟赛 dwarf tower

    [问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Vasya想得到编号为1的物品.获得一个物品有两种方式:1. ...

  5. dwarf tower

    dwarf tower(dwarf.cpp/c/pas)[问题描述]Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,它们的编号为1到n.现在Va ...

  6. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  7. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  8. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  9. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

随机推荐

  1. deepin安装metasploit

    [1]安装metasploit 1.curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/tem ...

  2. Python3 xml模块的增删改查

    xml数据示例 ? 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 <data>     < ...

  3. ADO POST时出现“无法为更新定位行,一些值可能已在最后一次读取后已更改”问题的解决方法

    原因有这样几种: 1.在数据库设计时,为某些字段设置了默认值,在修改进行提交以后,数据库会自动修改对应字段的所有行的默认值,从而导致了数据库与数据集中数据的不一致,使ADOQuery无法对数据集进行定 ...

  4. 《Java编程思想》阅读笔记一

    Java编程思想 这是一个通过对<Java编程思想>(Think in java)第四版进行阅读同时对java内容查漏补缺的系列.一些基础的知识不会被罗列出来,这里只会列出一些程序员经常会 ...

  5. 几条学习python的建议

    熟悉python语言, 以及学会python的编码方式. 熟悉python库, 遇到开发任务的时候知道如何去找对应的模块. 知道如何查找和获取第三方的python库, 以应付开发任务. 学习步骤 安装 ...

  6. jstorm系列-1:入门

    一.             Storm整体介绍 Storm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStorm系统,Jstorm将这 ...

  7. 关于指针pointer的位数与程序有关还是与系统有关、以及指针的指针的理解

  8. Sql 中取小数点后面两位小数

    ,),round(UnTaxAmount,))as UnTaxAmount from View_SaleVoice ,)) as UnTaxAmount from View_SaleVoice

  9. 你想了解的轮询、长轮询和websocket都在这里了

    日常生活中,有很多需要数据的实时更新,比如群聊信息的实时更新,还有投票系统的实时刷新等 实现的方式有很多种,比如轮询.长轮询.websocket 轮询 轮询是通过设置页面的刷新频率(设置多长时间自动刷 ...

  10. 【转载】Web Service 的工作原理

    http://www.cnblogs.com/Jessy/p/3528341.html Web Service基本概念 Web Service也叫XML Web Service WebService是 ...