hdu 1535 Invitation Cards(spfa)
Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3129 Accepted Submission(s):
1456
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 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.
amount of money to be paid each day by ACM for the travel costs of its
volunteers.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define M 1000010
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int now,to,w;
} e[M];
int first[M],nexts[M];
int n,m,dis[M],vis[M];
void init()
{
int i,j;
for(i=; i<=m; i++)
first[i]=nexts[i]=-;
for(i=; i<m; i++)
{
scanf("%d%d%d",&e[i].now,&e[i].to,&e[i].w);
nexts[i]=first[e[i].now]; //spfa()算法
first[e[i].now]=i;
}
} void spfa(int src,int flag)
{
int i,j;
for(i=; i<=n; i++)
dis[i]=inf;
dis[src]=;
for(i=; i<=n; i++)
vis[i]=;
queue<int> q;
q.push(src);
while(!q.empty())
{
src=q.front();
q.pop();
vis[src]=;
for(i=first[src]; i!=-; i=nexts[i])
{
int to=(flag?e[i].to:e[i].now);
if(dis[to]>dis[src]+e[i].w) //每次比较更新
{
dis[to]=dis[src]+e[i].w;
if(!vis[to])
{
vis[to]=;
q.push(to);
}
}
}
}
} void set_map()
{
int i,j;
for(i=; i<=m; i++)
first[i]=nexts[i]=-;
for(i=; i<m; i++)
{
int now=e[i].now;
int to=e[i].to;
nexts[i]=first[to];
first[to]=i;
}
}
int main()
{
int T,i,j,sum;
scanf("%d",&T);
while(T--)
{
sum=;
scanf("%d%d",&n,&m);
init();
spfa(,);
for(i=; i<=n; i++) //先将1到每个车站的最小花费加起来
sum+=dis[i];
set_map(); //重置图
spfa(,);
for(i=; i<=n; i++) //将所有点到1的最小花费加起来
sum+=dis[i];
printf("%d\n",sum);
}
return ;
}
hdu 1535 Invitation Cards(spfa)的更多相关文章
- HDU 1535 Invitation Cards(最短路 spfa)
题目链接: 传送门 Invitation Cards Time Limit: 5000MS Memory Limit: 32768 K Description In the age of te ...
- HDU 1535 Invitation Cards (POJ 1511)
两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...
- hdu 1535 Invitation Cards(SPFA)
Invitation Cards Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) T ...
- HDU - 1535 Invitation Cards 前向星SPFA
Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- HDU 1535 Invitation Cards (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- hdu 1535 Invitation Cards (最短路径)
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 1535 Invitation Cards(SPFA,及其优化)
题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...
- [HDU 1535]Invitation Cards[SPFA反向思维]
题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...
随机推荐
- 洛谷P1052 过河
P1052 过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上. 由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青 ...
- Js Array 删除
数组删除操作 Array.prototype.inArray = function (e) {for (i = 0; i < this.length; i++) {if (this[i] == ...
- 从0开始学习 GitHub 系列之「04.向GitHub 提交代码」
之前的这篇文章「从0开始学习 GitHub 系列之「Git速成」」相信大家都已经对 Git 的基本操作熟悉了,但是这篇文章只介绍了对本地 Git 仓库的基本操作,今天我就来介绍下如何跟远程仓库一起协作 ...
- SAS信用评分之模型拟合以及验证的大坑
SAS信用评分之模型拟合以及验证的大坑 今天的内容是来讲我这段时间被模型拟合和模型验证坑过的那些事.我也是千辛万苦终于是把模型给建出来了.此处应该有掌声.因为模型老是效果不好这件事,我躲在被窝里哭了好 ...
- Python3.7.4入门-4模块
4 模块 Python有一种方法可以把定义放在一个文件里,并在脚本或解释器的交互式实例中使用它们.这样的文件被称作 模块 :模块中的定义可以 导入 到其它模块或者 主 模块 模块是一个包含Python ...
- DirectX11笔记(一)--配置DirectX工程
原文:DirectX11笔记(一)--配置DirectX工程 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/article/d ...
- 2019-8-31-dotnet-core-用值初始化整个数组
title author date CreateTime categories dotnet core 用值初始化整个数组 lindexi 2019-08-31 16:55:58 +0800 2019 ...
- python 对位运算
- Spring_JDBC连接
1.导入jarbao 2.创建pojo,dao,Impl package com.tanlei.pojo; public class Department { private Long deptId; ...
- IntelliJ IDEA 添加项目后编译显示包不存在的解决方案
File -> Project Structure -> Modules 看看是否有多个项目,删掉无用的.或者调整一下项目,重新 Mark as 一下,指定成 Sources