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结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和。
思路:要求∑(边权*子树点权和),等价于求∑(点权*点到根路径上的边权和)。
第一道SPFA。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define N 50005
#define LL long long
#define INF (1LL<<60) struct Edge
{
int v,next;
LL w;
} edge[N]; int head[N]; int cnte=;
void addEdge(int a,int b,int w)
{
edge[cnte].v=b;
edge[cnte].w=w;
edge[cnte].next=head[a];
head[a]=cnte++;
} LL dist[N];
bool vis[N];
bool Spfa(int n)
{
for(int i=; i<=n; i++)
{
dist[i]=INF;
vis[i]=;
}
dist[]=;
vis[]=;
queue<int>q;
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].v;
if(dist[v]>dist[u]+edge[i].w)
{
dist[v]=dist[u]+edge[i].w;
if(vis[v]==)
{
vis[v]=;
q.push(v);
}
}
}
vis[u]=;
}
for(int i=; i<=n; i++)
{
if(dist[i]==INF)
return ;
}
return ;
} int verw[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
cnte=;
int vn,en;
scanf("%d%d",&vn,&en);
for(int i=; i<=vn; i++)
scanf("%d",verw+i);
memset(head,-,sizeof(head));
for(int i=; i<en; i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
addEdge(a,b,w);
addEdge(b,a,w);
}
if(!Spfa(vn))
{
puts("No Answer");
continue;
}
else
{
LL res=;
for(int i=; i<=vn; i++)
res+=verw[i]*dist[i];
printf("%lld\n",res);
}
}
return ;
}
POJ_3013_最短路的更多相关文章
- poj 3013 最短路SPFA算法
POJ_3013_最短路 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23630 ...
- bzoj1001--最大流转最短路
http://www.lydsy.com/JudgeOnline/problem.php?id=1001 思路:这应该算是经典的最大流求最小割吧.不过题目中n,m<=1000,用最大流会TLE, ...
- 【USACO 3.2】Sweet Butter(最短路)
题意 一个联通图里给定若干个点,求他们到某点距离之和的最小值. 题解 枚举到的某点,然后优先队列优化的dijkstra求最短路,把给定的点到其的最短路加起来,更新最小值.复杂度是\(O(NElogE) ...
- Sicily 1031: Campus (最短路)
这是一道典型的最短路问题,直接用Dijkstra算法便可求解,主要是需要考虑输入的点是不是在已给出的地图中,具体看代码 #include<bits/stdc++.h> #define MA ...
- 最短路(Floyd)
关于最短的先记下了 Floyd算法: 1.比较精简准确的关于Floyd思想的表达:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设maz ...
- bzoj1266最短路+最小割
本来写了spfa wa了 看到网上有人写Floyd过了 表示不开心 ̄へ ̄ 改成Floyd试试... 还是wa ヾ(。`Д´。)原来是建图错了(样例怎么过的) 结果T了 于是把Floyd改回spfa 还 ...
- HDU2433 BFS最短路
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 最短路(代码来源于kuangbin和百度)
最短路 最短路有多种算法,常见的有一下几种:Dijstra.Floyd.Bellman-Ford,其中Dijstra和Bellman-Ford还有优化:Dijstra可以用优先队列(或者堆)优化,Be ...
- Javascript优化细节:短路表达式
什么是短路表达式? 短路表达式:作为"&&"和"||"操作符的操作数表达式,这些表达式在进行求值时,只要最终的结果已经可以确定是真或假,求值过程 ...
随机推荐
- 一个简单的js队列,逻辑很清晰
function Queue(type) { //type 是否是一个接着一个执行 function QueueConst() {} QueueConst.execute_ing=[], QueueC ...
- Solidworks如何修改单位
文档属性-单位-修改成mm
- Nodejs创建HTTPS服务器
Nodejs创建HTTPS服务器 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Jav ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- 如何在 webpack 项目中使用绝对路径
react 小白编程 本项目是使用 create-react-app 脚手架构建的react项目 找到 config 配置文件下的 webpack.config.dev.js 和 webpack.co ...
- AngularJS入门学习
初识: {{}} 这种双层花括号的语法称之为:插值语法:也可以说是 标识符:AngularJS 主要就是使用这种方法进行数据绑定 ng-module="name" 在ng的 ...
- 应用程序无法正常启动 0xc0000013 vs2013
今天下午切换到Windows 优化代码,在debug 的时候一直出现这个问题,折腾了很久,发现原来是系统环境变量的问题,我之前装了双系统,讲原来win7 下的一块E盘删掉做了Linux 盘,而系统环境 ...
- Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀+后缀
B. Laurenty and Shop ...
- ios--plist
// // main.m // 03-plist文件的回顾 // // Created by xiaomage on 15/12/29. // Copyright © 2015年 小码哥. All r ...
- android TextView不用ScrollViewe也可以滚动的方法
TextView textview = (TextView) findViewById(R.id.text); /** * * 只有调用了该方法,TextView才能不依赖于ScrollView而实现 ...