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;
}


POJ3013 Big Christmas Tree

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

Poj 3013 /hrbust 1419 Big Christmas Tree/昂贵的假花【SPFA+思维】

昂贵的假花 Time Limit: 1000 MS Memory Limit: 65536 K   Total Subm...

POJ 3013 Big Christmas Tree(Dijkstra)

POJ 3013 Big Christmas Tree http://poj.org/problem?id=3013 题意:给你一个无向图,该图的每个点有一个权重,且每条边有一个单位重量开销.现在要你...

poj 3013 Big Christmas Tree spfa

题目大意   要建一棵圣诞树,使得总的花费最小。具体规则是:圣诞树是一颗无向树形图,其中,编号为1的节点为根节点,原始图中每条边具有边权,每个点也有一值权。圣诞树中,各条边的花费是该边权*该边的子树中...

poj 3013 Big Christmas Tree 最短路

Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 180...
  • cyendra
  • cyendra
  • 2013年03月23日 20:50
  • 876

poj3013 Big Christmas Tree 变形的最短路

题目大意:有一棵树,每个点有个权值,每条边有个权值,求这棵树的整体权值,如果不能组成一棵树,就输出No Answer 思路:借鉴百度到的分析,最小总花费=每条边(u,v)*v的子树中各结点的重量 ...

最短路SPFA——Big Christmas Tree ( POJ 3013 )

题目链接: http://poj.org/problem?id=3013 分析: 给出N个点和M条边,每个点有它的权值,每条边也有它的权值,建造这条边的花费等于这条边的权值*另一端端点的子树所包含...
  • FeBr2
  • FeBr2
  • 2016年08月28日 17:42
  • 307

POJ 3013 Big Christmas Tree【最短路变形,DIjkstra堆优化+spfa算法】

Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 230...

POJ_3013_Big Christmas Tree(最短路)

题意:最短路问题,ans += dist[i] * weight[i] 。

POJ3013 - Big Christmas Tree - 最短路变形

1.题目描述: Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Sub...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ_3013_Big Christmas Tree
举报原因:
原因补充:

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