spfa-spfa(deque)-poj1511 - Invitation Cards

51 篇文章 0 订阅
26 篇文章 0 订阅

题意:

从1点出发去所有点,并且从所有点返回1号点。询问总花费

思路:

两遍spfa,正反存边。

poj3268 -- Silver Cow Party很像,一样的思路

第一次spfa是从1到所有点的花费。
第二次spfan是从所有点到1的花费,只要把边逆过来就可以了

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <cstring>
#define inf 1000000010
#define maxn 1000010
using namespace std;
typedef long long ll;
ll n,m,x;

int head[maxn];
int headn[maxn];
ll cnt=0,cot=0;
struct node
{
    ll to,val,next;
}edge[maxn],edgen[maxn];
void add(ll u,ll v,ll w)
{
    edge[++cnt].to=v;
    edge[cnt].val=w;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
void addn(ll u,ll v,ll w)
{
    edgen[++cot].to=v;
    edgen[cot].val=w;
    edgen[cot].next=headn[u];
    headn[u]=cot;
}
ll  vis[maxn];
ll  dist[maxn];
ll ans[maxn];
void spfa()
{
    for(int i=1;i<=n;i++)
        vis[i]=0,dist[i]=inf;
    vis[1]=1;
    dist[1]=0;
    ll cur=1;
    ll maxx=0;
    queue<int >q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        vis[cur]=0;
        for(int i=head[cur];i!=-1;i=edge[i].next)
        {
            int id=edge[i].to;
            if(dist[id]>edge[i].val+dist[cur])
            {
                dist[id]=edge[i].val+dist[cur];
                if(!vis[id])
                {
                    vis[id]=1;
                    q.push(id);

                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        ans[i]=dist[i];
       // cout<<ans[i]<<" ";
    }
}
void spfan()
{
    for(int i=1;i<=n;i++)
        vis[i]=0,dist[i]=inf;
    vis[1]=1;
    dist[1]=0;
    ll cur=1;
    ll maxx=0;
    queue<int >q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        vis[cur]=0;
        for(int i=headn[cur];i!=-1;i=edgen[i].next)
        {
            int id=edgen[i].to;
            if(dist[id]>edgen[i].val+dist[cur])
            {
                dist[id]=edgen[i].val+dist[cur];
                if(!vis[id])
                {
                    vis[id]=1;
                    q.push(id);

                }
            }
        }
    }
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
        sum=sum+dist[i]+ans[i];
    }
    cout<<sum<<endl;
}
int main()
{
    int T;

    scanf("%d",&T);
    while(T--)
    {
    scanf("%lld%lld",&n,&m);
    memset(head,-1,sizeof(head));
    memset(headn,-1,sizeof(headn));
    for(int i=1;i<=m;i++)
    {
        ll x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        add(x,y,z);
        addn(y,x,z);
    }
    spfa();
    spfan();
    }
    return 0;
}
 

看到网上可以用两列来存,大大缩小了代码长度,然而记得有个代码说spfa的优化是deque 然而试了试并不快好吧?

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <cstring>
#define inf 1000000010
#define maxn 1000010
using namespace std;
typedef long long ll;
ll n,m,x;
ll sum;
int head[2][maxn];
ll cnt=0,cot=0;
struct node
{
    ll to,val,next;
}edge[2][maxn];
void add(ll u,ll v,ll w)
{
    edge[0][++cnt].to=v;
    edge[0][cnt].val=w;
    edge[0][cnt].next=head[0][u];
    head[0][u]=cnt;
}
void addn(ll u,ll v,ll w)
{
    edge[1][++cot].to=v;
    edge[1][cot].val=w;
    edge[1][cot].next=head[1][u];
    head[1][u]=cot;
}
ll  vis[maxn];
ll  dist[maxn];
ll ans[maxn];
void spfa(int cap)
{
    for(int i=1;i<=n;i++)
        vis[i]=0,dist[i]=inf;
    vis[1]=1;
    dist[1]=0;
    ll cur=1;
    ll maxx=0;
    deque<int >q;
    q.push_back(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop_front();
        vis[cur]=0;
        for(int i=head[cap][cur];i!=-1;i=edge[cap][i].next)
        {
            int id=edge[cap][i].to;
            if(dist[id]>edge[cap][i].val+dist[cur])
            {
                dist[id]=edge[cap][i].val+dist[cur];
                if(!vis[id])
                {

                    vis[id]=1;
                    if(!q.empty()&&q.front()>id)
                        q.push_front(id);
                    else
                        q.push_back(id);
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        sum+=dist[i];
    }
}

int main()
{
    int T;

    scanf("%d",&T);
    while(T--)
    {
    sum=0;
    scanf("%lld%lld",&n,&m);
    memset(head,-1,sizeof(head));
    for(int i=1;i<=m;i++)
    {
        ll x,y,z;
        scanf("%lld%lld%lld",&x,&y,&z);
        add(x,y,z);
        addn(y,x,z);
    }
    spfa(0);
    spfa(1);
    cout<<sum<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值