昨天的题太水了,堆优化跑的不爽,今天换了一个题,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. 【NOIP2005】过河

    感觉这题好玄--最后看了chty的代码才过,我现在这样必须看题解才能A题怎么办嘛qaq 原题: 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上 ...

  2. [原创]cocos2d-x研习录-第二阶 概念类之布场层类(CCLayer)

    上面说场景CCScene相当于一个大容器,那么布景层类CCLayer就是大容器里的若干个小容器.每个游戏场景CCScene会有很多层CCLayer,每一层CCLayer负责各自的任务.看一下CCLay ...

  3. python 字典操作

    字典的常用操作: clear 功能:清空字典 dict_li = {'users':'swht','age':'18',} dict_li.clear() 返回结果:dict_li {} copy 功 ...

  4. 接口自动化之Postman+Newman

    简介 Postman 使一款可以方便我们调用API的工具,通过Postman 与 Newman结合我们还可以批量运行API达到API自动化测试的目的. Postman 安装 Window 系统需要先安 ...

  5. PHP 载入图像 imagecreatefrom_gif_jpeg_png 系列函数

    imagecreatefrom 系列函数用于从文件或 URL 载入一幅图像. 载入图像 imagecreatefrom 系列函数用于从文件或 URL 载入一幅图像,成功返回图像资源,失败则返回一个空字 ...

  6. [Spring MVC] - InitBinder验证

    Spring MVC使用InitBinder验证: 使用InitBinder做验证的情况一般会在此Controller中提交的数据需要有一些是业务性质的,也即比较复杂的验证情况下才会使用.大部份简单的 ...

  7. IOS开发-CAlayer-锚点小结

    CAlayer层的属性:anchorPoint(锚点)   CAlayer *view; 1.决定着CALayer上的哪个点会在position属性所指的位置(设置以后旋转动画,就是以锚点为中心旋转) ...

  8. STM32F4 SPI2初始化及收发数据【使用库函数】

    我的STM32F4 Discovery上边有一个加速度传感器LIS302DL.在演示工程中,ST的工程师使用这个传感器做了个很令人羡慕的东西:解算开发板的姿态.当开发板倾斜时候,处于最上边的LED点亮 ...

  9. JAVA虚拟机垃圾回收算法原理

    除了释放不再被引用的对象外,垃圾收集器还要处理堆碎块.新的对象分配了空间,不再被引用的对象被释放,所以堆内存的空闲位置介于活动的对象之间.请求分配新对象时可能不得不增大堆空间的大小,虽然可以使用的总空 ...

  10. 38. Same Tree && Symmetric Tree

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...