hdu 1535 Invitation Cards (最短路径)
Invitation Cards
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1588 Accepted Submission(s): 714
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.
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
210
//1125MS 41076K 1434 B G++
/* 其实开始让我做我是拒绝的,不能叫我做我马上做,我得先试一下。不然加了特效搞得好像很容易那样,结果群众却做不出来,
我会被批的。 题意:
有编号1~P个点,有Q条单向路,求 1到其他P-1个点 + 其他P-1个点到1 的最短路线 最短路径:
首先数据比较大,用spfa比较靠谱。其实,第一个很明显以1为源点进行一次spfa就出来了,问题是第二个的求法,
首先遍历P-1个点是会TLE的,所以要转一下思维,就是逆向思维。把原来的路线全都反向再建图(即把u->v变成v->u),
在来一次源点为1的spfa就得出解了。 */
#include<iostream>
#include<vector>
#include<queue>
#define N 1000005
#define inf 0x7ffffff
using namespace std;
struct node{
int v,w;
node(int a,int b){
v=a;w=b;
}
};
vector<node>V[N];
int d[N];
int vis[N];
int p,q;
int a[N],b[N],w[N]; //记录路线信息
void spfa(int s) //spfa模板
{
for(int i=;i<=p;i++) d[i]=inf;
memset(vis,,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
Q.push(v);
vis[v]=;
}
}
}
}
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&p,&q);
for(int i=;i<=p;i++) V[i].clear();
int ans=;
for(int i=;i<q;i++){ //正向建图
scanf("%d%d%d",&a[i],&b[i],&w[i]);
V[a[i]].push_back(node(b[i],w[i]));
}
spfa();
for(int i=;i<=p;i++) ans+=d[i];
for(int i=;i<=p;i++) V[i].clear();
for(int i=;i<q;i++) //反向建图
V[b[i]].push_back(node(a[i],w[i]));
spfa();
for(int i=;i<=p;i++) ans+=d[i];
printf("%d\n",ans);
}
return ;
}
hdu 1535 Invitation Cards (最短路径)的更多相关文章
- 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(最短路 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 (最短路)
题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...
- HDU - 1535 Invitation Cards 前向星SPFA
Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...
- hdu 1535 Invitation Cards(spfa)
Invitation Cards Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 1535 Invitation Cards
http://acm.hdu.edu.cn/showproblem.php?pid=1535 这道题两遍spfa,第一遍sfpa之后,重新建图,所有的边逆向建边,再一次spfa就可以了. #inclu ...
- [HDU 1535]Invitation Cards[SPFA反向思维]
题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...
随机推荐
- git 简单使用(待完善)
git是一个分布式版本控制器,简单来说就是可以记录每次代码的修改和提交,方便我们查看修改记录和版本的回退 工作流程 基本概念 仓库 git 是一个分布式版本控制器,其单位就是仓库,每个仓库就是当前gi ...
- (排班表二)后台动态绘制Grid表格
后台动态绘制值班表(Grid表格 列名不固定) 要求:表头除了值班人姓名,还要显示日期,及每天的星期值,用斜杠‘/’分隔.即:几号/星期几 最终实现的效果:根据查询的年月显示每个值班人查询月份每天的值 ...
- 一、小程序连接MySql数据库
前言:我用的是wafer2 node.解决方案 下面连接有环境配置及搭建流程(https://github.com/tencentyun/wafer2-quickstart-nodejs) ,连接是官 ...
- python导包语句执行
今天在做项目中遇到一个问题,在first_page中引用login的登录方法,第一次执行登录可以正常登录,登录成功后,再选择返回主菜单,回到上个页面,再选择登录时报错“login_class isno ...
- 面试题 LazyMan 的Rxjs实现方式
前言 笔者昨天在做某公司的线上笔试题的时候遇到了最后一道关于如何实现LazyMan的试题,题目如下 实现一个LazyMan,可以按照以下方式调用:LazyMan("Hank")输出 ...
- PHP生成特定长度的纯字母字符串
PHP中,md5().uniqid()函数可以返回32位和13位不重复的字符串,但是这些字符串都可能包含有数字.如果需要纯字母的字符串,而且长度不定,比如8位,那么直接用这两个函数无法达到效果. 这时 ...
- thinkPHP5.0 save和saveAll,新增和更新的问题
今天遇到一个问题,在模型中使用save保存数据之后,使用saveAll继续新增数据,结果报 缺少更新条件,网上搜了下发现一篇文章https://www.jianshu.com/p/1848f61de6 ...
- JZOJ 5922. sequence
5922. [NOIP2018模拟10.23]sequence (File IO): input:sequence.in output:sequence.out Time Limits: 1000 m ...
- python——int()函数
1. 使用 int() 将小数转换为整数,结果是向上取整还是向下取整呢? 小数取整会采用比较暴力的截断方式,即向下取整.(注:5.5向上取整为6,向下取整为5) 2. 我们人类思维是习惯于“四舍五入” ...
- 01,jupyter环境安装
jupyter notebook环境安装 一.什么是Jupyter Notebook? 1. 简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文 ...