1262: 魔法宝石

http://218.28.220.249:50015/JudgeOnline/problem.php?id=1262

1262: 魔法宝石

时间限制: 2 秒  内存限制: 64 MB
提交: 371  解决: 96
提交 状态 

题目描述

小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种宝石的魔力值最小花费。

样例输入

1
3 1
1 1 10
1 2 3

样例输出

1 1 2

提示

极限思想

#include <iostream>
#include<cstdio>
#include<math.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
typedef long long LL;
#define lson root<<1
#define rson root<<1|1
using namespace std;
const int maxn=100005;
const int INF=0x3f3f3f3f;
int v[maxn];
int a[maxn], b[maxn], c[maxn];

int main()
{
    int T;
    scanf("%d", &T);

    while(T--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        for(int i=1; i<=n; i++)
            scanf("%d", &v[i]);
        for(int i=1; i<=m; i++)
            scanf("%d %d %d", &a[i], &b[i], &c[i]);

        for(int j=0; j<100; j++)
        {
            for(int i=1; i<=m; i++)
            {
                v[c[i]]=min(v[c[i]], v[a[i]]+v[b[i]]);
            }
        }

        for(int i=1; i<=n; i++)
            printf("%d%c", v[i], i==n?'
':' ');
    }
    return 0;
}
原文地址:https://www.cnblogs.com/w-y-1/p/6758983.html