Invitation Cards POJ - 1511(spfa算法)

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

题意:一张有向图,求从点1开始到其他各点的最短路权值和加上从其他各点到点1的最短路权值和。

补充一下spfa算法(从很好的博客上学习到的):SPFA是一种单源最短路径算法,我们记源点为S,由源点到达点i的“当前最短路径”为D[i],开始时将所有D[i]初始化为无穷大,D[S]则初始化为0。算法所要做的,就是在运行过程中,不断尝试减小D[]数组的元素,最终将其中每一个元素减小到实际的最短路径。

过程中,我们要维护一个队列,开始时将源点置于队首,然后反复进行这样的操作,直到队列为空:

(1)从队首取出一个结点u,扫描所有由u结点可以一步到达的结点,具体的扫描过程,随存储方式的不同而不同;

(2)一旦发现有这样一个结点,记为v,满足D[v] > D[u] + w(u, v),则将D[v]的值减小,减小到和D[u] + w(u, v)相等。其中,w(u, v)为图中的边u-v的长度,由于u-v必相邻,所以这个长度一定已知(不然我们得到的也不叫一个完整的图);这种操作叫做松弛。
引用内容
松弛操作的原理是著名的定理:“三角形两边之和大于第三边”,在信息学中我们叫它三角不等式。所谓对i,j进行松弛,就是判定是否d[j]>d[i]+w[i,j],如果该式成立则将d[j]减小到d[i]+w[i,j],否则不动。

(3)上一步中,我们认为我们“改进了”结点v的最短路径,结点v的当前路径长度D[v]相比于以前减小了一些,于是,与v相连的一些结点的路径长度可能会相应地减小。注意,是可能,而不是一定。但即使如此,我们仍然要将v加入到队列中等待处理,以保证这些结点的路径值在算法结束时被降至最优。当然,如果连接至v的边较多,算法运行中,结点v的路径长度可能会多次被改进,如果我们因此而将v加入队列多次,后续的工作无疑是冗余的。这样,就需要我们维护一个bool数组vis[],来记录每一个结点是否已经在队列中。我们仅将尚未加入队列的点加入队列。
总结来说这个算法的主要目的就是更新d[j]>d[i]+w[i,j]。并将更新过的符合要求的路径拿队列储存。

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
#define N 1000010
#define INF 0x3f3f3f3f

typedef long long ll;
ll d[N];
int n,tot;
int head[N];
bool ins[N];
struct edge
{
    int u,v,w,next;
}e[N],tt[N];//e数组储存正向图,tt数组储存反向图。

void add(int u ,int v ,int w , int k)
{
    e[k].u = u;
    e[k].v = v;
    e[k].w = w;
    e[k].next = head[u];
    head[u] = k;
}

ll spfa_stack()
{
    queue<int>sta;
    memset(d,INF,sizeof(d));
    memset(ins,false,sizeof(ins));
    sta.push(1);
    d[1] = 0;
    ins[1] = true;
    sta.push(1);
    while(!sta.empty())
    {
        int u = sta.front();
        sta.pop();
        ins[u] = false;
        for(int k=head[u]; k!=-1; k=e[k].next)
        {
            int v =e[k].v;
            int w = e[k].w;
            if( d[u] + w < d[v] )
            {
                d[v] = d[u] + w;
                if(!ins[v])
                {
                    ins[v] = true;
                    sta.push(v);
                }
            }
        }
    }
    ll res = 0;
    for(int i=1; i<=n ; i++)
        res += d[i];
    return res;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&tot);

        memset(head,-1,sizeof(head));
        for(int i=0; i<tot; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            tt[i].u = u; tt[i].v = v; tt[i].w = w;//给反向图赋值
            add(u,v,w,i);
        }
        ll suma = spfa_stack();

        memset(head,-1,sizeof(head));
        for(int i=0; i<tot; i++)
            add(tt[i].v , tt[i].u , tt[i].w , i);
        ll sumb = spfa_stack();

        printf("%lld\n",suma+sumb);
    }
    return 0;
}

方法二:

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
#define maxn 1000010
#define inf 0x3f3f3f3f
int n, m;
int head[2][maxn], vis[maxn];
long long sum, dis[maxn];  
struct node
{
    int to, w, next;
}edge[2][maxn];//正反两个图同一个二维数组储存
void SPFA(int a) 
{
    int v, i, b;
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++)
        dis[i] = inf;
    queue<int>q;
    q.push(1);
    vis[1] = 1;
    dis[1] = 0;
    while(!q.empty())
    {
        v = q.front();
        q.pop();
        vis[v] = 0;
        for(i = head[a][v]; i != -1; i = edge[a][i].next)
        {
            b = edge[a][i].to;
            if(dis[b] > dis[v]+edge[a][i].w)
            {
                dis[b] = dis[v]+edge[a][i].w;
                if(!vis[b])
                {
                    vis[b] = 1;
                    q.push(b);
                }
            }
        }
    }
}
int main()
{
    int Case, a, b, w, i;
    cin>>Case;
    while(Case--)
    {
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++)
        {
            head[0][i] = -1;
            head[1][i] = -1;
        }
        // 用静态邻接表( 又称为链式前向星 )存储,效率很高。
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &a, &b, &w);
            edge[0][i].to = b;
            edge[0][i].w = w;
            edge[0][i].next = head[0][a];
            head[0][a] = i;
            // 构造反图
            edge[1][i].to = a;
            edge[1][i].w = w;
            edge[1][i].next = head[1][b];
            head[1][b] = i;
        }
        sum = 0;
        SPFA(0);
        for(int i = 1; i <= n; i++)
            sum += dis[i];
        SPFA(1);
        for(int i = 1; i <= n; i++)
            sum += dis[i];
        printf("%lld\n", sum);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Wedding invitation H5是一种免费的电子邀请函平台,可以用来制作婚礼邀请函。H5是指使用HTML5技术,可以在浏览器中直接运行的网页。 Wedding invitation H5提供了各种模板供用户选择,用户只需根据自己的需求修改文字内容、配图等,即可制作出个性化的婚礼邀请函。 Wedding invitation H5的免费性质使其成为了许多新人的首选。相对于传统的印刷邀请函,H5邀请函具有许多优点。首先,H5邀请函制作和分发的成本较低,省去了印刷和邮寄的费用。其次,H5邀请函的传递速度更快,可以通过电子邮件、微信、朋友圈等方式一键分享给亲朋好友。此外,H5邀请函还支持更多的互动功能,如音乐、短视频、交互式地图等,能够为婚礼增添更多的亮点。 使用Wedding invitation H5制作婚礼邀请函非常简便。用户只需打开平台,选择心仪的模板,根据平台提供的编辑工具修改相关内容,如添加婚礼日期、地点、时间等。用户还可以上传自己的照片、视频以及音乐,使邀请函更加个性化。完成编辑后,用户可以预览效果,确认无误后即可保存和分享。Wedding invitation H5还提供了二维码下载和打印邀请函的选项,方便那些更喜欢传统方式的亲友。 总之,Wedding invitation H5的免费提供给了新人制作个性化、高效传达的婚礼邀请函的机会,并且使用简单方便。它在传统邀请函的基础上融入了现代科技,更加贴合了年轻人的需求。如果你正在准备婚礼并需要制作邀请函,Wedding invitation H5是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值