HAUT校赛 魔法宝石 暴力

问题 C: 魔法宝石

时间限制: 2 秒  内存限制: 64 MB
提交: 505  解决: 149
提交  状态 
题目描述

小s想要创造n种魔法宝石。小s可以用ai的魔力值创造一棵第i种魔法宝石,或是使用两个宝石合成另一种宝石(不消耗魔力值)。请你帮小s算出合成某种宝石的所需的最小花费。

输入

第一行为数据组数T(1≤T≤3)。
对于每组数据,首先一行为n,m(1≤n,m≤10^5)。分别表示魔法宝石种类数和合成魔法的数量。
之后一行n个数表示a1到an。(1≤ai≤10^9)。a_i表示合成第i种宝石所需的魔力值。
之后n行,每行三个数a,b,c(1≤a,b,cn),表示一个第a种宝石和第b种宝石,可以合成一个第c种宝石。

输出

每组数据输出一行n个数,其中第i个数表示合成第i种宝石的魔力值最小花费。

样例输入
13 11 1 101 2 3
样例输出

1 1 2

显然,每次更新最值小值就可以了,我把数据开到了1e2,这个应该满足上界了。

#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
//#include <bits/stdc++.h>
//#define LOACL
#define space " "
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
//typedef __int64 Int;
typedef pair<int, int> PAI;
const int INF = 0x3f3f3f3f;
const double ESP = 1e-5;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const int MAX = 3000;
int ar[MAXN];
struct node 
{
    int a, b, c;
}data[MAXN];
int main(int argc, char const *argv[])
{
    int T, N, M;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &N, &M);
        for (int i = 1; i <= N; i++) scanf("%d", &ar[i]);
        for (int i = 0; i < M; i++) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            data[i].a = a;
            data[i].b = b;
            data[i].c = c;
            ar[c] = min(ar[c], ar[a] + ar[b]);
        }
        for (int i = 0; i < 1e2; i++) {
            for (int j = 0; j < M; j++) {
                int ta = data[j].a;
                int tb = data[j].b;
                int tc = data[j].c;
                ar[tc] = min(ar[tc], ar[ta] + ar[tb]);
            }
        }
        printf("%d", ar[1]);
        for (int i = 2; i <= N; i++) {
            printf(" %d", ar[i]);
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值