POJ:1511 Invitation Cards(双向搜索最短路径)
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(双向搜索最短路径)的更多相关文章
- 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
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- SPFA算法(2) POJ 1511 Invitation Cards
原题: Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 31230 Accepted: ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- 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 : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
随机推荐
- Cadence SPB 22.1 -- 原理图的电器元件放置03Day
1.新增原理图:"SCHEMATIC"-->"New Page" 2.元器件放置 ①.添加原理图库 ②.放置元器件 选择对应元件库,再选择需要放置的元件, ...
- .NET8顶级调试lldb观察FOH堆字符串分配
前言 好久没有动用LLDB了,这种未来的下一代高性能调试器应该是用在Linux内核系统的Arm64/Riscv64/X64系统指令集上的,LLDB Debug .NET有点杀鸡用牛刀.本篇通过它来看下 ...
- [USACO2007NOVS] Cow Hurdles S
题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...
- 带圆角的虚线边框?CSS 不在话下
今天,我们来看这么一个非常常见的切图场景,我们需要一个带圆角的虚线边框,像是这样: 这个我们使用 CSS 还是可以轻松解决的,代码也很简单,核心代码: div { border-radius: 25p ...
- 解决Tensorflow2.0出现:AttributeError: module 'tensorflow' has no attribute 'get_default_graph'的问题
问题描述 在使用tensorflow2.0时,遇到了这个问题: AttributeError: module 'tensorflow' has no attribute 'get_default_gr ...
- 数字孪生与GIS结合趋势背后,是市场需求的变化
随着数字化时代的来临,数字孪生和地理信息系统(GIS)作为两个独立的技术领域,正日益融合并发挥着协同作用.这一趋势的背后,是市场需求的变化和对更智能.更精准.更实用的解决方案的追求. 数字孪生与GIS ...
- Volcano 原理、源码分析(一)
0. 总结前置 1. 概述 2. Volcano 核心概念 2.1 认识 Queue.PodGroup 和 VolcanoJob 2.2. Queue.PodGroup 和 VolcanoJob 的关 ...
- Pikachu漏洞靶场 File Inclusion(文件包含漏洞)
File Inclusion(文件包含漏洞) 本地文件包含 url: 192.168.171.30/pikachu/vul/fileinclude/fi_local.php?filename=file ...
- Java 8升级Java 11,升级必知要点!竟然有这些坑…
随着技术的不断进步,Java作为一种广泛使用的编程语言,其版本更新带来了许多新特性和性能提升.从Java 8升级到Java 11,是一个重要的转变,它不仅带来了新的编程范式,还引入了对现代软件开发的多 ...
- 快速掌握服务网格系列二:云原生、K8S、服务网格(Service Mesh)及微服务之间的关系
快速掌握服务网格系列二:云原生.K8S.服务网格(Service Mesh)及微服务之间的关系 首先看下CNCF对云原生的定义: Cloud native technologies empower o ...