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的最短路之和. 思路: 返回的时 ...
随机推荐
- Leetcode598.Range Addition II范围求和2
给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作. 操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 < ...
- void 运算符
void 是 javascript 的操作符,意思是:只执行表达式,但没有返回值.该表达式会被计算但是不会在当前文档处装入任何内容,void其实是javascript中的一个函数,接受一个参数,返回值 ...
- javascript中uber实现子类访问父类成员
function Animal(){} Animal.prototype={ name:"animal", toString:function(){ console.log(thi ...
- 当inline-block和text-indent遇到IE6,IE7
在实际应用中,考虑到seo,很多button,icon都要用到inline-block和text-indent来处理,例如: <a href="#" class=" ...
- UI标签库专题五:JEECG智能开发平台 Tabs(选项卡父标签)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/28956223 tools string ...
- IOS开发基础篇--CAShapeLayer的strokeStart和strokeEnd属性
http://blog.csdn.net/yixiangboy/article/details/50662704 一.案例演示 最近有一个小需求,就是要做一个圆形进度条,大概样子如下: . 在不知道有 ...
- Leetcode5.Longest Palindromic Substring最长回文字串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- Leetcode888.Fair Candy Swap公平的糖果交换
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...
- 【水滴石穿】MyFirstRNDemo
比较简单的项目 //index.js /** @format */ import {AppRegistry} from 'react-native'; //默认创建的类 import App from ...
- 【转】基于OLSR路由协议实现Ad-Hoc组网
一.软件包的安装 1. olsrd软件包的安装 libpthread_0.9.33.2-1_ar71xx.ipk olsrd_0.6.6.2-4_ar71xx.ipk 2. luci的安装 olsrd ...