# POJ_3013_Big Christmas Tree

1.图是无向图来的

2.边数组要开10W以上

3.求最短路时，保存中间路径长度要用long long，当然结果也就是long long保存啦

5.INF要大一点，可以用0x7f7f7f7f7f7f7f7fLL;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Edge {
int to,next;
int dis;
}Edge;

const int MAXN = 200100;
const int MAXM = MAXN << 1;
const long long INF = 0x7f7f7f7f7f7f7f7fLL;

bool vis[MAXN];
Edge edge[MAXN];
long long dis[MAXN];
int val[MAXN];
int queue[MAXM];
int total,n,m;

void init();
void spfa(int src);

int main(int argc,char * argv[]) {
int i,j,k;
int x,y,z;
int Case,flag;
long long ans;
scanf("%d",&Case);
while (Case-->0) {
scanf("%d%d",&n,&m);
init();
for (i=1;i<=n;++i) scanf("%d",val+i);
for (i=1;i<=m;++i) {
scanf("%d%d%d",&x,&y,&z);
}
if (0 == n || 1 == n) {
printf("0\n");
continue;
}
if (0 == m) {
continue;
}
spfa(1);
flag = 1;
ans = 0LL;
for (i=2;i<=n;++i) {
if (INF == dis[i]) {
flag = 0;
break;
}
ans += dis[i] * val[i];
}
if (!flag) {
} else {
printf("%lld\n",ans);
}
}
return 0;
}

void spfa(int src) {
int p,num;
int front,tail,cur;
memset(dis,0x7f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[src] = 0;
vis[src] = 1;
tail = 0;
front = -1;
queue[0] = src;
while (front != tail) {
++front;
if (front >= MAXM) front = 0;
cur = queue[front];
while (p) {
if (-1 == dis[edge[p].to] || dis[edge[p].to] - edge[p].dis > dis[cur]) {
dis[edge[p].to] = dis[cur] + edge[p].dis;
if (!vis[edge[p].to]) {
vis[edge[p].to] = 1;
++tail;
if (tail >= MAXM) tail = 0;
queue[tail] = edge[p].to;
}
}
vis[cur] = 0;
p = edge[p].next;
}
}

}

void init() {
total = 0;
for (int i=0;i<=n;++i)
}

void add_edge(int from,int to,int dis) {
++total;
edge[total].to = to;
edge[total].dis = dis;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;

typedef struct Edge {
int to,next;
int dis;
}Edge;

typedef struct Node {
int from;
long long dis;
Node(){};
Node(int f,long long d):from(f),dis(d){};
}Node;

const int MAXN = 200100;
const long long INF = 0x7f7f7f7f7f7f7f7fLL;

bool vis[MAXN];
Edge edge[MAXN];
long long dis[MAXN];
int val[MAXN];
int total,n,m;

void init();
void dijkstra(int src);
bool operator < (const Node &a,const Node &b);

int main(int argc,char * argv[]) {
int i,j,k;
int x,y,z;
int Case,flag;
long long ans;
scanf("%d",&Case);
while (Case-->0) {
scanf("%d%d",&n,&m);
init();
for (i=1;i<=n;++i) scanf("%d",val+i);
for (i=1;i<=m;++i) {
scanf("%d%d%d",&x,&y,&z);
}
if (0 == n || 1 == n) {
printf("0\n");
continue;
}
if (0 == m) {
continue;
}
dijkstra(1);
flag = 1;
ans = 0LL;
for (i=2;i<=n;++i) {
if (INF == dis[i]) {
flag = 0;
break;
}
ans += dis[i] * val[i];
}
if (!flag) {
} else {
printf("%lld\n",ans);
}
}
return 0;
}

void dijkstra(int src) {
int p;
Node cur;
priority_queue<Node> pq;
memset(vis,0,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
dis[src] = 0;
while (!pq.empty()) pq.pop();
pq.push(Node(src,0));
while (!pq.empty()) {
cur = pq.top();
pq.pop();
if (vis[cur.from]) continue;
vis[cur.from] = 1;
while (p) {
if (dis[edge[p].to] - edge[p].dis > dis[cur.from]) {
dis[edge[p].to] = dis[cur.from] + edge[p].dis;
pq.push(Node(edge[p].to,dis[edge[p].to]));
}
p = edge[p].next;
}
}
}

void init() {
total = 0;
for (int i=0;i<=n;++i)
}

void add_edge(int from,int to,int dis) {
++total;
edge[total].to = to;
edge[total].dis = dis;
}

bool operator < (const Node &a,const Node &b) {
return a.dis > b.dis;
}


• 本文已收录于以下专栏：

## POJ_3013_Big Christmas Tree(最短路)

• jhgkjhg_ugtdk77
• 2015年04月19日 21:17
• 487

## Big Christmas Tree

Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a b...
• wang2534499
• 2015年08月19日 16:44
• 571

## POJ Big Christmas Tree（基础最短路）

Big Christmas Tree 题目分析：     叫你构造一颗圣诞树，使得 (sum of weights of all descendant nodes) × (unit pri...
• u010016150
• 2014年09月30日 11:18
• 649

## POJ 3013 Big Christmas Tree 最短路

• huanghongxun
• 2016年03月03日 16:55
• 385

## poj-3013-Big Christmas Tree-求最短路

• rowanhaoa
• 2014年03月31日 17:13
• 1052

## POJ3013 Big Christmas Tree

POJ3013 Big Christmas Tree
• NNDXNM
• 2015年05月09日 18:55
• 538

## Symbols of Christmas -- 圣诞节的象征（一）

Symbols of Christmas Discover why stores and churches put up all those decorations very year. ...
• jcx5083761
• 2014年08月27日 10:34
• 2303

## B - Christmas Spruce

B. Christmas Spruce time limit per test 1 second memory limit per test 256 megabytes ...
• memory_qianxiao
• 2018年01月10日 00:09
• 198

## PKU 3013 Big Christmas Tree 最短路 spfa

• peter_zhu01
• 2016年05月03日 20:01
• 296

## codeforces 913 B. Christmas Spruce 【思维】

B. Christmas Spruce time limit per test1 second memory limit per test256 megabytes Consider a roo...
• nobleman__
• 2018年01月09日 01:56
• 193

举报原因： 您举报文章：POJ_3013_Big Christmas Tree 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)