zoukankan      html  css  js  c++  java
  • POJ3013 Big Christmas Tree[数据超大,谨慎小心]

    Big Christmas Tree
    Time Limit: 3000MS   Memory Limit: 131072K
    Total Submissions: 17317   Accepted: 3644

    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

    POJ Monthly--2006.09.29, Kim, Chan Min (kcm1700@POJ)

    题意:从各点到1的最短路径乘以权值就是结果了。

    code:

      1 #include<iostream>
      2 #include<queue>
      3 using namespace std;
      4 
      5 #define MAXN 50010
      6 #define inf 1LL<<61            //这个inf必须用这个,不然会WA
      7 
      8 typedef struct edge
      9 {
     10     long long a,b;
     11     long long dis;
     12     int next;
     13 }Edge;
     14 Edge e[MAXN*6];
     15 
     16 long long head[MAXN];
     17 long long w[MAXN];
     18 long long v,ei;
     19 long long edgenum;
     20 
     21 void addedge(long long s,long long t,long long k)
     22 {
     23     e[edgenum].a=s;e[edgenum].b=t;e[edgenum].dis=k;e[edgenum].next=head[s];head[s]=edgenum++;
     24     e[edgenum].a=t;e[edgenum].b=s;e[edgenum].dis=k;e[edgenum].next=head[t];head[t]=edgenum++;
     25 }
     26 
     27 long long dis[MAXN];
     28 bool vst[MAXN];
     29 void spfa()
     30 {
     31     for(int i=1;i<=MAXN;i++)
     32     {
     33         dis[i]=inf;
     34         vst[i]=false;
     35     }
     36     dis[1]=0;
     37     vst[1]=true;
     38     queue<long long>Q;
     39     Q.push(1);
     40     while(!Q.empty())
     41     {
     42         long long temp=Q.front();
     43         Q.pop();
     44         vst[temp]=true;
     45         for(int i=head[temp];i!=-1;i=e[i].next)
     46         {
     47             if(dis[e[i].b]>dis[temp]+e[i].dis)
     48             {
     49                 dis[e[i].b]=dis[temp]+e[i].dis;
     50                 if(!vst[e[i].b])
     51                 {
     52                     vst[e[i].b]=1;
     53                     Q.push(e[i].b);
     54                 }
     55             }
     56         }
     57         vst[temp]=false;
     58     }
     59     bool flag=true;
     60     long long sum=0;
     61     for(int i=2;i<=v;i++)
     62     {
     63         if(dis[i]<inf)
     64         {
     65             sum+=dis[i]*w[i];
     66         }
     67         else
     68         {
     69             flag=false;
     70             break;
     71         }
     72     }
     73     if(flag)
     74         printf("%I64d\n",sum);
     75     else
     76         printf("No Answer\n");
     77 }
     78 
     79 int main()
     80 {
     81     int t;
     82     scanf("%d",&t);
     83     while(t--)
     84     {
     85         memset(head,-1,sizeof(head));
     86         scanf("%I64d%I64d",&v,&ei);
     87         int i;
     88         edgenum=0;
     89         for(i=1;i<=v;i++)
     90             scanf("%I64d",&w[i]);
     91         for(i=0;i<ei;i++)
     92         {
     93             long long s,t,k;
     94             scanf("%I64d%I64d%I64d",&s,&t,&k);
     95             addedge(s,t,k);
     96         }
     97         spfa();
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    IBM 2013策略发布:大数据和分析、云计算、企业移动、社交商务、智慧商务、智慧城市
    BakAndImgCD 6.0 发布,数据备份和映像
    YaCy 1.4 发布,分布式Web搜索引擎
    Pig安装及本地模式测试,体验
    GCC 4.7.3 发布
    UPUPW Nginx版PHP高配引擎发布
    如何选择基于云的大数据方案
    PortalBasic v3.1.1 beta1 示例工程发布
    LLVM 编译器架构获得 ACM 软件系统奖
    123 Flash Chat Server 9.9 发布
  • 原文地址:https://www.cnblogs.com/XBWer/p/2662899.html
Copyright © 2011-2022 走看看