poj-1511

从1节点到所有节点的最短路和,加上所有节点返回1节点的最短路和,刚开始的方法时间复杂度有毒啊

其实只要把边全反向重装一次就好了哈哈哈


好了就是这样,套路了一个dijkstra+优先队列
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1000000 + 100;
typedef long long ll;
int n, m;
int dis[maxn];
int vis[maxn];
int head[maxn]; struct Node {
int to;
int len;
int next;
}G[maxn]; bool operator < (const Node a, const Node b) {
if (a.len > b.len) return true;
else return false;
}
int cnt = 1; void insert(int be, int en, int len) {
G[cnt].to = en; G[cnt].next = head[be]; head[be] = cnt; G[cnt].len = len;//头插法
cnt++;
} int dijstra(int be) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i <= n; i++) dis[i] = INF;
dis[be] = 0;
priority_queue<Node>que;
Node a;
a.to = be;
a.len = 0;
que.push(a);
while (!que.empty()) {
Node ans = que.top();
que.pop();
if (vis[ans.to] == 0) {
vis[ans.to] = 1;
for (int i = head[ans.to]; i; i = G[i].next) {
int p = G[i].to;
if (!vis[p] && dis[p] > dis[ans.to] + G[i].len) {
dis[p] = dis[ans.to] + G[i].len;
Node ac;
ac.len = dis[p];
ac.to = p;
que.push(ac);
}
}
}
}
return 0;
}
int T;
int list_be[maxn];
int list_en[maxn];
int list_val[maxn];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &m);
memset(head, 0, sizeof(head));
cnt = 1;
int a, b, c;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &a, &b, &c);
list_be[i] = a;
list_en[i] = b;
list_val[i] = c;
insert(a, b, c);
}
dijstra(1);
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += dis[i];
}
memset(head, 0, sizeof(head));
cnt = 1;
for (int i = 0; i < m; i++) {
insert(list_en[i], list_be[i], list_val[i]);
}
dijstra(1);
for (int i = 1; i <= n; i++) {
ans += dis[i];
}
printf("%lld\n", ans);
}
return 0;
}
poj-1511的更多相关文章
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- HDU 1535 Invitation Cards (POJ 1511)
两次SPFA. 求 来 和 回 的最短路之和. 用Dijkstra+邻接矩阵确实好写+方便交换.可是这个有1000000个点.矩阵开不了. d1[]为 1~N 的最短路. 将全部边的 邻点 交换. d ...
- 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 / ...
- poj 1511(spfa)
---恢复内容开始--- http://poj.org/problem?id=1511 一个spfa类的模板水题. 题意:就是求从1到n个点的来回的所有距离和. 对spfa类的题还是不太熟练,感觉还是 ...
- Poj(1511),SPFA
题目链接:http://poj.org/problem?id=1511 嗯,最后一次写SPFA了,以后就套模板了. 题意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返 ...
- poj 1511(SPFA+邻接表)
题目链接:http://poj.org/problem?id=1511 思路:题目意思很简单就是要求源点到各点的最短路之和,然后再求各点到源点的最短路之和,其实就是建两个图就ok了,其中一个建反图.1 ...
- POJ 1511 - Invitation Cards (dijkstra优先队列)
题目链接:http://poj.org/problem?id=1511 就是求从起点到其他点的最短距离加上其他点到起点的最短距离的和 , 注意路是单向的. 因为点和边很多, 所以用dijkstra优先 ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- (最短路 SPFA)Invitation Cards -- poj -- 1511
链接: http://poj.org/problem?id=1511 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82829#probl ...
- POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...
随机推荐
- oracle函数 MAX([distinct|all]x)
[功能]统计数据表选中行x列的最大值. [参数]all表示对所有的值求最大值,distinct只对不同的值求最大值,默认为all 如果有参数distinct或all,需有空格与x(列)隔开. [参数] ...
- javascript导图 标签: javascript 2015-12-06 16:37 721人阅读 评论(24)
- @bzoj - 4380@ [POI2015] Myjnie
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有 n 家洗车店从左往右排成一排,每家店都有一个正整数价格 p[ ...
- Linux 正文处理命令及tar命令 利用vi编辑器创建和编辑正文文件
要点回顾 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cp /etc/passwd . cat ./passwd >1.txt cp /etc/group ...
- zoj 3859 DoIt is Being Flooded (MFSet && Flood Fill)
ZOJ :: Problems :: Show Problem 这题开始的时候想不到怎么调整每个grid的实际淹没时间,于是只好找了下watashi的题解,发现这个操作还是挺简单的. ZOJ3354 ...
- ajax上传文件 基于jquery form表单上传文件
<script src="/static/js/jquery.js"></script><script> $("#reg-btn&qu ...
- 如何创建私有pod三方库
1.先登录github或者开源中国码云,创建远程仓库,用来存放库文件代码 仓库创建完成,得到远程仓库地址,并保存备用 2.创建本地代码库 打开终端,cd到你想创建的文件夹下,使用命令:pod lib ...
- Flex AIR应用拍照功能(Android和IOS版本)
说明: 使用AIR处理拍照后的回调.照片文件的保存功能时,针对于IOS和Android两个平台是有所不同的. 但.关于如何调用摄像头进行拍照这个功能,Android和IOS是一致的. 技术实现: 1) ...
- 读取Flex AIR应用程序设置
说明: 本人之前做过一年的Flex AIR移动跨平台开发,在之前学习过程中,一直是将笔记记在了Evernote上,有的笔记是自己写的,也有的笔记是在网上看到,顺手记下了的. 所以在这里声明下,如果在网 ...
- Python--day21--包
包: 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高 ...