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点的最短路 ...
随机推荐
- Python 编写代码 检查是否遵循PEP 8标准
实际上并非必须遵守PEP 8,但是它已经成为一个默认的.约定俗成的规则,可以使代码风格更统一,提高可读性. 由于最近一直在学习Ubuntu,因此此处仍然以Ubuntu为例,介绍一下规则检查工具,它能帮 ...
- S7-300数据处理基本知识(结尾以MW8+1 ADD指令实训仿真,并用状态表监控及刷写变量)
数据处理基本知识汇总 STEP7 的数据类型包括什么? 基本数据类型 复杂数据类型 用于FB(功能块)的输入,输出参数类型 用于FC(功能)的输入,输出参数类型 基本数据类型是什么? 先列举12种数据 ...
- Gym - 101190F Foreign Postcards (期望dp)
题意:有n张标有“C”或“F”的卡片. 1.随机取前k张(1<=k<=n) 2.若这k张的第一张为“C”,则不翻转,否则,全部翻转这k张. 3.然后处理剩下的n-k张 4.重复步骤1~3直 ...
- 51nod 1055:最长等差数列
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 收藏 取消关注 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 ...
- excel提取数字
部分提取,那么就用=-LOOKUP(,-MID(A1,MIN(FIND({0;1;2;3;4;5;6;7;8;9},A1&1234567890)),ROW($1:$1024))) ------ ...
- 洛谷 三月月赛 B
搞出每一位与前一位的差,然后区间修改只是会影响区间的端点,所以只修改一下端点的值就好. %%%高一神犇线段树 #include<bits/stdc++.h> #define N 10000 ...
- SpringMVC原理及流程解析
前言 春节期间宅在家里闲来无事,对SpringMVC进行了比较深入的了解,将之前模糊不清的地方基本摸索清楚了,特此撰文总结记录一下. 正文 一.一个请求为什么会调用到SpringMVC框架里? 首先问 ...
- SLAM的评测工具evo
遇到的问题 今天用orbslam2跑euroc数据集,将结果和真实轨迹用evo测评,发现差别特别大: evo_traj tum data.tum CameraTrajectory.txt --plot ...
- SQL优化工具 - SQL Server Profiler与数据库引擎优化顾问
最近项目做到几千个学生分别去人脸识别记录(目前约630000行)中查询最后一次记录,可想而知性能这块是个麻烦.于是乎,GET到了SQL Server Profiler和数据库引擎优化顾问这俩工SHEN ...
- Spring-ResolvableType可解决的数据类型
ResolvableType,可解决的数据类型.它为java语言中的所有类型提供了相同的数据结构,其内部封装了一个java.lang.reflect.Type类型的对象. 在讲解这个数据结构之前,首先 ...