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点的最短路 ...
随机推荐
- 解析基于keras深度学习框架下yolov3的算法
一.前言 由于前一段时间以及实现了基于keras深度学习框架下yolov3的算法,本来想趁着余热将自己的心得体会进行总结,但由于前几天有点事就没有完成计划,现在趁午休时间整理一下. 二.Keras框架 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-plus
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-upload
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 026-PHP常用字符串函数(三)
<?php //颠倒字串 print("abcdefg 颠倒 "); print(strrev("abcdefg")."<hr>&q ...
- Bean XML 配置(1)- 通过XML配置加载Bean
系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...
- json字符串格式化显示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ROS常用命令或经常碰到的问题
本篇博客会随时更新. 一.常用命令 1.添加环境变量 gedit ~/.bashrc 2.ubuntu系统监视器 gnome-system-monitor 二.问题 1.sudo apt-get up ...
- SQL的7种连接查询详细实例讲解
SQL的7种连接查询详细实例讲解 原文链接:https://mp.weixin.qq.com/s/LZ6BoDhorW4cSBhaGy8VUQ 在使用数据库查询语句时,单表的查询有时候不能满足项目的业 ...
- 【分类问题中模型的性能度量(二)】超强整理,超详细解析,一文彻底搞懂ROC、AUC
文章目录 1.背景 2.ROC曲线 2.1 ROC名称溯源(选看) 2.2 ROC曲线的绘制 3.AUC(Area Under ROC Curve) 3.1 AUC来历 3.2 AUC几何意义 3.3 ...
- oracle学习笔记(3)
使用profile文件对口令进行管理 sql>create profile 文件名 limit failed_login_arrempts 3 password_lock_time 2; 将配之 ...