poj 3013 big christmas tree 最短路SPFA

本文介绍了一个特殊的圣诞树搭建问题,需要在给定节点和边的情况下,寻找一种方式来连接这些节点形成一棵树,使得整棵树的成本最低。文章提供了一种算法实现思路,包括如何计算每条边的价格以及如何寻找最小成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Big Christmas Tree

   


Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of the tree is shown in right picture.

The tree can be represented as a collection of numbered nodes and some edges. The nodes are numbered 1 through n. The root is always numbered 1. Every node in the tree has its weight. The weights can be different from each other. Also the shape of every available edge between two nodes is different, so the unit price of each edge is different. Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).

Suby wants to minimize the cost of whole tree among all possible choices. Also he wants to use all nodes because he wants a large tree. So he decided to ask you for helping solve this task by find the minimum cost.

Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case consists of several lines. Two numbers v, e (0 ≤ v, e ≤ 50000) are given in the first line of each test case. On the next line, v positive integers wi indicating the weights of v nodes are given in one line. On the following e lines, each line contain three positive integers a, b, c indicating the edge which is able to connect two nodes a and b, and unit price c.

All numbers in input are less than 216.

Output
For each test case, output an integer indicating the minimum possible cost for the tree in one line. If there is no way to build a Christmas tree, print “No Answer” in one line.

Sample Input
2
2 1
1 1
1 2 15
7 7
200 10 20 30 40 50 60
1 2 1
2 3 3
2 4 2
3 5 4
3 7 2
3 6 3
1 5 9
Sample Output
15
1210

题意真的是太难懂,不知道大佬们是怎么看懂的,为什么不是最小生成树,真的很一脸懵逼呀,,,但是题还是要刷的。。求出最短路之后,乘以点的权值相加。(inf的值狠大,WA了无数次。)

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#define inf 0x3f3f3f3f3f
using namespace std;
long long dis[50005];
bool vis[50005];
int v[50005];
int n,m;
struct node
{
    int u,v,w;
    struct node*next;
}*h[50005];
void LA(int u,int v,int w)
{
    struct node*p=(struct node*)malloc(sizeof(struct node));
    p->v=v;
    p->w=w;
    p->next=h[u];
    h[u]=p;
}
void in()
{
    for(int i=0; i<=n; i++)
    {
        dis[i]=inf;
    }
}
void spfa(int s)
{
    memset(vis,false,sizeof(vis));
    queue<int>q;
    dis[s]=0;
    vis[s]=true;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        struct node*p;
        for(p=h[u]; p!=NULL; p=p->next)
        {
            int Q=p->v;
            if(dis[Q]>dis[u]+p->w)
            {
                dis[Q]=dis[u]+p->w;
                if(!vis[Q])
                {
                    vis[Q]=true;
                    q.push(Q);
                }
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(h,0,sizeof(h));
        memset(v,0,sizeof(v));
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&v[i]);
        }
        for(int i=0; i<m; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            LA(a,b,c);
            LA(b,a,c);
        }
        in();
        spfa(1);
        long long ans=0;
        int flag=0;
        for(int i=1; i<=n; i++)
        {
            if(dis[i]==inf)
            {
                flag=1;
                printf("No Answer\n");
                break;
            }
            ans+=(dis[i]*v[i]);
        }
        if(flag==0)
            cout<<ans<<endl;
    }
}







内容概要:本文档详细介绍了一个基于MATLAB实现的跨尺度注意力机制(CSA)结合Transformer编码器的多变量时间序列预测项目。项目旨在精准捕捉多尺度时间序列特征,提升多变量时间序列的预测性能,降低模型计算复杂度与训练时间,增强模型的解释性和可视化能力。通过跨尺度注意力机制,模型可以同时捕获局部细节和全局趋势,显著提升预测精度和泛化能力。文档还探讨了项目面临的挑战,如多尺度特征融合、多变量复杂依赖关系、计算资源瓶颈等问题,并提出了相应的解决方案。此外,项目模型架构包括跨尺度注意力机制模块、Transformer编码器层和输出预测层,文档后提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,尤其是熟悉MATLAB和深度学习的科研人员、工程师和研究生。 使用场景及目标:①需要处理多变量、多尺度时间序列数据的研究和应用场景,如金融市场分析、气象预测、工业设备监控、交通流量预测等;②希望深入了解跨尺度注意力机制和Transformer编码器在时间序列预测中的应用;③希望通过MATLAB实现高效的多变量时间序列预测模型,提升预测精度和模型解释性。 其他说明:此项目不仅提供了一种新的技术路径来处理复杂的时间序列数据,还推动了多领域多变量时间序列应用的创新。文档中的代码示例和详细的模型描述有助于读者快速理解和复现该项目,促进学术和技术交流。建议读者在实践中结合自己的数据集进行调试和优化,以达到佳的预测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值