SPFA算法之三

//poj 3013 Big Christmas Tree

#include
<iostream>
#include
<deque>
using namespace std;
#define maxn 50002
const __int64 inf=(__int64)1<<63-1;
struct Edge
{
int v;
int weight;
int next;
}Vertex[
4*maxn];
int head[maxn],curr;
int cases,v,e,i,j,edge[maxn][3],w[maxn];
void add_edge(int s,int t,int w) //新增结点不用申请空间, 跑了 579 MS
{
Vertex[curr].v
=t;
Vertex[curr].weight
=w;
Vertex[curr].next
=head[s];
head[s]
=curr++;
}
bool S[maxn];
__int64 distD[maxn],sum;
void spfa(int u)
{
fill(distD,distD
+v+1,inf);
distD[u]
=0;
memset(S,
0,sizeof(S[0])*(v+1));
S[u]
=1;
deque
<int> col;
col.push_back(u);
while(!col.empty())
{
int uu=col.front();
col.pop_front();
S[uu]
=0;
for(i=head[uu];i!=-1;i=Vertex[i].next)
{
if(distD[uu]+Vertex[i].weight<distD[Vertex[i].v])
{
distD[Vertex[i].v]
=distD[uu]+Vertex[i].weight;
if(!S[Vertex[i].v])
{
S[Vertex[i].v]
=1;
col.push_back(Vertex[i].v);
}
}
}
}
for(i=1;i<=v;++i)
if(distD[i]==inf)
{
printf(
"No Answer\n");
return ;
}
sum
=0;
for(i=1;i<=v;++i)
sum
+=w[i]*distD[i];
printf(
"%I64d\n",sum);
}
int main()
{
scanf(
"%d",&cases);
while(cases--)
{
scanf(
"%d%d",&v,&e);
for(i=1;i<=v;++i)
scanf(
"%d",&w[i]);
memset(head,
-1,sizeof(head));
curr
=0;
for(i=1;i<=e;++i)
{
scanf(
"%d%d%d",&edge[i][0],&edge[i][1],&edge[i][2]);
add_edge(edge[i][
0],edge[i][1],edge[i][2]);
add_edge(edge[i][
1],edge[i][0],edge[i][2]);
}
spfa(
1);
}
return 0;
}



/*


这题要有个重要的转化;
price of an edge will be (sum of weights of all descendant nodes) × (unit price of the edge).
每条边的代价为该边所有"后继"节点权值(重量weight)之和乘以该边的权值(unit price)。
换个角度就是每个节点的代价为该节点到根节点的所有边的权值乘以该节点的权值。
要使得总的花费最小,其实就是求从根结点到每个点的最短路径*该点的权值,然后求和
数据量很大,采用SPFA算法

*/

  

转载于:https://www.cnblogs.com/mjc467621163/archive/2011/08/24/2152149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值