题目链接:https://cn.vjudge.net/problem/POJ-1511

题意

给出一个图

求从节点1到任意节点的往返路程和

思路

没有考虑稀疏图,上手给了一个Dijsktra(按紫书上的存边方法)

直接超时

写了一个极限大小数据

发现读入时间很长,Dij时间也很长,相当于超时超到姥姥家了

赶紧做优化

  • 发现稀疏图,于是换Bellman(spfa)
  • 换邻接表
  • (虽然没有必要)scanf换成getchar输入模版,大量数据可以节省大概800ms的样子
  1. 稀疏图适用Bellman(optimed),稠密图适用Dijsktra
  2. 对大数据(maxn>1e6),一定要用邻接表
  3. 对大数据(x>1e9, maxm>1e6),用输入模版可以降大概800ms

代码

#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int maxn=1e6, maxm=maxn;
const long long INF=1LL<<60;
struct Edge{
int to, dis, next;
}edges[maxm+5], redges[maxn+5];
int size, rsize, head[maxn+5], rhead[maxn+5]; inline void addEdge(int from, int to, int dis){
edges[size]=Edge{to, dis, head[from]};
head[from]=size++;
redges[rsize]=Edge{from, dis, rhead[to]};
rhead[to]=rsize++;
} long long dist[maxn+5];
long long Bellman(int n, int ahead[], Edge *aedges){
int cnt[maxn+5]={0};
bool inq[maxn+5]={false};
queue<int> que; for (int i=0; i<=n; i++) dist[i]=INF; dist[1]=0;
que.push(1);
while (que.size()){
int from=que.front(); que.pop();
inq[from]=false; for (int i=ahead[from]; i!=-1; i=aedges[i].next){
Edge &e=aedges[i];
int &to=e.to, &dis=e.dis; if (dist[to]<=dist[from]+dis) continue;
dist[to]=dist[from]+dis;
if (inq[to]) continue;
inq[to]=true; que.push(to);
// if (++cnt[to]>n) return -1;
}
} long long sum=0;
for (int i=1; i<=n; i++) if (dist[i]<INF)
sum+=dist[i];
return sum;
} void init(void){
memset(head, -1, sizeof(head));
memset(rhead, -1, sizeof(rhead));
rsize=size=0;
} inline void read(int &num){
char in;
in=getchar();
while(in <'0'|| in > '9') in=getchar();
num = in -'0';
while(in =getchar(),in >='0'&&in <='9')
num *=10, num+=in-'0';
} int main(void){
int T, n, m, from, to, dis; scanf("%d", &T);
while (T--){
init();
scanf("%d%d", &n, &m);
for (int i=0; i<m; i++){
read(from); read(to); read(dis);
addEdge(from, to, dis);
}printf("%lld\n", Bellman(n, head, edges)+Bellman(n, rhead, redges));
} return 0;
}
Time Memory Length Lang Submitted
860ms 40672kB 1914 G++ 2018-05-26 19:26:39

POJ-1511 Invitation Cards 往返最短路 邻接表 大量数据下的处理方法的更多相关文章

  1. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  2. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  3. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  4. poj 1511 Invitation Cards (最短路)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 33435   Accepted: 111 ...

  5. POJ 1511 Invitation Cards (最短路的两种方法spfa, Dij)

    题意: 给定n个点, m条路, 求1到 2 ~n的最短路之和加上2~n到1的最短路之和 分析: 裸最短路, 求其他点到源点的距离只需要把边方向再从源点求一次即可 spfa代码 #include< ...

  6. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  7. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  8. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  9. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

随机推荐

  1. k8s集群启动了上万个容器(一个pod里放上百个容器,起百个pod就模拟出上万个容器)服务器超时,无法操作的解决办法

    问题说明: 一个POD里放了百个容器,然后让K8S集群部署上百个POD,得到可运行上万个容器的实验目的. 实验环境:3台DELL裸机服务器,16核+64G,硬盘容量忽略吧,上T了,肯定够. 1.一开始 ...

  2. (翻译) Inheritance and the prototype chain 继承和原型链

    JavaScript is a bit confusing for developers experienced in class-based languages (like Java or C++) ...

  3. WIN10 java环境变量问题之 配置的JDK1.8版本却是1.7

    问题前沿,在开发项目中,发布的项目出现了内存溢出问题,我挨个把代码看了一遍,并不能准确定位到那个地方能出现内存溢出问题,后来想到使用压力测试,较可能出现内存溢出的接口进行一番测试. 我就安装了一个ap ...

  4. HTML 编码规范

    语法 使用 4 个空格做为一个缩进层级,不允许使用 2 个空格或 tab 字符 在属性上,使用双引号 "",不要使用单引号 '' 属性名 / 属性值全小写,用中划线 - 做分隔符 ...

  5. Django REST Framework - 分页 - 渲染器 - 解析器

    为什么要使用分页? 我们数据表中可能会有成千上万条数据,当我们访问某张表的所有数据时,我们不太可能需要一次把所有的数据都展示出来,因为数据量很大,对服务端的内存压力比较大还有就是网络传输过程中耗时也会 ...

  6. Unity WWW类调用http

    1.Http请求中Content-Type讲解 MediaType,即是Internet Media Type,互联网媒体类型:也叫做MIME类型,在Http协议消息头中,使用Content-Type ...

  7. centos7下部署Redis

    1.1. Redis的安装 Redis是c语言开发的. 安装redis需要c语言的编译环境.如果没有gcc需要在线安装.yum install gcc-c++ 安装步骤: 第一步:redis的源码包上 ...

  8. RabbitMQ学习总结(3)——入门实例教程详解

    一.起航 本章节,柯南君将从几个层面,用官网例子讲解一下RabbitMQ的实操经典程序案例,让大家重新回到经典"Hello world!"(The simplest thing t ...

  9. mybatis插入操作时,返回自增主键id

    mapper.xml 代码 <insert id="insert" parameterType="com.Student" > <select ...

  10. angular-应用

    什么是SPA 真正的 AngularJS 单页 Web 应用(single page web application,SPA) 一些基础概念 <html> 元素是 AngularJS 应用 ...