昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= =

试一试邻接表

写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [1000000] 导致 struct 爆栈,此问题亟待解决..

实力碾压SPFA 2500 ms,有图为证

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define MAXP (1000000 + 10)
#define MAXQ MAXP
#define INF 2000000000
using namespace std; struct Edge{
int u, v, w;
}e[MAXQ]; //struct Graph{
static int firstEdge[MAXP], cnt;
struct E{
int v, w, next;
E(){}
E(int U, int V, int W):v(V),w(W),next(firstEdge[U]){}
}edges[MAXQ];
inline void reset(){
memset(firstEdge, -1, sizeof(firstEdge));
cnt = 0;
}
inline void addEdge(int u, int v, int w){
edges[cnt] = E(u, v, w);
firstEdge[u] = cnt++;
}
//}g; int d1[MAXP], d2[MAXP], *d; template <class T>
T min(T &a, T &b){
return a < b ? a : b;
} //struct BHeap{
int heap[MAXP], n, index[MAXP];
//BHeap(){}
inline void up(int i){
for (int j = i >> 1; j > 0; j >>= 1){
if (d[heap[i]] < d[heap[j]]){
swap(index[heap[i]], index[heap[j]]);
swap(heap[i], heap[j]);
i = j;
}
else break;
}
}
inline void down(int i){
for (int j = i << 1; j <= n; j <<= 1){
j += (j < n) && (d[heap[j]] > d[heap[j + 1]]);
if (d[heap[i]] > d[heap[j]]){
swap(index[heap[i]], index[heap[j]]);
swap(heap[i], heap[j]);
i = j;
}
else break;
}
}
inline void push(int i){
heap[++n] = i;
index[i] = n;
up(n);
}
inline int pop(){
if (!n) return 0;
swap(index[heap[1]], index[heap[n]]);
swap(heap[1], heap[n--]);
down(1);
return heap[n + 1];
}
void BHeap(int N){
n = 0;
for (int i = 2; i <= N; i++){
push(i);
}
}
//}heap; bool been[MAXP]; void Dijkstra(){
memset(been, 0, sizeof(been));
been[1] = 1;
while (int v = pop()){
been[v] = 1;
for (int i = firstEdge[v]; ~i; i = edges[i].next){
if (!been[edges[i].v]){
if (d[edges[i].v] > d[v] + edges[i].w){
d[edges[i].v] = d[v] + edges[i].w;
up(index[edges[i].v]);
}
}
}
}
} int main(){
int n, p, q;
freopen("fin.c", "r", stdin);
scanf("%d", &n);
while (n--){
scanf("%d%d", &p, &q);
for (int i = 0; i < q; i++){
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
}
reset();
d = d1;
for (int i = 1; i <= p; i++){
d[i] = INF;
}
for (int i = 0; i < q; i++){
addEdge(e[i].u, e[i].v, e[i].w);
if (e[i].u == 1){
d[e[i].v] = min(d[e[i].v], e[i].w);
}
}
BHeap(p);
Dijkstra(); reset();
d = d2;
for (int i = 1; i <= p; i++){
d[i] = INF;
}
for (int i = 0; i < q; i++){
addEdge(e[i].v, e[i].u, e[i].w);
if (e[i].v == 1){
d[e[i].u] = min(d[e[i].u], e[i].w);
}
}
BHeap(p);
Dijkstra(); long long ans = 0;
for (int i = 2; i <= p; i++){
ans += d1[i] + d2[i];
}
printf("%lld\n", ans);
}
}

POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化的更多相关文章

  1. 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 / ...

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

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

  3. POJ 1511 - Invitation Cards (dijkstra优先队列)

    题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...

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

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

  5. POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra

    传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...

  6. POJ - 1511 Invitation Cards(Dijkstra变形题)

    题意: 给定一个有向图,求从源点到其他各点的往返最短路径和.且这个图有一个性质:任何一个环都会经过源点. 图中的节点个数范围:0-100w; 分析: 我们先可以利用Dijkstra算法求解从源点到其余 ...

  7. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  8. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  9. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

随机推荐

  1. JSBinding / Memory Management (GC)

    C# and JavaScript both have Garbage Collection (GC). They should not conflict with each other. Class ...

  2. 化繁为简 如何向老婆解释MapReduce?(转载)

    化繁为简 如何向老婆解释MapReduce? 昨天,我在Xebia印度办公室发表了一个关于MapReduce的演说.演说进行得很顺利,听众们都能够理解MapReduce的概念(根据他们的反馈).我成功 ...

  3. Linux下编译安装PCRE库

    备注:如果没有root权限,使用 --prefix 指定安装路径 ./configure --prefix=/home/work/tools/pcre-8.xx =================== ...

  4. VSFTP服务器

    vsftpd 是"very secure FTP daemon"的缩写,安全性是它的一个最大的特点.vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 ...

  5. mpstat, pidstat, iostat和sar

    在我们上一篇文章中,我们已经学习了如何去安装和更新sysstat,并且了解了包中的一些实用工具. 今天,我们将会通过一些有趣的实例来学习mpstat, pidstat, iostat和sar等工具,这 ...

  6. 浏览器收藏夹插件-Xmarks

    Xmarks 一一 一款简约实用的浏览器书签同步插件 首先还是想吐槽一下firefox的收藏夹同步功能,感觉不实用,密钥的长度如果不是存到手机或者别的终端,压根没办法实现同步. 而且还区分了,如果两台 ...

  7. ASP.NET MVC+EF5 开发常用代码

      Asp.Net Mvc,EF 技术常用点总结 1.Asp.Net MVC a)获得当前控制器名和当前操作的名称(action) 1.Action 中 RouteData.Values[" ...

  8. webform 创建树

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  9. 企业架构(Enterprise Architecture)

    ylbtech-Miscellaneos: 企业架构(Enterprise Architecture) A,返回顶部 1, 简称EA.是指对企业事业信息管理系统中具有体系的.普遍性的问题而提供的通用解 ...

  10. bootstrap-轮播图

    <!-- 1.写一个父级,class为carousel slide:添加滑动的效果 data-interval 图片轮播间隔时间,单位ms data-ride="carousel&qu ...