POJ1511(最短路大数据处理)
| Time Limit: 8000MS | Memory Limit: 262144K | |
| Total Submissions: 23357 | Accepted: 7675 |
Description
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
Output
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(最短路大数据处理)的更多相关文章
- 翻译-In-Stream Big Data Processing 流式大数据处理
相当长一段时间以来,大数据社区已经普遍认识到了批量数据处理的不足.很多应用都对实时查询和流式处理产生了迫切需求.最近几年,在这个理念的推动下,催生出了一系列解决方案,Twitter Storm,Yah ...
- [转载] 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
原文: http://www.36dsj.com/archives/25042 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要有日志收集系统.消息系统.分布式服务 ...
- eMarketer:DMP帮广告主搞定大数据处理问题
DMP(数据管理平台)帮助广告主获得可行动的洞察 在数字广告领域,大数据和数据管理平台(DPMs)仍大有可为.DMPs让广告主可以使用他们的大数据来做出更灵活更有效的营销决策. 数据管理和分析是业界挑 ...
- 《Spark大数据处理:技术、应用与性能优化 》
基本信息 作者: 高彦杰 丛书名:大数据技术丛书 出版社:机械工业出版社 ISBN:9787111483861 上架时间:2014-11-5 出版日期:2014 年11月 开本:16开 页码:255 ...
- Spark大数据处理技术
全球首部全面介绍Spark及Spark生态圈相关技术的技术书籍 俯览未来大局,不失精细剖析,呈现一个现代大数据框架的架构原理和实现细节 透彻讲解Spark原理和架构,以及部署模式.调度框架.存储管理及 ...
- hadoop大数据处理之表与表的连接
hadoop大数据处理之表与表的连接 前言: hadoop中表连接其实类似于我们用sqlserver对数据进行跨表查询时运用的inner join一样,两个连接的数据要有关系连接起来,中间必须有一个 ...
- 0基础搭建Hadoop大数据处理-初识
在互联网的世界中数据都是以TB.PB的数量级来增加的,特别是像BAT光每天的日志文件一个盘都不够,更何况是还要基于这些数据进行分析挖掘,更甚者还要实时进行数据分析,学习,如双十一淘宝的交易量的实时展示 ...
- 0基础搭建Hadoop大数据处理-编程
Hadoop的编程可以是在Linux环境或Winows环境中,在此以Windows环境为示例,以Eclipse工具为主(也可以用IDEA).网上也有很多开发的文章,在此也参考他们的内容只作简单的介绍和 ...
- 《Spark大数据处理:技术、应用与性能优化》【PDF】 下载
内容简介 <Spark大数据处理:技术.应用与性能优化>根据最新技术版本,系统.全面.详细讲解Spark的各项功能使用.原理机制.技术细节.应用方法.性能优化,以及BDAS生态系统的相关技 ...
随机推荐
- 黑名单机制来临,你的应用还好么?Android P DP2最新兼容性报告出炉
5月9日,谷歌面向全球开发者发布了 Android P Beta,即 Android P DP2. 华为终端开放实验室第一时间对TOP1000主流应用兼容性进行测试,那么在版本兼容性方面各主流应用有何 ...
- 深入Asyncio(二)从线程到协程
线程的真相 多线程并不是一无是处,在实际问题中,要权衡优劣势来选择多线程.多进程或是协程.协程为多线程的某些问题提供了一种解决方案,所以学习协程首先要对线程有一定了解. 多线程优点 代码可读性 多线程 ...
- vue 流程设计器
github地址:https://github.com/280780363/gucflow.designer demo地址:https://280780363.github.io/gucflow.de ...
- spring AOP(切面) 表达式介绍
在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...
- 九度OJ 1177:查找 (字符串操作)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6729 解决:1981 题目描述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共有2中命令:1.翻转 从下标 ...
- mooc课程mit 6.00.1x--problem set3解决方法
RADIATION EXPOSURE 挺简单的一道题,计算函数和算法过程都已经给出,做一个迭代计算就行了. def radiationExposure(start, stop, step): ''' ...
- 洛谷2483 k短路([SDOI2010]魔法猪学院)
题目请戳这里 一句话题意: 给你一张n个节点,m条单向边的图,求1到n第k短的路. emmm,纪念第一个黑题(我是真的菜啊!!) 这题目还是很难的,本蒟蒻只会被洛谷卡掉的A(所以就愉快地特判了),首先 ...
- 我的Android进阶之旅------>Android中MediaButtonReceiver广播监听器的机制分析
今天看公司的一段关于MediaButtonReceiver的代码看的比较混乱,幸好看了下面的这篇文章,才能茅塞顿开的理解好代码.在此转载下来,以备以后理解,希望都到这篇文章的人也能够有所帮助. 本文转 ...
- POJ - 1611 The Suspects 【并查集】
题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...
- oracle ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
ORA-12514: TNS:listener does not currently know of service requested in connect descriptor 1.看看是不是监听 ...