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 ;
}

最新文章

  1. wcf,jquery,post,跨域
  2. SAP采购订单屏幕增强
  3. CSS命名规则
  4. java异常处理机制Exception
  5. SpringMVC + Spring + MyBatis 学习笔记:遭遇order by 排序问题
  6. COJ 0332 The Flash
  7. mysql 5.6.20的安装、配置服务、设置编码格式
  8. Visual Studio Tips: How to change project namespace
  9. JS脚本语言(全称java script:网页里使用的脚本语言:非常强大的语言):基础语法
  10. Visual Studio 2017正式版安装
  11. tomcat+jdk+mysql
  12. android bitmap的内存分配和优化
  13. 在MySQL中实现Rank高级排名函数【转】
  14. react native 中实现个别页面禁止截屏
  15. MapReduce shuffle过程剖析及调优
  16. TCP/IP 免费ARP
  17. C 线性表的顺序存储实现及插入、删除等操作示例
  18. 20135323符运锦----第七周:Linux内核如何装载和启动一个可执行程序
  19. PHP的内存限制 Allowed memory size of 134217728 bytes exhausted (tried to allocate 1099 bytes) in
  20. recordMyDesktop的安装与使用

热门文章

  1. 关于java赋值操作的原子性问题
  2. Spark SQL is a Spark module for structured data processing.
  3. RabbitMQ使用简述
  4. i2c_set_clientdata函数【转】
  5. IDEA maven dependency自动提示
  6. SILVERLIGHT实现对HTML DOM的访问
  7. Python(2)(基本输入输出语句)
  8. UIButton常见属性和方法
  9. 管理 Word 博客账户
  10. 安卓开发eclipse如何导出项目