POJ 3013 Big Christmas Tree SPFA模版

我靠,这么水的题目让我那么纠结,直到A了也不知道数据范围为多大。

View Code
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#define maxn 50005
using namespace std;

int cost[maxn],head[maxn];
bool vis[maxn];
long long dis[maxn];

struct node
{
    int v,w,next;
}list[maxn*2];

int n, m, tot;
void add(int s,int t,int w)
{
    list[tot].v=t;
    list[tot].w=w;
    list[tot].next=head[s];
    head[s]=tot++;
}
void spfa(int src, int dest)
{
    int i,j,to,u,e;
    queue<int> q;
    for(i=1;i<=n;i++)
    dis[i]=-1,vis[i]=0;

    dis[src]=0;
    vis[src]=1;
    q.push(src);
    while(!q.empty())
    {
        u=q.front();q.pop();vis[u]=0;
        for(i=head[u];i!=-1;i=list[i].next)
        {
            e=list[i].v;
            if(dis[e]==-1||dis[e]>dis[u]+list[i].w)
            {
                 dis[e]=dis[u]+list[i].w;
                 if(!vis[e])
                 {
                     vis[e]=1;
                     q.push(e);
                 }
            }
        }
    }
}
int main()
{
    int cas,x,y,w;
    int i,j;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d %d",&n,&m);
        for(i=1;i<=n;i++)
        scanf("%d",&cost[i]);
        tot=0;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            scanf("%d %d %d",&x,&y,&w);
            add(x,y,w);
            add(y,x,w);
        }
        spfa(1, n);
        long long ans=0;
        bool flag=1;
        for(i=1;i<=n;i++)
        {

            if(dis[i]==-1)
            {
                flag=0;break;
            }
            ans+=dis[i]*cost[i];
        }
        if(!flag)printf("No Answer\n");
        else printf("%I64d\n",ans);
    }
    return 0;
}

 INF要开大一点啊

View Code
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

const int maxn = 50000 + 5;
const unsigned long long INF = 9999999999LL;
int cost[maxn];       
int tot;
int head[maxn];
long long dis[maxn];
bool vis[maxn];
int n, m;

struct node
{
    int v;
    int w;
    int next;
}list[maxn * 2];

void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
    memset(vis, false, sizeof(vis));
    for (int i=0; i<=n; i++)dis[i] = INF;
}

void spfa(int s)
{
    int u, v, w;
    queue<int> q;
    q.push(s);dis[s] = 0;
    vis[s] = true;
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        vis[u] = false;
        for (int k=head[u]; k!=-1; k=list[k].next)
        {
            v = list[k].v;
            w = list[k].w;
            if (dis[u] + w < dis[v])
            {
                dis[v] = dis[u] + w;
                if (!vis[v])
                {
                    q.push(v);
                    vis[v] = true;
                }
            }
        }
    }
}
void add(int s, int t, int w)
{
    list[tot].v = t;
    list[tot].w = w;
    list[tot].next = head[s];
    head[s] = tot++;
}

int main()
{
    int i, j;
    int x, y, w;
    int cas;
    scanf("%d",&cas);
    while (cas--)
    {
        scanf("%d%d",&n,&m);
        init();
        for (i=1; i<=n; i++)
           scanf("%d",&cost[i]);
        while(m--)
        {
            scanf("%d%d%d", &x, &y, &w);
            add(x, y, w);
            add(y, x, w);
        }
        spfa(1);
        long long sum = 0;
        bool flag = true;
        for (i=2; i<=n; i++)
        {
            if (dis[i] == INF)
            {
                flag = false;
                break;
            }
            sum += dis[i] * cost[i];
        }
        flag ? printf("%I64d\n", sum) : printf("No Answer\n");
    }
    return 0;
}

 

 

posted @ 2012-07-24 21:33  To be an ACMan  Views(190)  Comments(0)    收藏  举报