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生态系统的相关技 ...
随机推荐
- python学习(六)元组学习
元组就是列表的一种,不过元组具有不可变性,而且是用圆括号访问的. 索引(下表索引或者键索引都是用的中括号) #!/usr/bin/python # 这节来学习元组, tuple, 基本上就像一个不可以 ...
- Windows系统的Jenkins持续集成环境
Windows系统的Jenkins持续集成环境 如题:本文将介绍如何在Windows环境下运用Jenkins部署持续集成环境.之所以写本文,是因为在最近工作当中,学习使用Jenkins时,确实遇到了一 ...
- Redis源代码分析(十七)--- multi事务操作
redis作为一非关系型数据库,居然相同拥有与RDBMS的事务操作,不免让我认为比較吃惊.在redis就专门有文件就是运行事务的相关操作的.也能够让我们领略一下.在Redis的代码中是怎样实现事务操作 ...
- Windows下Nginx+Web.py+FastCGI服务搭建
在搭建之前,有必要了解下什么是fastcgi,但鉴于我自己也不大了解,这里就不搬门弄斧了,请参考各种百科和官网资料. 1.资源下载 python下载地址:戳这里webpy下载地址:戳这里flup下载地 ...
- C#抓取网面上的html内容(JS动态生成的无法抓取)
抓取内容的代码: /// </summary> /// <param name="url">路径URL</param> /// <para ...
- 再看GS线程
再看GS线程 void GameServer::ProcessThreadTry() { ; packet rcvPkt; rcvPkt.data = * ]; //该事件工厂主要创建了两个定时器1. ...
- 九度OJ 1072:有多少不同的面值组合? (计数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3112 解决:1591 题目描述: 某人有8角的邮票5张,1元的邮票4张,1元8角的邮票6张,用这些邮票中的一张或若干张可以得到多少种不同的 ...
- Swift 学习笔记 (方法)
方法 是关联了特定类型的函数.类,结构体以及枚举都能定义实例方法,方法封装了给定类型特定的任务和功能.类,结构体和枚举同样可以定义类型方法,这是与类型本身关联的方法.类型方法与 Objective-C ...
- 我的Java开发学习之旅------>Java String对象作为参数传递的问题解惑
又是一道面试题,来测试你的Java基础是否牢固. 题目:以下代码的运行结果是? public class TestValue { public static void test(String str) ...
- python cookbook第三版学习笔记五:datetime
Python中表示时间的模块是datetime,引入下面的模块 from datetime import datetime,timedelta print datetime.today() #打印出 ...