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. H.264中的帧

    导言 高级视频编码 (AVC) 也称为 H.264,是使用最广泛的视频压缩标准.它与所有主要的流式传输协议和容器格式兼容. 当我们使用播放器播放一个视频时,通常会经过:解协议,解封装,音视频解码,音视 ...

  2. 京东面试:说说Cookie、Session和Token的区别?

    东子作为目前传统电商三巨头之一(其他还有阿里巴巴和拼多多),其面试题的难度也中规中矩,总体来说没有其他两家面试难度高,当然薪资也没有其他两家薪资高. 其中拼多多的薪资最为离谱,尤其是前几年,听说挖同行 ...

  3. force语句

    类似于assign,用于调试,可以强制给赋值,放在initial后,可以穿透到最内部模块. force (强制赋值操作)与 release(取消强制赋值)表示第二类过程连续赋值语句. 使用方法和效果, ...

  4. 循环依赖导致编译或者服务启动报错问题:The dependencies of some of the beans in the application context form a cycle

    错误如图: 我的是服务器启动服务时报错: ***************************APPLICATION FAILED TO START************************* ...

  5. eclipse的问题

    在我使用eclipse的时候出现的一个问题 就是因为汉化之后出现的问题 问题的解决办法 点击帮助 关于 安装细节 选中Babel Language Pack for rt.rap in Chinese ...

  6. springMvc_快速入门

    概念:是一种基于Java实现mvc模型的轻量级web框架 优点:使用简单,开发便捷    灵活性强 总体来说springMvc就是来替代servlet的一种工具 快速入门: 1.创建maven-web ...

  7. Selenium-[实例]猫眼电影爬取

    import random import time from selenium import webdriver from selenium.webdriver import ActionChains ...

  8. python tkinter使用(五)

    python tkinter使用(五) 本篇文章讲述tkinter 中treeview的使用 Treeview是一个多列列表框,可以显示层次数据. #!/usr/bin/python3 # -*- c ...

  9. Kernel Memory 入门系列:自定义处理流程

    Kernel Memory 入门系列:自定义处理流程 在整个文档预处理的流程中,涉及到很多的处理步骤,例如:文本提取,文本分片,向量化和存储.这些步骤是Kernel Memory中的默认提供的处理方法 ...

  10. Java使用HttpUtil.request方法可以发送请求即【Java访问url得到响应数据】

    Java使用HttpUtil.request方法可以发送请求即[Java访问url得到响应数据] 注:这个工具类可以在网上找,也可以自己手写 ,手写的话需要用到以下依赖: <dependency ...