POJ 1511 Invitation Cards 正反SPFA

题意:学生从A站到B站花费C元,将学生每天从‘1’号站送往其它所有站,再从所有站接回到‘1’号站,问着个过程最小花费是多少。

思路:因为数据很大所以要用SPFA,因为不仅要从1点连接各个点还要从各个点返回一点,所以需要正邻接表和逆邻接表。然后正反各跑一次SPFA,值得注意的是因为数据很大,要将INF定位0xffffffff。

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<queue>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cmath>

#define INF 0xffffffff
#define MAX 1000005

using namespace std;

int vis[MAX],a[MAX],b[MAX],k,n,m;

long long dist[MAX];

struct node
{
    int u,v,nextgo,nextback;
    long long w;
} Map[MAX];

void Init()
{
    int i,j;

    memset(vis,0,sizeof(vis));

    for(i=0; i<MAX; i++)
    {
        a[i]=-1;
        b[i]=-1;
        dist[i]=INF*100;
    }
}

void Add(int u,int v,int w)
{
    Map[k].u=u;
    Map[k].v=v;
    Map[k].w=w;
    Map[k].nextgo=a[u];//正向建图
    Map[k].nextback=b[v];//逆向建图
    a[u]=k;
    b[v]=k++;
}

long long gospfa()
{
    long long sum=0;

    queue<int>Q;
    int start=1,i;
    vis[start]=1;
    dist[start]=0;
    Q.push(start);

    while(!Q.empty())
    {
        start=Q.front();
        Q.pop();
        vis[start]=0;

        for(i=a[start]; i!=-1; i=Map[i].nextgo)
        {
            int v=Map[i].v;
            if(dist[v] > dist[start]+Map[i].w)
            {
                dist[v]=dist[start]+Map[i].w;

                if(!vis[v])
                {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }

    for(i=1; i<=n; i++)
    {
        sum+=dist[i];
    }

    return sum;
}

long long backspfa()
{
    long long sum=0;

    queue<int>Q;
    int start=1,i;
    vis[start]=1;
    dist[start]=0;
    Q.push(start);

    while(!Q.empty())
    {
        start=Q.front();
        Q.pop();
        vis[start]=0;

        for(i=b[start]; i!=-1; i=Map[i].nextback)
        {
            int u=Map[i].u;
            if(dist[u] > dist[start]+Map[i].w)
            {
                dist[u]=dist[start]+Map[i].w;

                if(!vis[u])
                {
                    vis[u]=1;
                    Q.push(u);
                }
            }
        }
    }

    for(i=1; i<=n; i++)
    {
        sum+=dist[i];
    }

    return sum;
}

int main()
{
    int T,i,j,u,v;
    long long w,ans;

    scanf("%d",&T);

    while(T--)
    {
        ans=0;
        k=1;

        scanf("%d%d",&n,&m);

        Init();

        for(i=1; i<=m; i++)
        {
            scanf("%d%d%lld",&u,&v,&w);

            Add(u,v,w);
        }

        ans+=gospfa();

        for(i=0;i<MAX;i++)
            dist[i]=INF;
        memset(vis,0,sizeof(vis));

        ans+=backspfa();

        printf("%lld\n",ans);
    }

    return 0;
}

时间: 2024-10-05 14:36:52

POJ 1511 Invitation Cards 正反SPFA的相关文章

(简单) POJ 1511 Invitation Cards,SPFA。

Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all

poj 1511 Invitation Cards (spfa+邻接表)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19527   Accepted: 6375 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

POJ 1511 Invitation Cards(逆向思维 SPFA)

Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all

POJ 1511 Invitation Cards (最短路)

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 19215   Accepted: 6311 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

POJ 1511 Invitation Cards

题目来源:http://poj.org/problem?id=1511 题目很长,花了不少时间才理解题意,目的就是为了求出来回两次最小路径(即为本题的差旅费)之和, 第一次从CCS(1)出发到各个点路径最小,SPFA算法没得说,回来时终点是确定的都是CCS(1),相当于把路 径反过来,即把有向图去反方向,又是从1出发到各个点路径最小,再用一个SPFA.注意ans要用long long 不然也WA,这个地方WA了好几次,虽然更改后AC了,但还是不明白,题目明明写了smaller than 1000

[2016-04-05][POJ][1511][Invitation Cards]

时间:2016-04-05 12:57:22 星期二 题目编号:[2016-04-05][POJ][1511][Invitation Cards] 题目大意:给定一个有向图,从点1出发,分别到各个站点后,又回到点1,问最少需要多少车费, 分析: 从1跑一次最短路,然后矩阵转置,再跑一次最短路,两次求和 这里不能用邻接矩阵保存,所以改成邻接表,然后矩阵转置的操作变成重新加一次边 遇到的问题:用vector存图超时,改用数组实现 #include <queue> #include <algo

HDU 1535 &amp;&amp; POJ 1511 Invitation Cards (SPFA 模板 + 反向建图)

Invitation Cards HDU: Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) POJ: Time Limit: 8000 MS     Memory Limit: 262144 K       Problem Description In the age of television, not many people attend theater performa

POJ 1511 Invitation Cards 【最短路,spfa算法,Dijkstra算法堆优化】

Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 25219   Accepted: 8346 Description In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They wan

DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. POJ 3268 //#include <bits/stdc++.h> #include <cstdio> #include <queue> #include <algorithm> #include <cstring> using namespace