Invitation Cards POJ 1511 SPFA || dij + heap
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的更多相关文章
- Invitation Cards POJ - 1511 (双向单源最短路)
		
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
 - (最短路  SPFA)Invitation Cards -- poj -- 1511
		
链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...
 - Invitation Cards POJ - 1511
		
题目链接:https://vjudge.net/problem/POJ-1511 思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间. 那么我们只要正跑一次图,然后反向存边,再跑一次 ...
 - POJ1511 Invitation Cards —— 最短路spfa
		
题目链接:http://poj.org/problem?id=1511 Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Tota ...
 - POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
		
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...
 - POJ 1511 SPFA+邻接表 Invitation Cards
		
题目大意: 计算从 1 点 到 其他所有点的 往返距离之和, 因为是 有向图, 所以我们需要将图反存 一次, 然后求两次单源最短路, 结果就出来了. #include <iostream> ...
 - poj 1511(spfa)
		
---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...
 - poj 1511(SPFA+邻接表)
		
题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...
 - poj 3159 Candies (dij + heap)
		
3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...
 
随机推荐
- set -- $variable
			
1 set --的用途 给位置参数赋值. 2 $variable是如何赋值给位置参数的 假如variable=a b c?或者variable=a;b;c? 这里果然和IFS有关,默认情况下,vari ...
 - Flume接收器组的指数退避上限
			
指数退避 agent.sinkgroups.sg1.sinks=k1,k2,k3agent.sinkgroups.sg1.processor.type=failoveragent.sinkgroups ...
 - python selenium实现百度搜索
			
1.环境 python2.7+selenium+phantomjs+linux 2.代码 #-*-coding:utf-8 -*- from selenium import webdriver fro ...
 - html5--6-55 动画效果-关键帧动画
			
html5--6-55 动画效果-关键帧动画 实例 @charset="UTF-8"; div{ width: 150px; height: 150px; font-size: 2 ...
 - Oracle安装:silent安装方式
			
之前一直是通过图形界面来安装oracle,这次上机考试说用silent (静默)形式安装.一点头绪都没有,虽然当时提供了oracle官方文档. 遂查找资料,安装了一下: 一.准备工作: 1.系统参数调 ...
 - 深入理解WeakHashmap
			
转自:http://mikewang.blog.51cto.com/3826268/880775 (一) 查看API文档,WeakHashmap要点如下: 1. 以弱键 实现的基于哈希表的 Map.在 ...
 - Start Developing Mac Apps -- Human Interface Design 用户界面设计
			
Human Interface Design It’s not enough to create an app that works. Users expect Mac apps to be powe ...
 - 用Python在局域网根据IP地址查找计算机名
			
1.要使用socket模块 代码如下: import sys, socket # hostname = socket.gethostname()# print("Host name:&quo ...
 - vim中编辑了代码  但是提示can not write的解决办法和代码对齐办法
			
方式1: 1 :w /tmp/xxxx(如果是c文件就.c拉) 保存在/tmp下面 2 从tmp中复制到有权限的目录下面 cp /tmp xxxx ./(当前目录) 方式2::w !sudo tee ...
 - 基础总结篇之二:Activity的四种launchMode (转载)
			
转自:http://blog.csdn.net/liuhe688/article/details/6754323 合抱之木,生於毫末:九層之台,起於累土:千里之行,始於足下.<老子> 今天 ...