Poj 3031 Big Christmas Tree (Water spfa)

Source: Internet
Author: User

 

Question:

Because of a technical difficulty, price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge ).This sentence has never been understood. We thought it was the minimal spanning tree later.

 

To give an undirected graph, calculate the sum of the weights of each node in the shortest path (dis [I]) * of each node.

Note: The edge weight value must be _ int64; undirected graph;

 

 

#include 
 
  #include 
  
   #include #include 
   
    #include
    #include 
     
      #include 
      
       #include 
       
        #include 
        
         #include 
         
          #define LL long long#define _LL __int64using namespace std;const _LL INF = 1e18;const int maxn = 50010;const int maxm = 50010;struct node{int v;_LL w;int next;}edge[2*maxm];int cnt,head[maxn];_LL W[maxn];int n,m;_LL ans,dis[maxn];void init(){cnt = 0;memset(head,-1,sizeof(head));}void add(int u, int v, _LL w){edge[cnt] = (struct node){v,w,head[u]};head[u] = cnt++;}void solve(){int inque[maxn];queue 
          
            que;while(!que.empty()) que.pop();memset(inque,0,sizeof(inque));for(int i = 1; i <= n; i++)dis[i] = INF;dis[1] = 0;inque[1] = 1;que.push(1);while(!que.empty()){int u = que.front();que.pop();inque[u] = 0;for(int i = head[u]; i != -1; i = edge[i].next){int v = edge[i].v;_LL w = edge[i].w;if(dis[v] > dis[u] + w){dis[v] = dis[u] + w;if(!inque[v]){inque[v] = 1;que.push(v);}}}}}int main(){int test;int u,v;_LL w;scanf(%d,&test);while(test--){init();scanf(%d %d,&n,&m);for(int i = 1; i <= n; i++)scanf(%I64d,&W[i]);for(int i = 1; i <= m; i++){scanf(%d %d %I64d,&u,&v,&w);add(u,v,w);add(v,u,w);}solve();ans = 0;bool flag = true;for(int i = 2; i <= n; i++){if(dis[i] == INF){flag = false;break;}ans += dis[i] * W[i];}if(flag == false)printf(No Answer);else printf(%I64d,ans);}return 0;}
          
         
        
       
      
     
   
  
 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.