POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图

【题目描述】

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 the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery.

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where ‘X’ denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan.

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
Sample Input

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output

46
210

【题目分析】
求的是从源点到所有点以及所有点到源点的路程和最小,对于源点到所有点的路程和最小我们很自然的想到Dijkstra,因为数据规模比较大需要用邻接表,我用的是前向星的方式进行储存,虽然用vector也可以,但是听说vector更有可能MLE(因为STL中vector实际要多用一倍的空间)和TLE(因为vector的一些挺简单的操作耗时比一般数组要长)。
但是对于其他所有的点到源点的路程和最小我们直接用Dijkstra显然是不行的,而用Floyed复杂度太高。
这个时候就需要一点点脑洞想到反向建图(图论中反向建图经常会碰到,很多正面看起来难以解决的问题反过来看就会变的简单起来),反向建图后我们再跑一遍Dijkstra,这时候的路径方向全部反过来就是实际上其他所有点到源点的路径。然后再求和就可以啦。
前向星进行储存的时候要注意head数组(保存每个点第一条边的数组)如果初始化为0,边一定要从1开始,否则无法判断到底是没有边了还是指向第一个边,如果习惯从0开始数边应该将head数组初始化为-1.
将结构体作为优先队列的元素的时候需要进行运算符重载,优先队列默认是使用小于号进行比较的,我们应该对小于号进行重载。如果想要按照某个值从大到小重载后还是小于号,如果想要某个值从小到大应该重载为大于号,这是因为优先队列默认是从大到小的,也就是“重载后值越小”的越在后面。
在对Dijkstra用优先队列优化的时候不要进行初始化,也不要将源点设置为已经访问过,如果设为已经访问过就直接退出了。而将dis数组进行初始化以后我们后面就很有可能没有办法将其他节点放入队列。
【AC代码】

/*
*因为将两个过程分别写出来了显得有点丑,空间也有点大
*我们也可以两次建图,用一套数组就好了
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;

typedef long long ll;
const int MAXN=1e6+5;
struct node
{
    int v,next;
    ll w;
    node(int _v=0,int _next=0,ll _w=0):v(_v),w(_w),next(_next){}
}Edge1[MAXN],Edge2[MAXN];
struct qnode
{
    int x; ll d;
    qnode(int _x=0,ll _d=0):x(_x),d(_d){}
    bool operator <(const qnode &r)const 
    {
        return d>r.d;
    }
}p;
int head1[MAXN],head2[MAXN],tot1,tot2;
bool vis[MAXN];
ll dis1[MAXN],dis2[MAXN],ans;
int n,m;

void AddEdge1(int u,int v,ll w)
{
    tot1++;
    Edge1[tot1].v=v; Edge1[tot1].w=w; Edge1[tot1].next=head1[u];
    head1[u]=tot1;
}

void AddEdge2(int u,int v,int w)
{
    tot2++;
    Edge2[tot2].v=v; Edge2[tot2].w=w; Edge2[tot2].next=head2[u];
    head2[u]=tot2;
}

void Dijkstra1()
{
    memset(vis,0,sizeof(vis));
    memset(dis1,0x3f,sizeof(dis1));
    const int INF=dis1[1];
    int u,v;
    
    // for(int i=head1[1];i;i=Edge1[i].next)
    // {
    //     dis1[Edge1[i].v]=Edge1[i].w;
    // }
    dis1[1]=0;
    //vis[1]=true;
    priority_queue<qnode> q;
    q.push(qnode(1,0));
    while(!q.empty())
    {
        p=q.top(); q.pop();
        if(vis[p.x]) continue;
        vis[p.x]=true;
        u=p.x;
        for(int i=head1[u];i;i=Edge1[i].next)
        {
            v=Edge1[i].v;
            if(!vis[v]  && dis1[v]>dis1[u]+Edge1[i].w)
            {
                dis1[v]=dis1[u]+Edge1[i].w;
                q.push(qnode(v,dis1[v]));
            }
        }
    }
}

void Dijkstra2()
{
    memset(vis,0,sizeof(vis));
    memset(dis2,0x3f,sizeof(dis2));
    const int INF=dis2[1];
    int u,v;
    // for(int i=head2[1];i;i=Edge2[i].next)
    // {
    //     dis2[Edge2[i].v]=Edge2[i].w;
    // }
    dis2[1]=0; 
    //vis[1]=true;
    priority_queue<qnode> q;
    q.push(qnode(1,0));
    while(!q.empty())
    {
        p=q.top(); q.pop();
        if(vis[p.x]) continue;
        vis[p.x]=true;
        u=p.x;
        for(int i=head2[u];i;i=Edge2[i].next)
        {
            v=Edge2[i].v;
            if(!vis[v] && dis2[v]>dis2[u]+Edge2[i].w)
            {
                dis2[v]=dis2[u]+Edge2[i].w;
                q.push(qnode(v,dis2[v]));
            }
        }
    }
}

void test()
{
    for(int i=1;i<=n;i++)
    {
        printf("%lld ",dis1[i]);
    }
    printf("\n");
    for(int i=1;i<=n;i++)
    {
        printf("%lld ",dis2[i]);
    }
    printf("\n");
}

int main()
{
    int T,u,v; ll w;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        tot1=0; tot2=0;
        memset(head1,0,sizeof(head1));
        memset(head2,0,sizeof(head2));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%lld",&u,&v,&w);
            AddEdge1(u,v,w); AddEdge2(v,u,w);
        }
        Dijkstra1(); Dijkstra2();
        //test();
        ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=dis1[i]+dis2[i];
        }
        printf("%lld\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值