Invitation Cards POJ - 1511
题目链接:https://vjudge.net/problem/POJ-1511
思路:题目意思就是,从1出发到所有城市,再从所有城市回到1的最短时间。
那么我们只要正跑一次图,然后反向存边,再跑一次图,把所有单源最短路相加就是答案了。
emmm,这题,很卡时间,作为一个懒人,用c++的输入输出,加了简单的快读,用了链式前项星,
还是险些通过,感觉遇到很大的输入数据,用c++时,最好减少空间申请和回收的开销,不然会卡。
我一个优先队列在函数申请用,T了,在全局申请,再清空就A了。。。当然,这是之前的,后面改了下,
emm,7780ms。。。当然,这题用dijkstra一定要堆优化一下,点有1e6之多。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf 1e10
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = 1e6 + ;
int head[N];
int vis[N];
int dis[N];
int U[N],V[N],W[N];
int cnt;
LL ans; int n,m; struct Edge{
int to;
int w;
int next;
}e[N]; struct node{
int loc;
int w; bool friend operator< (const node& a,const node& b){
return a.w > b.w;
}
};
priority_queue<node > que; void add(int u,int v,int w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void dijkstra(){ while(!que.empty()) que.pop();
rep(i,,n) vis[i] = false;
rep(i,,n) dis[i] = inf;
dis[] = ;
que.push(node{,}); while(!que.empty()){
int u = que.top().loc;
que.pop();
if(vis[u]) continue;
vis[u] = true; for(int o = head[u]; ~o; o = e[o].next){
int v = e[o].to;
int w = e[o].w; if(!vis[v] && dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
que.push(node{v,dis[v]});
}
}
}
rep(i,,n) ans += dis[i];
} int main(){ ios::sync_with_stdio(false);
cin.tie(); int T;
cin >> T;
while(T--){
cin >> n >> m; rep(i,,n) head[i] = -;
cnt = ; int u,v,w;
rep(i,,m){
cin >> U[i] >> V[i] >> W[i];
add(U[i],V[i],W[i]); //正向存边
} ans = ;
dijkstra(); rep(i,,n) head[i] = -;
cnt = ;
rep(i,,m){
add(V[i],U[i],W[i]); //反向存边
} dijkstra();
cout << ans << endl;
} getchar(); getchar();
return ;
}
Invitation Cards POJ - 1511的更多相关文章
- 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 SPFA || dij + heap
http://poj.org/problem?id=1511 求解从1去其他顶点的最短距离之和. 加上其他顶点到1的最短距离之和. 边是单向的. 第一种很容易,直接一个最短路, 然后第二个,需要把边反 ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- 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 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
随机推荐
- HTML与CSS学习笔记(5)
1.文字阴影?(针对文字) text-shadow: 例如 text-shadow: 10px 10px 10px blue;四个值分别是 x y blur color blur表示模糊值,越大越模糊 ...
- 【Eureka篇三】Eureka常用配置说明(7)
服务注册中心配置(Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean) #关闭注册中心的保护机制, ...
- Mybatis动态SQL(五)
if choose (when, otherwise) trim (where, set) foreach 一.if 动态SQL通常要做的事情是有条件地包含 where 子句的一部分.比如: < ...
- BZOJ1688 「USACO05OPEN」Disease Manangement 背包+状压DP
问题描述 BZOJ1688 题解 背包,在转移过程中使用状压. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; ...
- InvalidProgramException
InvalidProgramException 这tmd是个什么错,我现在都想不起这个exception是怎么触发的了. 后来google了一下,发现是.net 2.0的编译器的bug,和内存或堆栈使 ...
- k8s 二进制部署详解
环境说明: 192.168.1.101 -- master01 + etcd01 192.168.1.102 -- etcd02 192.168.1.103 -- etcd03 192.168.1.1 ...
- mysql-新增数据表
新增数据表之前,需确保已经存在数据库,如还没有数据库请先参考上一篇文章新增数据库 1.创建表 create table test( id int PRIMARY KEY, name varcha ...
- vue开发 element的select下拉框设定初值后,不能重新选择的问题
问题描述: 用的element的select可多选的下拉选框,在回显后有初始值的情况下,不能修改,也不能再选择 如图,明明点击了一般内勤主管,但没有任何反应 <el-select v-model ...
- let/const特性
let: 1.声明的变量不存在预解析: console.log(a); let a=1; 2.变量名不允许重复(在同一作用域下): { let a=1; let a=2; console.lo ...
- SQL Server 通过“with as”方法查询树型结构
一.with as 公用表表达式 类似VIEW,但是不并没有创建对象,WITH AS 公用表表达式不创建对象,只能被后随的SELECT语句,其作用: 1. 实现递归查询(树形结构) 2. 可以在一个 ...