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次合成,找到最小的花费。
#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 (最短路)的更多相关文章
- Codeforces Gym 100269D Dwarf Tower spfa
Dwarf Tower 题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a ...
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Codeforces Gym 100269A Arrangement of Contest 水题
Problem A. Arrangement of Contest 题目连接: http://codeforces.com/gym/100269/attachments Description Lit ...
- Problem D. Dwarf Tower spfa
http://codeforces.com/gym/100269/attachments 首先建图,然后图中每条边的权值是会变化的,是由dis[x] + dis[y] ---> dis[m ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- Codeforces Gym 100269K Kids in a Friendly Class 构造题
Kids in a Friendly Class 题目连接: http://codeforces.com/gym/100269/attachments Description Kevin resemb ...
- Codeforces Gym 100269G Garage 数学
Garage 题目连接: http://codeforces.com/gym/100269/attachments Description Wow! What a lucky day! Your co ...
- Codeforces Gym 100269F Flight Boarding Optimization 树状数组维护dp
Flight Boarding Optimization 题目连接: http://codeforces.com/gym/100269/attachments Description Peter is ...
- Codeforces Gym 100269B Ballot Analyzing Device 模拟题
Ballot Analyzing Device 题目连接: http://codeforces.com/gym/100269/attachments Description Election comm ...
随机推荐
- (二)inlineCallbacks,同步方式写异步代码
一. 上篇提到了用defered对象注册回调的方式,来处理异步操作,这样大家都知道,实际情况代码很难搞的.因为当业务逻辑复杂后,这边一个异步操作,我们注册一个回调,代码跳到A地方,A里面也有异步操作, ...
- C#WebApi自动生成文档
1.效果图 2.在webApi项目,打开Nuget,搜索WebApiTestClient,安装WebApiTestClient,注意是给HelpPage的 3.打开引入WebApiTestClient ...
- 用了 10 多年的 Tomcat 居然有bug !
Java技术栈 www.javastack.cn 优秀的Java技术公众号 为了解决分布式链路追踪的问题,我们引入了实现OpenTracing的Jaeger来实现.然后我们为SpringBoot框架写 ...
- zebra代码分析
http://blog.csdn.net/xuyanbo2008/article/details/7439738
- python列表的复制,扯一下浅拷贝与深拷贝的区别
将一个列表的数据复制到另一个列表中.使用列表[:],可以调用copy模块 import copy A = [21,22,23,24,['a','b','c','d'],25,26] B = A #直接 ...
- js面向过程-拖拽
1.步骤分析: 1.1 获取id 1.2 当鼠标点击时执行的js 1.3当鼠标移动时执行的js 1.4当鼠标放开时执行的js 2.代码实现 <!DOCTYPE html> <html ...
- js防抖和节流优化浏览器滚动条滚动到最下面时加载更多数据
防抖和节流,主要是用来防止过于平凡的执行某个操作,如浏览器窗口变化执行某个操作,监听某个input输入框keyup变化,瀑布流布局时Y轴滚动,图片加载. js函数的防抖 经过一段事件才执行某个操作,如 ...
- python 模块发布及使用
将模块(此处名为nester)写好后,与setup.py放入同一个文件夹中: //setup.py from distutils.core import setup setup( name=" ...
- 基于linux(CentOS7)数据库性能优化(Postgresql)
基于CentOS7数据库性能优化(Postgresql) 1. 磁盘 a) Barriers IO i. 通过查看linux是否加载libata,确定是否开 ...
- 使用HashSet实现不重复的随机数
package demo; import java.util.HashSet; import java.util.Random; public class RandomDemo { public st ...