zoukankan      html  css  js  c++  java
  • POj --- 3013 Big Christmas Tree

    Big Christmas Tree
    Time Limit: 3000MS   Memory Limit: 131072K
    Total Submissions: 20034   Accepted: 4304

    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 ≤ ve ≤ 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 abc 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

    思路:ans = 每条边(u,v)*v的子树节点的w = 所有的dist[v]*w[v]之和;

     1 #include<iostream>
     2 #include<queue>
     3 #include<cstdio>
     4 #include<cstring>
     5 #define MAX 500005
     6 const long long int  INF = 100000000000000;
     7 using namespace std;
     8 typedef struct{
     9     int to, next, w;
    10 }Node;
    11 Node edge[MAX*2];
    12 queue<int>q;
    13 long long int vis[MAX], head[MAX], dist[MAX], W[MAX];
    14 long long int ans;
    15 void init(int n){
    16     memset(vis, 0, sizeof(vis));
    17     memset(head, -1, sizeof(head));
    18     for(int i = 1;i <= n;i ++)
    19         dist[i] = INF;
    20     dist[1] = 0;
    21 }
    22 void spfa(int s){
    23     while(!q.empty()) q.pop();
    24     q.push(s);
    25     vis[s] = 1;
    26     while(!q.empty()){
    27         int p =q.front();
    28         q.pop();
    29         vis[p] = 0;
    30         for(int i = head[p];i != -1;i = edge[i].next){
    31             int v = edge[i].to;
    32             if(dist[v] > dist[p] + edge[i].w){
    33                 dist[v] = dist[p] + edge[i].w;
    34                 if(!vis[v]){
    35                     q.push(v);
    36                     vis[v] = 1;
    37                 }
    38             }
    39         }
    40     }
    41 }
    42 void AddEdge(int i, int u, int v, int w){
    43     edge[i].to = v;
    44     edge[i].w = w;
    45     edge[i].next = head[u];
    46     head[u] = i;
    47 }
    48 int main(){
    49     int T, n, e, u, v, price, k;
    50     /* freopen("in.c", "r", stdin); */
    51     scanf("%d", &T);
    52     while(T--){
    53         scanf("%d%d", &n, &e);
    54         init(n);
    55         for(int i = 1;i <= n;i ++)
    56             cin >> W[i]; 
    57         k = 0;
    58         for(int i = 0;i < e;i ++){
    59             scanf("%d%d%d", &u, &v, &price);
    60             AddEdge(k++, u, v, price);
    61             AddEdge(k++, v, u, price);
    62         }
    63         spfa(1);
    64         int flag = 0;
    65         ans = 0LL;
    66         for(int i = 2;i <= n;i ++){
    67             if(dist[i] == INF){
    68                 flag = 1;
    69                 break;
    70             }
    71             ans += W[i]*dist[i];
    72         }
    73         if(!flag) cout << ans << endl;
    74         else cout << "No Answer
    ";
    75     }
    76     return 0;
    77 }
    
    
  • 相关阅读:
    mysql分页查询优化
    java反射及Method的Invoke方法(转载)
    java需会(转载)
    Java注解(Annotation)原理详解
    深入分析JDK动态代理
    Java并发编程:volatile关键字解析
    安装hadoop
    linux 配置ssh免密登录
    安装Centos 7 并且配置远程登录
    MAC安装VMware fusion
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3615560.html
Copyright © 2011-2022 走看看