Invitation Cards

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3129    Accepted Submission(s):
1456

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, 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
 
Source
 
Recommend
LL   |   We have carefully selected several similar
problems for you:  1531 1384 1596 1534 1532
 
第一次使用spfa算法,完全不知道是什么。。。看了网上的代码,明白了思路,先计算从1为起点到各点的距离之和,再重置地图,计算各点到1点的距离之和。
 
题意:有编号1~P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费。有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1。 求所有人来回的最小费用之和。
 
附上代码:
 
 #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)的更多相关文章

  1. HDU 1535 Invitation Cards(最短路 spfa)

    题目链接: 传送门 Invitation Cards Time Limit: 5000MS     Memory Limit: 32768 K Description In the age of te ...

  2. HDU 1535 Invitation Cards (POJ 1511)

    两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...

  3. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  4. HDU - 1535 Invitation Cards 前向星SPFA

    Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...

  5. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  6. HDU 1535 Invitation Cards (最短路)

    题目链接 Problem Description In the age of television, not many people attend theater performances. Anti ...

  7. hdu 1535 Invitation Cards (最短路径)

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. HDU 1535 Invitation Cards(SPFA,及其优化)

    题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...

  9. [HDU 1535]Invitation Cards[SPFA反向思维]

    题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...

随机推荐

  1. 洛谷P1757 通天之分组背包 [2017年4月计划 动态规划06]

    P1757 通天之分组背包 题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品 ...

  2. 清除SQL数据库文本字段中的回车、换行符的方法

    清除SQL数据库中文本字段的回车.换行符的方法 清除回车符: update tableName set columnName = rtrim(ltrim(replace(columnName ,cha ...

  3. Lambada. 计算和

    Lambada. 计算和 import java.util.Arrays; import java.util.List; public class ListLambada { public stati ...

  4. 【arduino】anroid的app与arduino的ch05,进行蓝牙通信遇到的问题

    Arduino程序上传不成功 显示系统找不到指定文件 错误信息显示是 avrdude: ser_open(): can't open device "\\.\COM1": 系统找不 ...

  5. windows中Navicat连接centos中的mysql报:1130-Host '192.168.xxx.1' is not allowed to connect to this MySQL解决方法

    解决方法: 在centos中登录mysql输入下面指令: grant all PRIVILEGES on *.* to ' WITH GRANT OPTION;

  6. let 和const命令

    ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效.换句话说,let声明了块级作用域. 输出 看下面代码: 输出: 变量i是var声明的,在 ...

  7. Sublime text2 常用插件

    很早就安装了这款软件,因为听说很NB.但是一直都是习惯用vim, 所以一直没有去学习它, 现在用用感觉确实很不错,所以找了些插件, 参考网址: http://www.hphq.net/Marketin ...

  8. LintCode A+B问题

    给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 说明 a和b都是 32位 整数么? 是的 我可以使用位运算符么? 当然可以 样例 如果 a=1 并且 b=2,返回3 1.(忽略进位 ...

  9. [Android]Space控件的应用场景

    Space控件是在Android 4.0中加入,是个空白的view,一般用于填充View组件中的间隙. support-v4包里提供了兼容低版本的Space控件. 源码分析 Space控件源码非常简单 ...

  10. IDEA 运行maven项目配置