题目连接:

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次合成,找到最小的花费。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define rep(a, b) for(int a = 0;a<b;a++)
#define rep1(a, b) for(int a = 1;a<=b;a++)
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
int c[maxn];
struct qnode {
int v;
LL c;
qnode(int _v=, LL _c =):v(_v), c(_c) {}
bool operator < (const qnode &r) const {
return c > r.c;
}
};
struct Edge {
int v, cost;
Edge(int _v=, int _cost =):v(_v), cost(_cost) {}
};
vector <Edge> E[*maxn];
bool vis[maxn];
LL dist[maxn];
void Dij(int n, int start) {
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty()) {
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=; i<E[u].size(); i++) {
int v=E[tmp.v][i].v;
int cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost) {
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
}
}
void addedge(int u, int v, int w) {
E[u].pb(Edge(v, w));
}
vector<pair<int, int> > One;
void solve() {
int n, m, x, a, b;
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++) {
scanf("%d", &c[i]);
addedge(, i, c[i]);//0指向物品i,表示直接购买的花费
}
for(int i = ; i<=m; i++) {
scanf("%d%d%d", &x, &a, &b);
addedge(a, x, c[b]);//a指向物品x,表示需要在花费c[b]的花费就可以合成x
addedge(b, x, c[a]);
if(x==){//记录一下物品1的合成
One.pb(mp(a, b));
}
}
Dij(n, );
LL ans = dist[];//得到1的花费
for(int i = ;i<One.size();i++){
ans = min(ans, dist[One[i].first]+dist[One[i].second]);//和通过合成的花费比较
}
printf("%lld\n", ans);
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
freopen("dwarf.in", "r", stdin);
freopen("dwarf.out", "w", stdout);
solve();
return ;
}

Codeforces Gym 100269 Dwarf Tower (最短路)的更多相关文章

  1. Codeforces Gym 100269D Dwarf Tower spfa

    Dwarf Tower 题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a ...

  2. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  3. Codeforces Gym 100269A Arrangement of Contest 水题

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

  4. Problem D. Dwarf Tower spfa

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

  5. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  6. Codeforces Gym 100269K Kids in a Friendly Class 构造题

    Kids in a Friendly Class 题目连接: http://codeforces.com/gym/100269/attachments Description Kevin resemb ...

  7. Codeforces Gym 100269G Garage 数学

    Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...

  8. Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp

    Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...

  9. Codeforces Gym 100269B Ballot Analyzing Device 模拟题

    Ballot Analyzing Device 题目连接: http://codeforces.com/gym/100269/attachments Description Election comm ...

随机推荐

  1. net 架构师-数据库-sql server-003-T-SQL 基本语句

    3.1 基本SELECT语句 SELECT [ALL|DISTINCT] [TOP (<expression>)  [PERCENT] [WITH TIES]] <coloumn  ...

  2. springboot 整合 tobato 的 fastdfs 实现文件上传和下载

    添加项目所需要的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...

  3. B - 卿学姐与基本法 (离散化+成段更新+区间求和)

    卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  4. HNUSTOJ-1258 Time

    1258: Time 时间限制: 1 Sec  内存限制: 128 MB提交: 16  解决: 11[提交][状态][讨论版] 题目描述 Digital clock use 4 digits to e ...

  5. HNUSTOJ-1636 心电图

    1636: 心电图 时间限制: 1 Sec  内存限制: 128 MB提交: 583  解决: 231[提交][状态][讨论版] 题目描述 众所周知,ACM/ICPC实验室聚集了一堆学霸Orz 有学霸 ...

  6. P4556 [Vani有约会]雨天的尾巴(线段树合并+lca)

    P4556 [Vani有约会]雨天的尾巴 每个操作拆成4个进行树上差分,动态开点线段树维护每个点的操作. 离线处理完向上合并就好了 luogu倍增lca被卡了5分.....于是用rmq维护.... 常 ...

  7. application session 实现简单的在线聊天人数的统计

    写了快一年的asp.net,application对象还真没怎么用过.看了看书,根据这两个对象的特性写了一个简单的聊天室程序.真的是非常的简陋 ASP.Net中有两个重要的对象,一个是applicat ...

  8. Mybatis驼峰式命名

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...

  9. keras数据增强

    1.keras数据增强:https://www.cnblogs.com/hutao722/p/10075150.html 2 .keras fit 中的 verbose verbose:日志显示ver ...

  10. mysql的索引以及优化

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...