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. Java核心知识体系7:线程安全性讨论

    Java核心知识体系1:泛型机制详解 Java核心知识体系2:注解机制详解 Java核心知识体系3:异常机制详解 Java核心知识体系4:AOP原理和切面应用 Java核心知识体系5:反射机制详解 J ...

  2. java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present

    完整的报错信息 java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present at ...

  3. 牛客小白月赛2 D题虚虚实实

    题目链接:https://www.nowcoder.com/acm/contest/86/D 解题思路:这题目就是判断是否存在欧拉路径.由无向图存在欧拉路径的充分必要条件可知先判断是否联通,再判断是否 ...

  4. 2023-12-20:用go语言,给定一个数组arr,长度为n,在其中要选两个不相交的子数组。 两个子数组的累加和都要是T,返回所有满足情况中,两个子数组长度之和最小是多少? 如果没有有效方法,返回-

    2023-12-20:用go语言,给定一个数组arr,长度为n,在其中要选两个不相交的子数组. 两个子数组的累加和都要是T,返回所有满足情况中,两个子数组长度之和最小是多少? 如果没有有效方法,返回- ...

  5. 使用vLLM和ChatGLM3-6b批量推理

    当数据量大的时候,比如百万级别,使用 ChatGLM3-6b 推理的速度是很慢的.发现使用 vLLM 和 ChatGLM3-6b 批量推理极大的提高了推理效率.本文主要通过一个简单的例子进行实践. 1 ...

  6. IIS下使用SSL证书

    IIS下使用SSL证书 本文介绍windowsServer下SSL证书配置及IIS站点配置 1.    生成SSL证书 在阿里云申请免费SSL证书 登录阿里云管理控制台,打开SSL证书管理 选择免费证 ...

  7. 华企盾DSC防泄密申请解密、外发等失败常见处理方法

    1.检查文件是否已经打开或被占用,以及文件的权限不是只读(错误代码32或5,这种情况比较常见) 2.系统用户名不能带特殊字符.老版本文件路径中不能含特殊字符(包括备份路径) 3.备份路径是否有读写权限 ...

  8. 复习:Java基础-泛型方法

    泛型 大家都很熟悉了 泛型方法呢 可能很多小伙伴都有混淆,今天来稍微复习一下 泛型方法(普通方法) public class Test<T> { public T f(T c) { //注 ...

  9. 在eclipse中拖动项目到Tomcat服务器中报错:Project facet Java version 16 is not supported.解决办法

    补充,还有一种情况:拖不进来,但是根本不报错,解决办法:

  10. ASR项目实战-决策点

    针对语音识别的产品,分别记录设计.开发过程中的决策点. 实时语音识别 对于实时语音识别来说,客户端和服务端之间实时交换语音数据和识别的结果. 客户端在启动识别时,即开始发送语音数据,期望在等待较短的时 ...