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的最短路之和. 思路: 返回的时 ...
随机推荐
- Data Lake Analytics: 使用DataWorks来调度DLA任务
DataWorks作为阿里云上广受欢迎的大数据开发调度服务,最近加入了对于Data Lake Analytics的支持,意味着所有Data Lake Analytics的客户可以获得任务开发.任务依赖 ...
- Vue--使用watch、computed、filter方法来监控
watch与computed.filter: watch:监控已有属性,一旦属性发生了改变就去自动调用对应的方法 computed:监控已有的属性,一旦属性的依赖发生了改变,就去自动调用对应的方法 f ...
- golang内置函数
- layui的select联动 - CSDN博客
要实现联动效果注意两点: 第一要可以监听到select的change事件: 第二异步加载的内容,需要重新渲染后才可以 正常使用. html结构: <form class="layui- ...
- Codeforces 404B
毫无疑问这题不是难题,但是这种题目最让人纠结 打心里对这种题目就比较害怕,果然,各种WE 这里贴上代码,用Python写的,比较偷懒: def cur_pos(a, d): if 0 <= d ...
- Java中时间和日期的处理
一.日期转换为字符串 1.日期以特定的格式输出: // 创建日期并转换为yyyy-MM-dd格式_(MM一定要大写,以免与hh:mm:ss中的mm冲突) SimpleDateFormat sdf = ...
- Directx11教程(47) alpha blend(4)-雾的实现
原文:Directx11教程(47) alpha blend(4)-雾的实现 除了用来实现透明效果之外,我们还可以用alpha blend来实现雾(fog)的效果.通过逐渐清晰的雾气效果,可 ...
- iOS app发布流程
http://www.xuebuyuan.com/1980497.html http://blog.csdn.net/alincexiaohao/article/details/38725367 ap ...
- Inno setup 卸载时删除程序文件夹(文件)
Inno setup 卸载时删除程序文件夹(文件) //删除所有配置文件以达到干净卸载的目的 procedure CurUninstallStepChanged(CurUninstallStep: T ...
- Precision和Recall
学习自: http://blog.csdn.net/wangran51/article/details/7579100