POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化
昨天的题太水了,堆优化跑的不爽,今天换了一个题,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堆优化的更多相关文章
- 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 / ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 - Invitation Cards (dijkstra优先队列)
题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...
- POJ - 1511 Invitation Cards(Dijkstra变形题)
题意: 给定一个有向图,求从源点到其他各点的往返最短路径和.且这个图有一个性质:任何一个环都会经过源点. 图中的节点个数范围:0-100w; 分析: 我们先可以利用Dijkstra算法求解从源点到其余 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
随机推荐
- javascript的document.execCommand(转)
document.execCommand()方法处理html数据时常用语法格式如下: 代码: document.execCommand(sCommand[,交互方式, 动态参数]) 其 中:sComm ...
- docvalues和Fieldcache
Fieldcache: docID->document->fieldvalue 无论是聚类排序关联等,首先都需要获得文档中某个字段的值,通过docID去获得整个document,然后再去 ...
- IO-同步,异步,阻塞,非阻塞
IO-同步,异步,阻塞,非阻塞1.什么是IO数据在系统内核(kernel)和用户进程之间的传递,称为IO. 2.IO操作步骤以read为例,涉及两个系统对象,调用IO的process(or threa ...
- 使Maven 2在package、install等阶段跳过运行Test的配置
方法1: To skip running the tests for a particular project, set the skipTests property to true.<proj ...
- 怎么 才能显示Eclipse中Console的全部内容
可以如下设置 preference->run/debug->console 设置limit console output 为false,方便调试时,查看全部console. 这个真是太有用 ...
- 入口点函数的19种消息,AcRxArxApp只处理16种。
AcRx::AppMsgCode一共有19种消息. 但由IMPLEMENT_ARX_ENTRYPOINT宏实现的App类,只处理了16种消息. 缺: kSuspendMsg = 16, kIni ...
- Autocad 常用命令
一律使用的简写 1:将多条线段合并成一条多线段[pe] 2:如果发现合并的多线段将自己不想合并的区域合并进去了,别担心,炸开就行了 [x] 3:如果画的线段太长工作区不适应,使用缩放命令.[scale ...
- Spring学习 Ioc篇(二 )
5.spring依赖注入的方式 方法一:使用构造器方式进行注入 1.dao的类和接口 package com.cvicse.dao.impl; import com.cvicse.dao.Person ...
- RMAN_学习笔记4_RMAN Virtual Catalog虚拟恢复目录
2014-01-01 Created By BaoXinjian Thanks and Regards
- gerrit error: unpack failed: error Permission denied
gerrit服务器迁移后,clone和pull代码到本地,都没问题. 但是,push时,报错: 查看了下git版本库存储目录,发现git下版本库镜像文件owner都是root.因为之前安装的gerri ...