【问题描述】
Vasya在玩一个叫做"Dwarf Tower"的游戏,这个游戏中有n个不同的物品,
它们的编号为1到n。现在Vasya想得到编号为1的物品。
获得一个物品有两种方式:
1. 直接购买该物品,第i件物品花费的钱为ci
2. 用两件其他物品合成所需的物品,一共有m种合成方式。
请帮助Vasya用最少的钱获得编号为1的物品。
【输入格式】
第一行有两个整数n,m(1<=n<=10000,0<=m<=100000),分别表示有n种物品以
及m种合成方式。
接下来一行有n个整数,第i个整数ci表示第i个物品的购买价格,其中
0<=ci<=10^9。
接下来m行,每行3个整数ai,xi,yi,表示用物品xi和yi可以合成物品ai,其
中(1<=ai,xi,yi<=n; ai<>xi, xi<>yi, yi<>ai)
【输出格式】
一行,一个整数表示获取物品 1 的最少花费。

输入样例: 输出样例:
5 3
5 0 1 2 5
5 2 3
4 2 3
1 4 5
2

【数据规模与约定】
60%的数据, n<=100
100%的数据, n<=10000, m<=100000

分析:稍微想一想就能发现这是最短路,合成操作看成松弛操作就可以了.

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int inf = 0x7fffffff;
int n, m, c[], d[],head[], to[], nextt[], w[], tot = ;
bool vis[]; void add(int x, int y, int z)
{
w[tot] = z;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void spfa()
{
queue <int> q;
for (int i = ; i <= n; i++)
{
d[i] = c[i];
vis[i] = ;
q.push(i);
}
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (d[v] > d[u] + d[w[i]])
{
d[v] = d[u] + d[w[i]];
if (!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
} int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++)
scanf("%d", &c[i]);
for (int i = ; i <= m; i++)
{
int t, x, y;
scanf("%d%d%d", &t, &x, &y);
add(x, t, y);
add(y, t, x);
}
spfa();
printf("%d\n", d[]); return ;
}

noip模拟赛 dwarf tower的更多相关文章

  1. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  2. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  5. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  6. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  7. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  8. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

随机推荐

  1. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居【切比雪夫距离+并查集+multiset】

    参考:http://hzwer.com/4361.html 坐标开long long,inf开大点 先曼哈顿转切比雪夫(x+y,x-y),距离就变成了max(x',y'): 先按x排序,维护两个指针, ...

  2. 清北考前刷题day2早安

    /* 做法一:按h sort一遍,对于一段区间[i,j],高度花费就是h[j]-h[i] 然后枚举区间,把区间内C排序,一个一个尽量选即可. n^3logn 标算:n^3 dp 高度排序,保证从前往后 ...

  3. 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)

    Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...

  4. 使用JS分页 <span> beta 2.0 未封装的分页

    <html> <head> <title>分页</title> <style> #titleDiv{ width:500px; backgr ...

  5. 用 jQuery 实现简单倒计时功能

    问题场景:假设某个活动截止时间给定了,现在需要开发一个页面可以自动刷新距离活动截止时间还剩多少天? <!DOCTYPE html> <html xmlns="http:// ...

  6. oracle多语言环境下to_date时间转换问题

    现象:在多语言环境下使用过oracle的同学想必都遇到过这样一个问题, date_v date; date_v := to_date('2010/11/16');--或'2010/11/16' 同一个 ...

  7. 创建http对象

    package test; import java.net.HttpURLConnection;import java.net.URL; import javax.servlet.http.HttpS ...

  8. cocos2d-x win7 部署

    1. 安装 下载python  https://www.python.org/downloads/release/python-279/ 2.从官网下载cocos2d-x  http://www.co ...

  9. iOS浏览器不能打开手机QQ客服与指定用户聊天界面

    这个问题是我在公司需求的时候遇到的,QQ推广工具网站获取的链接在苹果自带浏览器没法打开到聊天界面,是因为safair在打开到app store的时候把参数给丢了,app store再打开到QQ的时候就 ...

  10. 转载pcb设计详细版

    http://www.51hei.com/bbs/dpj-52438-1.html 详细的altium designer制作PCB步骤,按照步骤一步步的学习就会自己制作PCB模型 目 录 实验三  层 ...