poj 3013 最短路SPFA算法
POJ_3013_最短路
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 23630 | Accepted: 5125 |
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 题意:给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和。
思路:要求∑(边权*子树点权和),等价于求∑(点权*点到根路径上的边权和)。
由于所要求的花费最小,所以这是个最短路问题。因为跟节点为1,求1到各个点的最小值,每个点的权值乘所经过的边加起来就好了
样例中的计算方法
40*(4+3+1)+60*(2+3+1)+50*(3+3+1)+20*(3+1)+10*1+30*(2+1)=
记得要最短路,所以样例中的1 5 9放弃。
根据数据,需要SPFA优化。SPFA优化了B-F算法的很多无效操作,只对发生更新的点进行操作。
存图时我采用了链式前向星,它是反着来的,当然vector也行,不过据说很慢。 关于链式前向星,这个博客讲的非常好:地址。。具体的我也不太愿意写了,因为内容基本相似。
Bellman-Ford有很多低效或无效的操作,其核心部分在于每一轮操作更新所有节点到起点s的最短距离。因此,在计算节点u之后,下一步只计算和调整它的邻居,可以加快收敛速度。
queue就是这么个操作。记得while一轮后,vis[u]=0,否则会wa,因为u可能会重新入队。
#include<iostream>
#include<cstring>
#include<queue>
typedef long long ll;
using namespace std;
const int maxn=;
#define inf (1ll<<60)
ll cnte=;
ll head[maxn];
ll verw[maxn];
bool vis[maxn];
ll v,e;
ll dis[maxn];
struct node
{
int v,next;
ll w;
}edge[maxn*];
void add(int a,int b,int w) //add操作
{
edge[cnte].v=b;
edge[cnte].w=w;
edge[cnte].next=head[a];
head[a]=cnte++;
}
bool spfa()
{
for(int i=;i<=v;i++)
{
dis[i]=inf;
vis[i]=false;
}
dis[]=;
vis[]=;
queue<int>q;
q.push();
while(!q.empty())
{
ll u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next) //遍历操作
{
ll v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w; if(!vis[v]){
q.push(v);
vis[v]=;
}
}
}
vis[u]=; //注意
}
for(int i=;i<=v;i++)
{
if(dis[i]==inf)
return ;
}
return ;
}
int main()
{
ll t;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&v,&e);
cnte=;
for(int i=;i<=v;i++)
scanf("%lld",&verw[i]);
memset(head,-,sizeof(head));
for(int i=;i<e;i++)
{
ll a,b,w;
scanf("%lld%lld%lld",&a,&b,&w);
add(a,b,w);
add(b,a,w);
}
if(spfa())
printf("No Answer\n");
else
{
ll sum=;
for(int i=;i<=v;i++)
{
sum+=verw[i]*dis[i];
// cout<<verw[i]<<" "<<dis[i]<<endl;
}
printf("%lld\n",sum);
}
}
}
poj 3013 最短路SPFA算法的更多相关文章
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- 图论-单源最短路-SPFA算法
有关概念: 最短路问题:若在图中的每一条边都有对应的权值,求从一点到另一点之间权值和最小的路径 SPFA算法的功能是求固定起点到图中其余各点的的最短路(单源最短路径) 约定:图中不存在负权环,用邻接表 ...
- 图论算法(三) 最短路SPFA算法
我可能要退役了…… 退役之前,写一篇和我一样悲惨的算法:SPFA 最短路算法(二)SPFA算法 Part 1:SPFA算法是什么 其实呢,SPFA算法只是在天朝大陆OIers的称呼,它的正统名字叫做: ...
- 最短路-SPFA算法&Floyd算法
SPFA算法 算法复杂度 SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环. SPFA一般情况复杂度是O(m)最坏情况下复杂度和朴素 ...
- poj 3013 最短路变形
http://poj.org/problem?id=3013 给出n个点,m个边.给出每个点的权值,每个边的权值.在m条边中选n-1条边使这n个点成为一棵树,root=1,求这棵树的最小费用,费用=树 ...
- poj 2449 k短路+A*算法
http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...
- POJ 1511 最短路spfa
题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...
- 最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...
- 单源最短路——SPFA算法(Bellman-Ford算法队列优化)
spfa的算法思想(动态逼近法): 设立一个先进先出的队列q用来保存待优化的结点,优化时每次取出队首结点u,并且用u点当前的最短路径估计值对离开u点所指向的结点v进行松弛操作,如果v点的最短路 ...
随机推荐
- Oracle--sqlplus--常用命令
登陆:win+R输入sqlplus即可 如果前期没有用户可以输入sqlplus /nolog 记得sqlplus后有一个空格 --格式化命令 进行数据查询时,默认的方式排版会很乱,如果我们要解决这个 ...
- C# 绘制矩形方框读写内存类 cs1.6人物透视例子
封装的有问题 其中方框可能在别的方向可能 会显示不出来建议不要下载了 抽时间我会用纯c#写一个例子的 其中绘制方框文字和直线调用的外部dll采用DX11(不吃CUP)绘制我封装成了DLL命名为 S ...
- PIP无法使用,script文件夹为空解决
[问题]环境变量已配置,但pip.pip3无法使用,且script文件夹为空解决: 一.安装pip3 python -m ensurepip 运行完之后就pip3有了: 二.安装pip python ...
- JS - 获取数组中的最后1个
arr = [1,2,3,4,5] console.log(arr[arr.length-1]) //输出的为5,即最后一个
- Redis 详解 (五) redis的五大数据类型实现原理
目录 1.对象的类型与编码 ①.type属性 ②.encoding 属性和 *prt 指针 2.字符串对象 3.列表对象 4.哈希对象 5.集合对象 6.有序集合对象 7.五大数据类型的应用场景 8. ...
- TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...
- Django——HttpResponse()
HttpResponse(content, #返回给视图函数的内容 content_type=None,#返回给视图函数的类型 text/html文本.text/plain.css.js.xml.js ...
- XML 之 语法详解
一.文档规则 .区分大小写. .属性值必须加引号(单引号.双引号都可以),一般情况下建议使用使用双引号. .所有标记必须有结束符号. .所有空标记必须关闭. .必须有且仅有一根元素. .解析空白字符时 ...
- C#编码习惯2
1.一定要用大括号括住流程控制元素,如for,while,if,switch内嵌的代码,即便只包含一行代码. 2.如果语句中有else if,一定要有一个else跟着最后一个else if. 3.只要 ...
- opencv目录(转)
github:https://github.com/opencv/opencv OpenCV 3 的源代码文件夹: 3rdparty/: 包含第三方库,如用视频解码用的 ffmpeg.jpg.png. ...