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 ...
随机推荐
- Java 绘制环形的文字 (Circle Text Demo)
1. [代码]CircleTextDemo.java import java.awt.*;import java.awt.event.*;import java.awt.geom.*; /** ...
- Eclipse慢慢学会的快捷键
Java编辑器 添加单个import Ctrl+Shift+M Java编辑器 组织多个import Ctrl+Shift+O Ctrl+M切换窗口的大小 Ctrl+D删除当前行 ---------- ...
- 理解Objective-C Runtime (五)协议与分类
Objective-C中的分类允许我们通过给一个类添加方法来扩充它(但是通过category不能添加新的实例变量),并且我们不需要访问类中的代码就可以做到. Objective-C中的协议是普遍存在的 ...
- 区间DP中的环形DP
vijos1312 链接:www.vijos.org/p/1312 题目分析:经典的环形DP(区间DP) 环形DP,首先解环过程,把数组复制一遍,n个数变成2n个数,从而实现解环 dp[i][j]表示 ...
- 虚拟机bridged, NAT and host-only网络区别
In Linux, a network of each type is created when running vmware-config.pl. In Windows, they are auto ...
- 洛谷P1247取火柴游戏
题目:https://www.luogu.org/problemnew/show/P1247 可以知道必败局面为n[1]^n[2]^...^n[k]=x=0: 而若x不等于0,则一定可以取一次使其变为 ...
- 在linux下用tomcat部署java web项目的过程与注意事项(转)
在linux下用tomcat部署java web项目的过程与注意事项一.安装JDK到http://www.oracle.com/technetwork/java/javase/downloads/in ...
- 【转】java对象——new对象的理解
学了好长时间的java对于java中的对象一直没有理清楚,今天楼主对java中的对象进行了整理,希望对大家有帮助. 理解和使用java中的对象,我们首先了解一下构造方法与对象的创建. 类是面向对象语 ...
- 【旧文章搬运】再谈隐藏进程中的DLL模块
原文发表于百度空间,2009-09-17========================================================================== 相当老的话 ...
- 【旧文章搬运】Windbg+Vmware驱动调试入门(一)---Windbg的设置
原文发表于百度空间,2009-01-08========================================================================== Windb ...