题目链接: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. Java 面向对象详解

    0 引言 接触项目开发也有很长一段时间了,最近开始萌发出想回过头来写写以前学过的基础知识的想法. 1 面向对象 面向对象(Object Oriented)是一种新兴的程序设计方法,或者是一种新的程序设 ...

  2. Java基础之Colloction

    0 引言 以下是介绍Java有关集合类,以及对应每个类的用途,同时进行比较集合类的不同特点来让我们深入了解. 1 Collction接口 Collection是最基本的集合接口,一个Collectio ...

  3. Java 应用运维

    作者:http://blogread.cn/it/article/4918?f=wb 出处:http://blogread.cn/it/article/4918?f=wb Java应用运维    出处 ...

  4. 新手学python-Day2-变量和循环判断

    第二天作业: 初探三级菜单,凭现有知识,注意变量可以不声明,但要提前赋值! 此处shuru = '' 可以不写,因为第7行被赋值了,如果只调用shuru不赋值就会报错 shuru = '' sheng ...

  5. 洛谷P5239 回忆京都

    和 NOIP2016TG 组合数问题 差不多是一样的-- 首先要知道杨辉三角和组合数之间的关系 看一下数据范围,很明显要避免重复计算,而且查询的复杂度要非常小 一看n, m <= 1000 这明 ...

  6. Vue常用的GitHub项目

    Vue常用的GitHub项目(Demo案例) 应用实例 https://github.com/pagekit/pa... pagekit-轻量级的CMS建站系统 https://github.com/ ...

  7. SELECT使用子查询

    SELECT使用子查询   SELECT使用子查询,该子查询会执行多次,  次数是由记录数量决定.效率比较低,不推荐使用.  //查询部门编号,工资大于等于2000的人数,  //工资小于2000的人 ...

  8. soapUI 5.1.2 下载以及破解

    转:https://blog.csdn.net/weiqing723/article/details/78865734

  9. Eureka Server添加用户认证

    Eureka Server添加用户认证 学习了:http://blog.csdn.net/liuchuanhong1/article/details/54729556 注意:1,需要使用 defaul ...

  10. poj 2612 Mine Sweeper

    Mine Sweeper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6429   Accepted: 2500 Desc ...