J - Invitation Cards POJ - 1511
J - Invitation Cards POJ - 1511
题意: 共有 n 个站点,n 个志愿者从1 出发,分别到达不同的站点,晚上全部回到 1 ,求他们总的最小花费是多少?
思路: 首先想到从 1 出发跑一遍 DJ, 然后再以每一个点为起点出发跑 n-1 遍 DJ ,求出所有 dis[1] + dis2[i] 即可,但是看了一下复杂度, n <= 1e6, 炸了
优化一下,从 1 出发跑一边 DJ 后,反向建图,然后再从 1 出发跑一遍,答案就有了
复杂度:O(2*nlogn)=4e7
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
typedef pair<ll,int> PII;
const int maxn = 1e6+10;
int n, m;int u[maxn], v[maxn], w[maxn];inline int read() {char c = getchar(); int f = 1, x = 0;while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}return f * x;
}int head[maxn], cnt;
bool vis[maxn];
struct node{int to, next, w;
}e[maxn];void add(int u, int v, int w) {e[++cnt].next = head[u];e[cnt].to = v;e[cnt].w = w;head[u] = cnt;
}ll dis[maxn];void init() {memset(vis, 0, sizeof(vis));memset(head, 0, sizeof(head));cnt = 0;memset(dis, 0x3f3f3f3f3f3f3f3f, sizeof(dis));memset(e, 0, sizeof(e));
}ll dijkstra() { // 每次取出与连通块相邻的所有边中最小的一条加入连通图,直至所有边都在连通块内 dis[1] = 0;priority_queue<PII, vector<PII>, greater<PII> > heap;heap.push({0, 1});while(heap.size()) {PII now = heap.top(); heap.pop();ll dist = now.first; int u = now.second;if(vis[u]) continue; vis[u] = 1;for(int i = head[u]; i; i = e[i].next) {int to = e[i].to;if(dis[to] > dist + e[i].w) {dis[to] = dist +e[i].w; // 后面进入的点可以更新与其相邻的,先进入的点 heap.push({dis[to], to});}}}// 统计答案ll sum = 0;for(int i = 1; i <= n; i++) sum += dis[i];return sum;
}int main() {// freopen("test.in", "r", stdin);int T = read();while(T--) {ll ans = 0;init();n = read(); m = read();for(int i = 1; i <= m; i++) {u[i] = read(), v[i] = read(), w[i] = read();add(u[i], v[i], w[i]);} ans += dijkstra();init();for(int i = 1; i <= m; i++) add(v[i], u[i], w[i]);ans += dijkstra();printf("%lld\n", ans);}return 0;
}
J - Invitation Cards POJ - 1511相关推荐
- Invitation Cards POJ - 1511【最短路】
Invitation Cards POJ - 1511 题目 思路:建两张图,一张正向一张反向,先在正向图里求一遍一号点到所有点的距离,再在反向图里求一遍,反向图里一号点到所有点的距离的意思是所有点到 ...
- Invitation Cards POJ - 1511 SPFA(dijkstra+反向建图+邻接表(下标过大)+输入输出用stdio(iostream超时))
题目大意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接 到达终点站,是单向的,每条路线有它自己的车费.有P个人早上从1出发 ,他们要到达每一个公交站点, 然后到了晚上再返回点 ...
- Invitation Cards POJ 1511
http://poj.org/problem?id=1511 题意:现有一些ACMERS需要坐车从1号站分别抵达2,3,....N(N<=1000000000)号站,等到晚上后,他们再各自从各自 ...
- D - Silver Cow Party J - Invitation Cards 最短路
http://poj.org/problem?id=3268 题目思路: 直接进行暴力,就是先求出举行party的地方到每一个地方的最短路,然后再求以每一个点为源点跑的最短路. 还有一种方法会快很多, ...
- POJ 1511 Invitation Cards——Dijkstra优先队列优化+反向建图
[题目描述] In the age of television, not many people attend theater performances. Antique Comedians of M ...
- 【POJ - 1511】 Invitation Cards(Dijkstra + 反向建图 多源到单源最短路的处理)
题干: In the age of television, not many people attend theater performances. Antique Comedians of Mali ...
- POJ 1511:Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 26994 Accepted: 895 ...
- Invitation Cards——正反最短路
poj 1511: Invitation Cards 题意:点1到其他点的最短距离+其他点到点1的最短距离 的总和. 1.双向建图 正向建图是1到其他点的距离,反向建图则是其他点到1的距离. 2.数据 ...
- Invitation Cards(建反图 + 跑两遍SPFA)
题目如下: In the age of television, not many people attend theater performances. Antique Comedians of Ma ...
最新文章
- PHP运算符种类很多 主要有,PHP运算符的类别概览
- AWS elastic load balancer里的监听器certificate设置
- 兼容FF,IE的纯CSS下拉菜单
- 【Java从入门到头秃专栏 】(一)学在Java语法之前
- 程序员的大恩人永远地离开了
- python爬虫代码示例 动态_python动态爬虫的实例分享
- 单点登录 cas 设置回调地址_单点登录————CAS
- 万卷书 - 如何在股市中赚钱 How to Make Money in Stocks
- 一个网页设计需求方眼中的网页设计
- Air for ios横版格斗过关项目技术整理
- traceroute不通的解决
- 树莓派制作遥控小车教程
- 图像在空域上的平滑处理
- java jsf_eclipse搭建JSF简单示例的教程(亲测)
- opengl freeglut flew glut安装配置教程 VS2019 Windows10,无需复制文件
- 数据结构程序设计——山东省城际铁路建设建设
- 尚硅谷-谷粒商城-电商项目-秒杀系统-笔记
- 那些花儿(夏洛特烦恼版)
- MySQL主从复制(Windows 10 + CentOS 7混合云) | MySQL8.0.29主从复制(附视频教程)
- 服务器esxi虚拟机升级,ESXi 服务器安装升级补丁
热门文章
- 基于TI TMS320C6678 + Xilinx Kintex-7 的高性能信号处理方案
- 简单几行程序轻松分析Excel
- vue vue-router vuex element-ui axios 的学习笔记(七)完善登录注册
- 洛谷P1417 烹调方案
- 如何让Table显示滚动条
- (转自PHPer)成长的选择
- 转、转、转——陀螺的梦话
- mongodb导入hive
- setleft android,android TextView的setCompoundDrawables()方法
- php 爬虫_Scrapy 爬虫完整案例-基础篇