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

求解从1去其他顶点的最短距离之和。

加上其他顶点到1的最短距离之和。

边是单向的。

第一种很容易,直接一个最短路,

然后第二个,需要把边反向建一次,跑一个最短路就好。

★、cin  cout 超时

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
struct HeapNode {
int u, dis;
HeapNode(int from, int cost) : u(from), dis(cost) {}
bool operator < (const struct HeapNode & rhs) const {
return dis > rhs.dis;
}
};
const int maxn = + ;
int book[maxn], DFN, dis[maxn];
int first[maxn], num;
struct Node {
int u, v, w, tonext;
}e[maxn * ];
void add(int u, int v, int w) {
++num;
e[num].u = u, e[num].v = v, e[num].w = w;
e[num].tonext = first[u];
first[u] = num;
}
void dij(int bx, int n) {
++DFN;
for (int i = ; i <= n; ++i) dis[i] = inf;
dis[bx] = ;
priority_queue<HeapNode> que;
while (!que.empty()) que.pop();
que.push(HeapNode(bx, dis[bx]));
while (!que.empty()) {
HeapNode t = que.top();
que.pop();
int u = t.u;
if (book[u] == DFN) continue;
book[u] = DFN;
for (int i = first[u]; i; i = e[i].tonext) {
int v = e[i].v;
if (book[v] != DFN && dis[v] > dis[u] + e[i].w) {
dis[v] = dis[u] + e[i].w;
que.push(HeapNode(v, dis[v]));
}
}
}
}
bool in[maxn], tim[maxn];
bool spfa(int bx, int n) { //从bx开始,有n个顶点
for (int i = ; i <= n; ++i) {
dis[i] = inf;
tim[i] = ; //入队次数清0
in[i] = false; //当前这个节点不在队列里
}
queue<int> que;
while (!que.empty()) que.pop();
que.push(bx), in[bx] = true, dis[bx] = , tim[bx]++;
while (!que.empty()) {
int u = que.front();
if (tim[u] > n) return true; //出现负环
que.pop();
for (int i = first[u]; i; i = e[i].tonext) {
if (dis[e[i].v] > dis[e[i].u] + e[i].w) {
dis[e[i].v] = dis[e[i].u] + e[i].w;
if (!in[e[i].v]) { //不在队列
que.push(e[i].v);
in[e[i].v] = true;
tim[e[i].v]++;
}
}
}
in[u] = false;
}
return false;
}
int u[maxn], v[maxn], w[maxn];
void init(int n) {
for (int i = ; i <= n; ++i) first[i] = ;
num = ;
}
void work() {
int n, m;
scanf("%d%d", &n, &m);
init(n);
for (int i = ; i <= m; ++i) {
// cin >> u[i] >> v[i] >> w[i];
scanf("%d%d%d", &u[i], &v[i], &w[i]);
add(u[i], v[i], w[i]);
}
spfa(, n);
LL ans = ;
for (int i = ; i <= n; ++i) ans += dis[i];
init(n);
for (int i = ; i <= m; ++i) {
add(v[i], u[i], w[i]);
}
spfa(, n);
for (int i = ; i <= n; ++i) ans += dis[i];
printf("%lld\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

Invitation Cards POJ 1511 SPFA || dij + heap的更多相关文章

  1. Invitation Cards POJ - 1511 (双向单源最短路)

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

  2. (最短路 SPFA)Invitation Cards -- poj -- 1511

    链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...

  3. Invitation Cards POJ - 1511

    题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...

  4. POJ1511 Invitation Cards —— 最短路spfa

    题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Tota ...

  5. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  6. POJ 1511 SPFA+邻接表 Invitation Cards

    题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...

  7. poj 1511(spfa)

    ---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...

  8. poj 1511(SPFA+邻接表)

    题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...

  9. poj 3159 Candies (dij + heap)

    3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...

随机推荐

  1. 减肥 day1

    今天是我减肥第一天,现在体重是147斤, 早晨吃了一碗面,喝了一碗奶,中午吃了一个apple. 6点钟去打篮球,晚上去食堂稍微吃一点东西.

  2. ResultSetMetaData类的介绍

    ResultSetMetaData .DatabaseMetaData中的方法介绍 利用ResultSet的getMetaData的方法可以获得ResultSetMeta对象,而ResultSetMe ...

  3. UI标签库专题三:JEECG智能开发平台 FormValidation(表单提交及验证标签)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/28484209  自己定义弹出框提示 ...

  4. noi2014魔法森林

    为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含

  5. KMP 、扩展KMP、Manacher算法 总结

    一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...

  6. javascript flash 弹框

    1. [代码]FlashBox     // JavaScript Documentfunction FlashBox(src,width,height){var docbody = document ...

  7. html5--6-5 CSS选择器2

    html5--6-5 CSS选择器2 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...

  8. 调节音量的各个方法——AudioManager的使用

    AudioManager类位于android.Media包中,该类提供访问控制音量和铃声模式的操作. //获取AudioManager实例对象 AudioManager audioManage = ( ...

  9. I.MX6 sdio 设备注册及识别

    /************************************************************************* * I.MX6 sdio 设备注册及识别 * 说明 ...

  10. linux--vsftpd的安装和配置(转)

    Linux下如何进行FTP设置(转) [TOC] Redhat/CentOS安装vsftp软件 1. 安装vsftp $ yum install vsftpd -y 2. 添加ftp帐号和目录 先检查 ...