POJ3013 Big Christmas Tree
题目:http://poj.org/problem?id=3013
求每个点到1的最短路。不是最小生成树。
总是WA。看讨论里说INF至少2e10,于是真的A了!
算一下,dis最大可能3276800000,于是开成3276800001,果然可A!还快了16ms(?)!
不过出现了: [Warning] this decimal constant is unsigned only in ISO C90 [enabled by default]。是什么意思呢?
总之以后设上界的时候还是略算一下。何况自己常用的0x 7 f f f f f f f也并不是十分大。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const long long INF=;
int T,n,m,head[],xnt,x,y,c[],z;
long long ans,dis[];
bool in[],flag;
struct Edge{
int next,to,w;
Edge(int ne=,int t=,int o=):next(ne),to(t),w(o) {}
}edge[];
queue<int> q;
void spfa()
{
while(q.size())q.pop();
memset(in,,sizeof in);
in[]=;dis[]=;
q.push();
while(q.size())
{
int k=q.front();q.pop();
in[k]=;///////!
for(int i=head[k],v;i;i=edge[i].next)
if(dis[k]+edge[i].w<dis[v=edge[i].to])
{
dis[v]=dis[k]+edge[i].w;
if(!in[v])
{
in[v]=;////////!
q.push(v);
}
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(head,,sizeof head);
ans=;xnt=;flag=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&c[i]),dis[i]=INF;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
edge[++xnt]=Edge(head[x],y,z);head[x]=xnt;
edge[++xnt]=Edge(head[y],x,z);head[y]=xnt;
}
spfa();
for(int i=;i<=n;i++)
{
if(dis[i]==INF)
{
flag=;break;
}
ans+=c[i]*dis[i];
}
if(flag) printf("No Answer\n");
else printf("%lld\n",ans);
}
return ;
}
POJ3013 Big Christmas Tree的更多相关文章
- POJ3013 Big Christmas Tree[转换 最短路]
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23387 Accepted: 5 ...
- POJ3013 Big Christmas Tree(最短路径树)
题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于 ...
- Big Christmas Tree(poj-3013)最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 25823 Accepted: 5 ...
- poj 3013 Big Christmas Tree
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20974 Accepted: 4 ...
- poj 3013 Big Christmas Tree (最短路径Dijsktra) -- 第一次用优先队列写Dijsktra
http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total S ...
- poj 3013 Big Christmas Tree Djistra
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个 ...
- POJ 3013 Big Christmas Tree(最短Dijkstra+优先级队列优化,SPFA)
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n. ...
- POJ Big Christmas Tree(最短的基础)
Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the ...
- HDU - 5156 Harry and Christmas tree
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5156 题意 : 给一颗编号为1-n的以1为根的树, 已知有m个颜色的礼物分布在某些节点上(同一节点 ...
随机推荐
- 46. 47. Permutations
求全排列. 1. 无重复元素 Given a collection of distinct numbers, return all possible permutations. For example ...
- ActiveMQ (一):安装启动及测试
1. 预备知识 1.1 JMS JMS(Java Messaging Service)是Java平台上有关面向消息中间件(MOM)的技术规范.<百科> 1.2 JMX JMX(Java M ...
- RK3288 GMAC整理
一.源文件 源码路径:\drivers\net\ethernet\rockchip\gmac 源码阅读顺序: 二.重要探针函数stmmac_dvr_probe 1. alloc_etherdev 申请 ...
- poj3020 二分图匹配 最大独立集
这是一道水题, 这里是最大流解法,之后再补 坑在又忘了反向建边了 题意:给你二维bool数组,让你求出能用多米诺骨牌覆盖所有 1 且骨牌最少的放法(因为多米诺骨牌1*2的结构方便描述,原题没有),原本 ...
- bzoj1464
题解: 简单bfs 显然不能到负数 也不能超过k两倍 代码: #include<bits/stdc++.h> using namespace std; ; int b[N],vis[N], ...
- FE英文缩写
PM:product manager,产品经理,又称品牌经理,brand managerRD:research and development enginner,研发工程师FE:front-end d ...
- 线程局部存储TLS
1 .使用线程局部存储的理由 当我们希望这个进程的全局变量变为线程私有时,而不是所有线程共享的,也就是每个线程拥有一份副本时,这时候就可以用到线程局部存储(TLS,Thread Local Stora ...
- hibernate单向关联与双向关联的区别(原)
今天看着hibernate的关联关系,有点迷糊.这里通过多对一表述一下双向与单向的区别. 多对一: 就是A表中的一个字段是B表的主键.多个A表中的数据可以对应一个B表中的数据,同理,一个B表中的数据可 ...
- TreeView添加treeView1_NodeMouseClick----多么痛的领悟。。。
TreeView添加treeView1_NodeMouseClick----多么痛的领悟... 1首先说一点,通过参考代码,已经实现了菜单项自动添加到TreeView控件的树视图了. 2.在移植(菜单 ...
- 【c++基础】int转string自动补零
前言 使用to_string函数可以将不同类型的数据转换为string类,请参考here和here.如果string的位数固定,如何进行自动补零呢?请看本文实例! 代码 确定位数,to_string ...