| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 25643 | Accepted: 5539 |
Description
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
Source
题意:
每一个点有一个权值,让你构造一棵树,每个点的贡献值就是根到这个点的距离乘于这个点的权值。
思路:
很明显这是一个最短路的问题
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <queue>
#include <vector>
#include<functional>
#define INF 10000000000
using namespace std;
typedef long long LL;
typedef pair<LL, int> plli;
priority_queue<plli, vector<plli>, greater<plli> > q;
LL head[50001],quan[50001],n,m,book[50001];
LL dp[50001];
struct node {
int u, v, w, next;
}edge[100010];
void addegge(int u,int v,int w,int k)
{
edge[k].u = u; edge[k].v = v; edge[k].w = w;
edge[k].next = head[u]; head[u] = k++;
u = u ^ v; v = u ^ v; u = u ^ v;
edge[k].u = u; edge[k].v = v; edge[k].w = w;
edge[k].next = head[u]; head[u] = k++;
}
LL dij()
{
LL ans = 0,u;
int count = 0;
dp[1] = 0;
q.push(make_pair(dp[1], 1));
while (!q.empty())
{
plli temp1 = q.top();
q.pop();
u = temp1.second;
if (book[u])
continue;
count++;
book[u] = 1;
ans += quan[u] * dp[u];
for (int i = head[u]; i != -1; i = edge[i].next)
{
int w = edge[i].w;
int v = edge[i].v;
if (dp[v] > w + dp[u])
{
dp[v] = w + dp[u];
q.push(make_pair(dp[v], v));
}
}
}
if (count < n)
return -1;
else
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%lld", &quan[i]);
while (!q.empty()) q.pop();
for (int i = 1; i <= n; i++)
{
book[i] = 0;
head[i] = -1;
dp[i] = INF;
}
for (int i = 1; i <= 2 * m; i += 2)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
addegge(a,b,c,i);
}
if (n == 0 || n == 1)
{
printf("0\n");
continue;
}
__int64 ans = dij();
if (ans != -1)
printf("%I64d\n", ans);
else
printf("No Answer\n");
}
return 0;
}
本文介绍了一个经典的图论问题——如何构造一棵最小代价的圣诞树。每个节点都有一个权重,每条边的成本由其连接的所有后代节点的权重总和与单位价格相乘决定。文章提供了完整的代码实现,并解释了如何通过最短路径算法来解决这个问题。

445

被折叠的 条评论
为什么被折叠?



