poj 1511 spfa水题

poj 1511 Invitation Cards

题目:

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.

题意:

给一个有向图,求其中一个点到其他点的最短路和+其他点到该店的最短路和;

思路:

数据范围比较大,朴素dijstra并不适合,正好学了一下,所以干脆就用spfa解决了。第一次写spfa,其优点在于可以解决带负权边的最短路问题,初始状态是把原点入队,每次取队头更新子节点(类似dijstra),如果某一点被更新了(且不在队列中),就存入队列,当队列为空的时候就结束。与dijstra的区别就是,他会把一个节点更新好多次,因为可能存在负权边使之前找到的最短路更短,(当然这道题没有体现出来)。
只有怎么求其他边到该点的最短路之和把图所有边反向就可以了,在求一边spfa就可以啦!

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stdlib.h>
#define N 1000100
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll ;
struct node
{
    int next,to;
    ll  val;
};
node maps[2][N];//0正图,1反图
int head[2][N];
int cnt[2];
bool vis[N];//判断某节点在不在队列里面
long long val[N];
int n,m;
ll spfa(int t)
{
    memset(vis,0,sizeof(vis));
    memset(val,inf,sizeof(val));
    val[1]=0;//初始化就把原点标为0,其他不用,不然入队后会直接退出。。。
    queue<int>q;
    vis[1]=true;
    q.push(1);
    while(!q.empty())
    {
        int tmp=q.front();
        q.pop();
        vis[tmp]=false;
        for(int i=head[t][tmp];i!=0;i=maps[t][i].next)
        {
            int s=maps[t][i].to;
            if(val[s]>val[tmp]+maps[t][i].val)
            {
                val[s]=val[tmp]+maps[t][i].val;
                if(!vis[s])//如果更新了的节点不在队列中,就入队。
                {
                    q.push(s);
                    vis[s]=true;
                }

            }
        }
    }
    ll ans=0;
    for(int i=2;i<=n;i++)
        ans+=val[i];
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(maps,inf,sizeof(maps));
        memset(cnt,0,sizeof(cnt));
        memset(head,0,sizeof(head));

        int x,y,z;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            maps[0][++cnt[0]].next=head[0][x];
            maps[0][cnt[0]].to=y;
            maps[0][cnt[0]].val=z;
            head[0][x]=cnt[0];
            maps[1][++cnt[1] ].next=head[1][y];
            maps[1][cnt[1] ].to=x;
            maps[1][cnt[1] ].val=z;
            head[1][y]=cnt[1];
        }
        ll ans=spfa(0)+spfa(1);
        printf("%I64d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值