SPFA: Invitation Cards

有向图的spfa模板 spfa模板,双向图的,但是里面也有介绍改成单向图的。 实际是freepascal的,但是没有分类,只好放到C/C++分类里了,希望能帮到大家 立即下载
G - Invitation Cards
Time Limit:8000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

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 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


贴模板水过,先求正向图,再求反向图,再相加即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>

using namespace std;

const long long INF = 1LL<<63-1;

int n, m, ww;
long long d[1000010];
int cnt;

struct node{
    int to;
    int next;//每次插边的时候是插在已插的边前面,所以已插的边是新边的next
    int weight;
}e[1000010];

int lastshow[1000010];
bool inqueue[1000010];
queue<int>q;

void insert(int a, int b, int w){
    e[++cnt].to = b;
    e[cnt].weight = w;
    e[cnt].next = lastshow[a];
    lastshow[a] = cnt;//lastshow记录一个点上次作为起点的边的序号,所以一条边的next是它的起点的lastshow值
}

bool spfa(){
    q.push(1);
    while(!q.empty()){
        int x = q.front();
        q.pop();
        inqueue[x] = false;
        int id = lastshow[x];
        while(id != -1){
            if(d[x] < INF && d[e[id].to] > e[id].weight + d[x]){
                d[e[id].to] = e[id].weight + d[x];
                if(!inqueue[e[id].to]){
                    inqueue[e[id].to] = true;
                    q.push(e[id].to);
                }
            }
            id = e[id].next;
        }
    }
    return false;
}

void init(){
    int i;
    for(i = 1; i <= n; i ++){
        lastshow[i] = -1;
        inqueue[i] = false;
    }
    for(i = 1; i <= n; ++ i)
        d[i]=INF;
    d[1]=0;
    cnt=0;
    while(!q.empty()){
        q.pop();
    }
}

int a[1000010], b[1000010], w[1000010];

int main(){
    int f;
    int i;
    long long sum;
    cin >> f;
    while(f -- ){
        sum = 0;
        cnt = 0;
        scanf("%d %d", &n, &m);
        init();
        for(i = 1; i <= m; i ++){
            scanf("%d %d %d", &a[i], &b[i], &w[i]);
            insert(a[i], b[i], w[i]);
        }
        spfa();
        for(i = 2; i <= n; i ++)
            sum += d[i];
        init();
        for(i = 1; i <= m; i ++)
            insert(b[i], a[i], w[i]);
        spfa();
        for(i = 2; i <= n; i ++)
            sum += d[i];
        cout << sum << endl;
    }
    return 0;
}


2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛-Shuffle Card Shuffle Card Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 507Accepted Submission(s): 257 题目链接:http://acm.hdu.edu.cn/showproblem.ph... 阅读详情

相关推荐

hpu18级第一次训练赛

D - Wave 题目链接:https://vjudge.net/contest/341543#problem/D 题意:定义 波为 至少有两个元素的字符串的奇数位和偶数位分别是相同的,而且奇偶位不相同,求给定序列的子序列是波的最大长度 思路:开一个二维dp数组,dp [ i ] [ j ] 表示奇数位是i 偶数位是j的波的长度,全部初始化为0; 遍历一遍字符串,假设当前字符是a,这个字符可以组成...

qq_45288870的博客 260

洛谷 P1629 spfa + 正难则返(反向建图)

题意:n个点,m条有向边,求从点1到各点再从各点回到点1的最小路程和 从点1到个点——单源最短路 从各点回到点1——由于是单向边,所以去和回所经过的路程可能不一样且都回到1——反向建图,重新转化成1到各点——单源最短路 #include<stdio.h> #include<string.h> struct rec { int x; int y; ...

Eirlys的博客 529

【最短路】 HDOJ 1535 Invitation Cards

建正图,反图。然后用dijstra搜两边就行了。。 #include #include #include #include #include #include #include #include #include #include #include #include #include #define maxn 1000005 #d

yysys 553

Invitation CardsSPFA+前向星反向建图)

题意:一堆人从1出发,到其他的点,然后从其他的点再回来,最短距离。和之前那个牛聚会题目一样,两次spfa。Silver Cow Party 由于这题的数据已经不能用邻接矩阵来储存,所以矩阵逆置就不能用了,所以需要用前向星进行反向建图,这里用到的也是一个很巧妙地方法。 我们需要在用结构体存边的信息的时候,多定一个边的起点信息,用来后面的反向建图。 #include&lt;iostream&gt; #...

C'est la vie! 275

HDU1535:Invitation Cards(SPFA)

Problem 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, An

ACM!荣耀之路! 3278

POJ1511---Invitation Cards (最短路:邻接表+2次spfa

【题目来源】:https://vjudge.net/problem/POJ-1511 【题意】 一大批志愿者去一大批的公交站点发传单,这里的一大批数量为n, (^ ,^),完了之后呢,还有回到出发点,题中描述出发点为编号1的站点,然后,问题就变成了这样的一个模型: 求从1到其余个点需要的最短路径之和,加上,其余各点到1号点的最短路径之和。 【思路】 两次spfa。但是由于站点的数量较多,可

Crazy 377

Invitation Cards spfa

题意 n个点m条单向边 计算 从1到每一个点的权值(除了1) 和每个点回到1 的权值之和 正向跑一次 反向跑一次即可 #include<bits/stdc++.h> using namespace std; //input by bxd #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define rep...

weixin_30695195的博客 153

Invitation CardsSPFA

Invitation CardsSPFA) 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 wit

Anthcony的博客 425

POJ1511 SPFA Invitation Cards

POJ1511题目连接题意代码 题目连接 [POJ1511](https://vjudge.net/problem/POJ-1511) 题意 就是说从节点1出发然后问从节点1出发到其它各点的花费与其它各点到节点1之和最小 SPFA可以之结果,但是如果用kuangbin的板子,采用向量(邻接表)的话会超时,应当用链式前向星来作为 数据结构 如果链式前向星不太清楚,可以参考以下博客 [链式前向星](https://blog.csdn.net/weixin_44758733/article/details/10

Bran 199

Invitation Cardsspfa

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 Com...

海马有力量的博客 303

poj 1511 Invitation Cards(spfa)

题意:有个N个点的有向图,求从点1到其他各点,然后从其他各点回到点1的最小距离。 思路:题意很容易理解,但是这题给的时间有点紧,不能用Dijksra 和Bellman_ford来做,只能有Spfa,先求出点1到到其他各点的最短距离,然后将边逆转,再求一下点1到各点的距离,求和就行了。明明思路是对的,但是愣是WA了一上午,不知道哪里错的,将discuss里提到的各种注意都改了,还是不对,...

B218707的博客 206

poj1511 Invitation Cards SPFA

http://poj.org/problem?id=1511 先一次用SPFA求早上从定点1到各点的最短路径和,再对其求反向图,用SPFA求一次顶点1到各顶点的最短路径和,这个值其实就是晚上各点回到顶点1的最短路径和了。 #include <cstdio> #include <cstring> #include <algorithm> #include &...

skykone1的博客 159

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 the n

m0_56740685的博客 190

poj1511 - Invitation Cards

想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                                      转载请注明出处:http://blog.csdn.net/wangjian8006 题目大意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返源点1)

贰圣 5149

Invitation Cards

http://acm.hdu.edu.cn/showproblem.php?pid=1535 题意:有编号为1~p的站点,有Q条公交路线,公交车路线只能从起点走到对应的终点,而且是单向的,每条路线有其对应的车费。早上有p个人从站点1出发,到达与其编号对应的站点(编号为i的人到达编号为i的站点),晚上从各自站点回到1站点,问他们来回的最小费用之和。   思路:很水的题。 一个是求源点到其他点

967

poj 1511 Invitation Cards spfa

#include<stdio.h> #include<iostream> #include<stdio.h> #include<stdlib.h> #include<queue> #include<string.h> #define en '\n' #define ll long long #define mem(a,b) ...

fnq9999的博客 162

POJ 1511 Invitation CardsSPFA

http://poj.org/problem?id=1511POJ 1511 Invitation Cards大意:求有向图中从源点到每个节点的往返距离和分析: 1.根据题意建立两幅图,分别对应于正向图和反向图 2.分别对正向图和反向图求从源点即题中的1结点出发求単源最短路径, 3. 由2易得,正向图中求得的dis[i]为s到i的最短路,反向图中求得的dis[i]为i到s的最...

a48634331的博客 158

Invitation Cards POJ-1511 (spfa)

题目链接:Invitation Cards 题意:   给出一张有向图,现在要求从1到其他所有的结点的最小路径和与从所有其他结点到1的最小路径和之和。 题解:   求最小路径可以用SPFA来求解。从1到其他结点的正向跑一遍SPFA就可以求出来了,要求其他结点到1的最小路径则可以反向建图跑一边SPFA。但是这里面很奇怪的是,如果正向和反向建图开两个vector就会TLE,开一个就不...

163
上一篇: Floyd: Arbitrage
下一篇: 筛法素数表
lethic
博客等级 码龄14年 59粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值