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生态系统的相关技 ...
随机推荐
- Linux 文件系统IO性能优化
对于LINUX SA来说,服务器性能是需要我们特别关注的,包括CPU.IO.内存等等系统的优化变得至关重要,这里转载一篇非常不错的关于IO优化的文章,供大家参考和学习: 一.关于页面缓存的信息,可以用 ...
- Android 超高仿微信图片选择器 图片该这么载入
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自:[张鸿洋的博客] 1.概述 关于手机图片载入器,在当今像 ...
- Cannot create __weak reference in file using manual reference counting
Xcode更新到7.3后会出现NSObject+MJProperty.h报Cannot create __weak reference in file using manual reference c ...
- H2 database 应用
以前对内存表的引用一直采用sqllite,由于sqllite对字段的局限性无法满足需要.后来对h2 有了一定青睐做了下应用.下面对h2进行介绍. 1. H2数据库引擎 H2数据库由Java编写的,它可 ...
- 九度OJ 1015:还是A+B (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6773 解决:4031 题目描述: 读入两个小于10000的正整数A和B,计算A+B.需要注意的是:如果A和B的末尾K(不超过8)位数字相同 ...
- c++标准库比较
1 GNU standard c++ library debian发行版中使用的c++标准库是GNU standard c++标准库. 2 Boost debian发行版中也是用了boost库,但是不 ...
- mysql系列之8.mysql高可用 (mha4mysql)
环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...
- LeetCode:将有序数组转换为二叉搜索树【108】
LeetCode:将有序数组转换为二叉搜索树[108] 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差 ...
- 剑指Offer:对称的二叉树【28】
剑指Offer:对称的二叉树[28] 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 题目分析 Java题解 /* publi ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...