Invitation Cards
Time Limit: 8000MS   Memory Limit: 262144K
Total Submissions: 23357   Accepted: 7675

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
题意:求1号结点到其余各结点路径之和与其余各结点到1号结点之和的和。
思路:求其余各结点到1号结点的路径时可将有向边反向,转化为求1号结点到其余各结点的路径之和。注意:该题目的数据量较大,用动态邻接表存储会RE。
对比了一下 dijkstra 与 spfa算法。
/*
dijkstra 1511 Accepted 39744K 1907MS G++
*/
#include"cstdio"
#include"queue"
#include"vector"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
typedef long long LL;
typedef pair<int,int> P;
struct Edge{
int to,cost,next;
}es[][MAXN];
int V,E;
int head[][MAXN];
LL d[MAXN];
void add_edge(int u,int v,int cost,int type)
{
es[type][E].to=v;
es[type][E].cost=cost;
es[type][E].next=head[type][u];
head[type][u]=E;
} LL dijkstra(int s,int type)
{
for(int i=;i<=V;i++) d[i]=INF; priority_queue<P,vector<P>,greater<P> > que;
d[s]=,que.push(P(,s)); while(!que.empty())
{
P p=que.top();que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=head[type][v];i!=-;i=es[type][i].next)
{
Edge e=es[type][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
LL ans=;
for(int i=;i<=V;i++)
ans+=d[i];
return ans;
} int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int P,Q;
scanf("%d%d",&P,&Q);
V=P,E=;
for(int i=;i<=V;i++) head[][i]=head[][i]=-;
for(int i=;i<Q;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
add_edge(u,v,co,);
add_edge(v,u,co,);
E++;
} LL res=;
res+=dijkstra(,);
res+=dijkstra(,);
printf("%I64d\n",res); }
return ;
}
/*
spfa 1511 Accepted 43676K 1875MS G++
*/
#include"cstdio"
#include"queue"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
typedef long long LL;
struct Edge{
int to,cost,next;
}es[][MAXN];
int head[][MAXN];
int V,E;
LL d[MAXN];
int vis[MAXN];
LL spfa(int s,int type)
{
for(int i=;i<=V;i++)
{
d[i]=INF;
vis[i]=;
}
queue<int> que;
vis[s]=,d[s]=,que.push(s); while(!que.empty())
{
int v=que.front();que.pop();
vis[v]=;
for(int i=head[type][v];i!=-;i=es[type][i].next)
{
Edge e=es[type][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
if(!vis[e.to])
{
que.push(e.to);
vis[e.to]=;
}
}
}
}
LL ans=;
for(int i=;i<=V;i++)
ans+=d[i];
return ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int P,Q;
scanf("%d%d",&P,&Q);
V=P,E=;
for(int i=;i<=V;i++) head[][i]=head[][i]=-;
for(int i=;i<Q;i++)
{
int u,v,co;
scanf("%d%d%d",&u,&v,&co);
es[][E].to=v,es[][E].cost=co,es[][E].next=head[][u],head[][u]=E;
es[][E].to=u,es[][E].cost=co,es[][E].next=head[][v],head[][v]=E;
E++;
} LL res=;
res+=spfa(,);
res+=spfa(,);
printf("%I64d\n",res); }
return ;
}

堆优化dijkstra 算法的复杂度为 |E|*log(|V|) ,优势在于处理稀疏图。

POJ1511(最短路大数据处理)的更多相关文章

  1. 翻译-In-Stream Big Data Processing 流式大数据处理

    相当长一段时间以来,大数据社区已经普遍认识到了批量数据处理的不足.很多应用都对实时查询和流式处理产生了迫切需求.最近几年,在这个理念的推动下,催生出了一系列解决方案,Twitter Storm,Yah ...

  2. [转载] 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等

    原文: http://www.36dsj.com/archives/25042 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要有日志收集系统.消息系统.分布式服务 ...

  3. eMarketer:DMP帮广告主搞定大数据处理问题

    DMP(数据管理平台)帮助广告主获得可行动的洞察 在数字广告领域,大数据和数据管理平台(DPMs)仍大有可为.DMPs让广告主可以使用他们的大数据来做出更灵活更有效的营销决策. 数据管理和分析是业界挑 ...

  4. 《Spark大数据处理:技术、应用与性能优化 》

    基本信息 作者: 高彦杰 丛书名:大数据技术丛书 出版社:机械工业出版社 ISBN:9787111483861 上架时间:2014-11-5 出版日期:2014 年11月 开本:16开 页码:255 ...

  5. Spark大数据处理技术

    全球首部全面介绍Spark及Spark生态圈相关技术的技术书籍 俯览未来大局,不失精细剖析,呈现一个现代大数据框架的架构原理和实现细节 透彻讲解Spark原理和架构,以及部署模式.调度框架.存储管理及 ...

  6. hadoop大数据处理之表与表的连接

    hadoop大数据处理之表与表的连接 前言:  hadoop中表连接其实类似于我们用sqlserver对数据进行跨表查询时运用的inner join一样,两个连接的数据要有关系连接起来,中间必须有一个 ...

  7. 0基础搭建Hadoop大数据处理-初识

    在互联网的世界中数据都是以TB.PB的数量级来增加的,特别是像BAT光每天的日志文件一个盘都不够,更何况是还要基于这些数据进行分析挖掘,更甚者还要实时进行数据分析,学习,如双十一淘宝的交易量的实时展示 ...

  8. 0基础搭建Hadoop大数据处理-编程

    Hadoop的编程可以是在Linux环境或Winows环境中,在此以Windows环境为示例,以Eclipse工具为主(也可以用IDEA).网上也有很多开发的文章,在此也参考他们的内容只作简单的介绍和 ...

  9. 《Spark大数据处理:技术、应用与性能优化》【PDF】 下载

    内容简介 <Spark大数据处理:技术.应用与性能优化>根据最新技术版本,系统.全面.详细讲解Spark的各项功能使用.原理机制.技术细节.应用方法.性能优化,以及BDAS生态系统的相关技 ...

随机推荐

  1. HDFS源码分析心跳汇报之整体结构

    我们知道,HDFS全称是Hadoop Distribute FileSystem,即Hadoop分布式文件系统.既然它是一个分布式文件系统,那么肯定存在很多物理节点,而这其中,就会有主从节点之分.在H ...

  2. js中insertAdjacentHTML的玩法

    原型:insertAdajcentHTML(swhere,stext) insertAdjacentHTML方法:在指定的地方插入html标签语句 参数:swhere: 指定插入html标签语句的地方 ...

  3. Spring MVC获得HttpServletRequest

    以下代码是获得Spring MVC中的HttpServletRequest ServletRequestAttributes attr = (ServletRequestAttributes) Req ...

  4. iOS --生产JSON格式,创建JSON文件,创建文件夹,指定储存

    //生成json文件 - (void)onjson { //    如果数组或者字典中存储了  NSString, NSNumber, NSArray, NSDictionary, or NSNull ...

  5. __str__ 和 __unicode__ 的区别和用法

    转自:https://www.cnblogs.com/painberg/p/8514860.html 简而言之,就是__str__和__unicode__都是为了再管理站点中加载这个表时想显示什么属性 ...

  6. Redis实现主从复制(转)

    一.Redis的Replication: 这里首先需要说明的是,在Redis中配置Master-Slave模式真是太简单了.相信在阅读完这篇Blog之后你也可以轻松做到.这里我们还是先列出一些理论性的 ...

  7. 九度OJ 1073:杨辉三角形 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3780 解决:1631 题目描述: 输入n值,使用递归函数,求杨辉三角形中各个位置上的值. 输入: 一个大于等于2的整型数n 输出: 题目可 ...

  8. js为Object对象动态添加属性和值 eval c.k c[k]

    const appendInfo = () => { const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/maste ...

  9. python网络爬虫之使用scrapy自动登录网站

    前面曾经介绍过requests实现自动登录的方法.这里介绍下使用scrapy如何实现自动登录.还是以csdn网站为例. Scrapy使用FormRequest来登录并递交数据给服务器.只是带有额外的f ...

  10. 大家都是怎么看待STO的?

    STO,全称为「Security Token Offer」,即证券型通证发行.STO是2017年底从美国开始流行的,对于在美国注册的公司,STO是一个合法合规的ICO. 对于STO,大家都是怎么看待的 ...