POJ :1511 http://poj.org/problem?id=1511

思路

  • 求1号点到其他点的最短路 + 其他点到1号点的最短。 结果用long long。
  • 快读可以加快时间

代码

  • spfa 890ms
#ifdef ONLINE_JUDGE
#pragma warning (disable : 4996)
#endif // ONLINE_JUDHE
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, inf = 0x3f;
struct Edge {
int v, w, next;
}e[M];
int n, m, t, u, v, w, len, h[N], rh[N], d[N], rd[N]; //rh[]是反向建图
bool book[N]; inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch<'0' || ch>'9') { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= '0'&&ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
} void add(int h[], int u, int v, int w) {
e[len].w = w;
e[len].v = v;
e[len].next = h[u];
h[u] = len++;
} void spfa(int d[], int h[]) {
memset(d, 0x3f, sizeof(rd));
d[1] = 0;
queue<int> q;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
book[u] = false;
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
int w = d[u] + e[j].w;
if (d[v] > w) {
d[v] = w;
if (!book[v]) q.push(v), book[v] = true;
}
}
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int T;
cin >> T;
while (T--) {
memset(h, 0, sizeof(h)); len = 1;
memset(rh, 0, sizeof(rh));
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
u = read(); v = read(); w = read();
add(h, u, v, w);
add(rh, v, u, w);
}
spfa(d, h);//求1回其他各点的最短路
spfa(rd, rh);//求其他各点到1的最短路
long long ans = 0;
for (int i = 1; i <= n; ++i)ans += d[i] + rd[i];
cout << ans << endl; }
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif // !ONLINE_JUDGE
return 0;
}
  • Djkstra 2125ms...
#ifdef ONLINE_JUDGE
#pragma warning (disable : 4996)
#endif // ONLINE_JUDHE
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, inf = 0x3f;
struct Edge {
int v, w, next;
}e[M];
struct Node {
int d, v;
Node(int d,int v): d(d),v(v){}
Node(){}
bool operator < (const Node & w)const {
return d > w.d;
}
};
int n, m, t, u, v, w, len, h[N], rh[N], d[N], rd[N]; //rh[]是反向建图
bool book[N]; void add(int h[], int u, int v, int w) {
e[len].w = w;
e[len].v = v;
e[len].next = h[u];
h[u] = len++;
} void djkstra(int d[], int h[]) {
memset(d, 0x3f, sizeof(rd));
memset(book, false, sizeof(book));
d[1] = 0;
priority_queue<Node> q;
q.push(Node(0, 1));
while (!q.empty()) {
int u = q.top().v;
q.pop();
if (book[u]) continue;
book[u] = true;
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
int w = d[u] + e[j].w;
if (d[v] > w) {
d[v] = w;
q.push(Node{ d[v],v });
}
}
}
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d", &t);
while (t--) {
memset(h, 0, sizeof(h)); len = 1;
memset(rh, 0, sizeof(rh));
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
add(h, u, v, w);
add(rh, v, u, w);
}
djkstra(d, h); //求1回其他各点的最短路
djkstra(rd, rh);//求其他各点到1的最短路
long long ans = 0;
for (int i = 1; i <= n; i++) ans += d[i] + rd[i];
printf("%lld\n", ans);
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("out.txt");
#endif // !ONLINE_JUDGE
return 0;
}

POJ:1511 Invitation Cards(双向搜索最短路径)的更多相关文章

  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

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

  3. SPFA算法(2) POJ 1511 Invitation Cards

    原题: Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 31230   Accepted: ...

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

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

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

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

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

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

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

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

  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,SPFA。

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

  10. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

随机推荐

  1. C#中LINQ的使用知多少?LINQ常功能整理,实例源代码解析

    LINQ(Language-Integrated Query)是C#语言中的一个强大的查询技术,它提供了一种统一的查询语法,可以用于查询和操作各种数据源,包括集合.数据库.XML等.下面详细描述了LI ...

  2. 小满OKKICRM与金蝶云星空对接集成客户资料

    小满OKKICRM与金蝶云星空对接集成客户列表查询(更新列表)&客户新增(小满客户对接金蝶客户-P) 数据源平台:小满OKKICRM 小满科技成立于2013年,是阿里巴巴集团战略投资的高新技术 ...

  3. Linux RN6752 驱动编写

    一.概述 关于 RN6752V1 这个芯片这里就不做介绍了,看到这篇笔记的小伙伴应该都明白,虽然说 RN6752V1 芯片是 AHD 信号的解码芯片,但是也可以把芯片当做是一个 YUV 信号的 MIP ...

  4. Web前端工程的装机必备软件

    前言 最近作者的电脑 C 盘变红了,这让我很难受(有点小强迫症),所以准备重新安装下系统,顺便把 C 盘扩大点. 注意: 操作系统是 windows 11 23H2. 所有的命令行都是使用 Windo ...

  5. CTT2023 邮寄

    从广州被邮寄到了苏州.还有点感冒有点咳嗽,体温 37 度.还是来了. Day 0 清早坐 xp 的车,早上坐飞机,中午坐高铁,下午坐大巴,风尘仆仆地赶到了苏州. 飞机上有一套省选题要验,看了两眼,T1 ...

  6. NVIDIA RTX4090,你能用它做什么?

    都说男生是世界上最简单的动物,为什么呢?举个例子,你要给女朋友送礼,你可以选择包.口红.护肤品.化妆品等,而包的品牌和样式.口红的色号等足以让你挑得眼花缭乱.而男生不一样,如果女生选择给男生送礼,我相 ...

  7. Rong晔大佬教程学习(1):背景与项目设计目标

    riscv实际上是一种ISA的指令集,而处理器的设计的基本结构是不变的(如下所示),其区别在于所选用的指令集的类型,一般有ARM.RISCV.MIPS等,采用了不同的引擎,那么车的外观.系统等也会随之 ...

  8. 【笔记整理】xpath复习

    又是xpath.... 10年前学java就学过了...哈哈哈 要就看w3cschool的教程就行了, 函数部分会用得到: https://www.w3school.com.cn/xpath/xpat ...

  9. Alpha-Beta剪枝的原理的深入理解(无图预警)

    转载请注明 原文链接 :https://www.cnblogs.com/Multya/p/17929261.html 考虑一个树: 一棵树上只有叶子节点有值,有确定的根节点的位置 根据层数来划分叶子节 ...

  10. OpenEuler22.03安装PostgreSQL15.5并配置一主二从

    环境准备 序号 IP 标识(hostname) CPU/内存配置 系统盘 数据盘 1 192.168.8.190 pg01 8C+16G 80G 500G 2 192.168.8.191 pg02 8 ...