POJ_3013_Big Christmas Tree

原创 2013年12月05日 09:32:39

这道题目TLE了好多次才过,有些点要注意的:

1.图是无向图来的

2.边数组要开10W以上

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

4.当0==n或1==n,输出0,当0==m,输出No Answer;

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


参考代码(spfa):

#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];
Edge head[MAXN];
long long dis[MAXN];
int val[MAXN];
int queue[MAXM];
int total,n,m;

void init();
void add_edge(int from,int to,int dis);
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);
            add_edge(x,y,z);
            add_edge(y,x,z);
        }
        if (0 == n || 1 == n) {
            printf("0\n");
            continue;   
        }
        if (0 == m) {
            printf("No Answer\n");
            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) {
            printf("No Answer\n");
        } 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];
        p = head[cur].next;
        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)
        head[i].next = 0;
}

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


参考代码(dijkstra+priority_queue):

#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];
Edge head[MAXN];
long long dis[MAXN];
int val[MAXN];
int total,n,m;

void init();
void add_edge(int from,int to,int dis);
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);
            add_edge(x,y,z);
            add_edge(y,x,z);
        }
        if (0 == n || 1 == n) {
            printf("0\n");
            continue;   
        }
        if (0 == m) {
            printf("No Answer\n");
            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) {
            printf("No Answer\n");
        } 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;
        p = head[cur.from].next;
        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)
        head[i].next = 0;
}

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

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


POJ_3013_Big Christmas Tree(最短路)

题意:最短路问题,ans += dist[i] * weight[i] 。
  • jhgkjhg_ugtdk77
  • 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
  • wang2534499
  • 2015年08月19日 16:44
  • 571

POJ Big Christmas Tree(基础最短路)

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

POJ 3013 Big Christmas Tree 最短路

求图上根为1的生成树使得各边权*子树点权和最小。 ans=∑(u,v)wu,v×sumvans=\sum_{(u,v)} w_{u,v}\times sum_v 光看这个式子并没有卵用。专门迷惑人...
  • huanghongxun
  • huanghongxun
  • 2016年03月03日 16:55
  • 385

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

题目的意思是让那你建立一颗圣诞树。 圣诞树以1为头结点。 圣诞树每条边的花费为当前边的权值乘以当前边的子树的节点的权值和。 那么就相当于求每个节点乘以节点到根节点的最短路的和。 #includ...
  • rowanhaoa
  • rowanhaoa
  • 2014年03月31日 17:13
  • 1052

POJ3013 Big Christmas Tree

POJ3013 Big Christmas Tree
  • NNDXNM
  • 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
  • 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
  • memory_qianxiao
  • 2018年01月10日 00:09
  • 198

PKU 3013 Big Christmas Tree 最短路 spfa

题意/Description:     Christmas is coming to KCM city. Suby the loyal civilian in KCM city is prepari...
  • peter_zhu01
  • 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__
  • nobleman__
  • 2018年01月09日 01:56
  • 193
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ_3013_Big Christmas Tree
举报原因:
原因补充:

(最多只允许输入30个字)