SPFA: Invitation Cards

图论技巧之反向建图 (P1629题解) 对于单源最短路径无论是迪杰斯特拉还是SPFA,都只可以求出一个点到其他各它n-1个点的最短路径,但是假如现在需要求n-1个点到达某个点的最短路径一般会采用floyd算法,但是floyd算法的复杂度是On3因此很有可能会超时,因此此时可以采用反向建图,即边反向。对于邻接矩阵反向建图对于邻接表反向建图将原本add(a,b,c)变成add(b,a,c)即可,但是由于假如题目需要既有正向又有反向此时我们可以通过设置一个较大的数n,然后这样可以不印象正常建表。 阅读详情
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;
}


Java 处理地理信息数据[DEM TIF文件数据获取高程] EPSG 是一个国际标准,用于唯一标识地理坐标系和投影坐标系。 阅读详情

相关推荐

Chronos:学习时间序列的大语言模型(论文解读)

《Chronos:学习时间序列的大语言模型》论文翻译

qq_20144897的博客 9134

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

龍丶逸的博客 3万+

FMCW雷达基本原理(通俗易懂)篇2

FMCW雷达(Frequency Modulated Continuous Wave Radar)是一种使用频率调制连续波的雷达系统。它使用一个发射器发射一个连续波,但是这个连续波的频率会在一定的范围内不断变化。当这个连续波遇到一个目标物体并被反射回来时,接收器会检测到这个反射波的频率变化,并计算出目标物体的距离和速度等信息。FMCW雷达的原理可以理解为:1.发射器发射一个连续波,这个波的频率会从一个固定的起始频率开始,逐渐变化至一个固定的终止频率。

研二小学鸡的博客 7702

hpu18级第一次训练赛

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

qq_45288870的博客 261

OpenCV 开闭运算

开运算和闭运算是将腐蚀和膨胀按照一定的次序进行处理。 但这两者并不是可逆的,即先开后闭并不能得到原来的图像。 开运算 开运算是先腐蚀后膨胀,其作用是:分离物体,消除小区域。特点:消除噪点,去除小的干扰块,而不影响原来的图像。 闭运算 闭运算与开运算相反,是先膨胀后腐蚀,作用是消除/“闭合”物体里面的孔洞,特点:可以填充闭合区域。 cv.morphologyEx(img, op, kernel) img: 要处理的图像 op: 处理方式:若进行开运算,则设为cv.MORPH_OPEN,若进行闭

꧁༺ʚvv༒catɞ༻꧂的博客 952

【工业级视觉实战】海康威视SDK + Python + YOLOv11:零延迟目标追踪系统落地指南

要在 Python 中调用海康 DLL,首先得把 C++ 的结构体 1:1 翻译过来。这是很多开发者踩坑最深的地方(指针错误、内存溢出)。import sys# 根据操作系统加载库else:# 定义设备信息结构体 (对应 C++ LPNET_DVR_DEVICEINFO_V30)("sSerialNumber", ctypes.c_byte * 48), # 序列号("byAlarmInPortNum", ctypes.c_byte), # 报警输入个数。

BUG猿的博客 235

图的遍历--反向建图

dfs算法 + 反向建图

山中一扶苏的博客 837

Xilinx平台SRIO介绍(一)RapidIO协议介绍

菜鸟到老鸟:会用工具; 老鸟到大佬:深入理解原理。 ——FPGA大叔·沃自己硕得

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 1万+

洛谷P1629邮递员送信-Dji-优先队列-双向建图

题目描述 有一个邮递员要送东西,邮局在节点 11。他总共要送 n-1n−1 样东西,其目的地分别是节点 22 到节点 nn。由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有 mm 条道路。这个邮递员每次只能带一样东西,并且运送每件物品过后必须返回邮局。求送完这 n-1n−1 样东西并且最终回到邮局最少需要的时间。 输入格式 第一行包括两个整数,nn 和 mm,表示城市的节点数量和道路数量...

qq_41581913的博客 482

智慧社区:安防系统解决方案

智慧社区:安防系统解决方案

s13166803785的博客 436

AI上推荐 之 隐语义模型(LFM)和矩阵分解(MF)

1. 前言 随着信息技术和互联网的发展, 我们已经步入了一个信息过载的时代,这个时代,无论是信息消费者还是信息生产者都遇到了很大的挑战: 信息消费者:如何从大量的信息中找到自己感兴趣的信息? 信息生产者:如何让自己生产的信息脱颖而出, 受到广大用户的关注? 为了解决这个矛盾, 推荐系统应时而生, 并飞速前进,在用户和信息之间架起了一道桥梁,一方面帮助用户发现对自己有价值的信息, 一方面让信息能够展现在对它感兴趣的用户前面。 推荐系统近几年有了深度学习的助推发展之势迅猛, 从前深度学习的传统推荐模型(协

Miracle8070 1万+

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!荣耀之路! 3281

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

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

Crazy 377

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

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 Cards(SPFA)

Invitation Cards(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 wit

Anthcony的博客 426

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 200

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

海马有力量的博客 307

poj 1511 Invitation Cards(spfa)

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

B218707的博客 207

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的博客 191

poj1511 - Invitation Cards

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

贰圣 5152

Invitation Cards

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

969

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的博客 164

POJ 1511 Invitation Cards【SPFA】

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

a48634331的博客 159

Invitation Cards POJ-1511 (spfa)

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

165

POJ 1511 Invitation Cards //spfa

<br />Invitation CardsTime Limit: 8000MS Memory Limit: 262144KTotal Submissions: 8425 Accepted: 2617<br />DescriptionIn the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want t

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

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值