poj 1511 Invitation Cards(dijstra优化)
题目链接:http://poj.org/problem?id=1511
题意:给出n个点和n条有向边,求所有点到源点1的来回最短路之和(保证每个点都可以往返源点1)
题目比较简单就是边和点的个数有点多所以可以用dijstra+优先队列这样复杂度就可以到v*logn
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#define inf 1000000000
using namespace std;
const int M = 1e6 + 10;
int n , m , a[M] , b[M] , c[M] , dis[M];
struct TnT {
int v , w;
};
struct cmp {
bool operator() (int x , int y) {
return dis[x] > dis[y];
}
};
vector<TnT>vc[M];
bool vis[M];
void dij(int s) {
priority_queue<int , vector<int> , cmp>q;
memset(vis , false , sizeof(vis));
TnT gg;
q.push(s);
dis[s] = 0;
while(!q.empty()) {
int m = q.top();
vis[m] = true;
for(int i = 0 ; i < vc[m].size() ; i++) {
gg = vc[m][i];
if(dis[m] + gg.w < dis[gg.v]) {
dis[gg.v] = dis[m] + gg.w;
if(!vis[gg.v]) {
vis[gg.v] = true;
q.push(gg.v);
}
}
}
q.pop();
}
}
int main() {
int t;
TnT gg;
scanf("%d" , &t);
while(t--) {
scanf("%d%d" , &n , &m);
for(int i = 1 ; i <= n ; i++) {
vc[i].clear();
dis[i] = inf;
}
for(int i = 1 ; i <= m ; i++) {
scanf("%d%d%d" , &a[i] , &b[i] , &c[i]);
gg.v = b[i] , gg.w = c[i];
vc[a[i]].push_back(gg);
}
dij(1);
long long sum = 0;
for(int i = 1 ; i <= n ; i++) {
sum += (long long)dis[i];
}
for(int i = 1 ; i <= n ; i++) {
vc[i].clear();
dis[i] = inf;
}
for(int i = 1 ; i <= m ; i++) {
gg.v = a[i] , gg.w = c[i];
vc[b[i]].push_back(gg);
}
dij(1);
for(int i = 1 ; i <= n ; i++) {
sum += (long long)dis[i];
}
printf("%lld\n" , sum);
}
return 0; }
poj 1511 Invitation Cards(dijstra优化)的更多相关文章
- 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 ...
- [POJ] 1511 Invitation Cards
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 1511 Invitation Cards (最短路spfa)
Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- (简单) POJ 1511 Invitation Cards,SPFA。
Description In the age of television, not many people attend theater performances. Antique Comedians ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- poj 1511 Invitation Cards(最短路中等题)
In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...
随机推荐
- MySQL 之 Explain 输出分析
MySQL 之 Explain 输出分析 背景 前面的文章写过 MySQL 的事务和锁,这篇文章我们来聊聊 MySQL 的 Explain,估计大家在工作或者面试中多多少少都会接触过这个.可能工作中 ...
- GStreamer流媒体知识介绍
GStreamer框架 1.GStreamer是什么? 众所周知,Microsoft's Windows和Apple's MacOS对多媒体设备.多媒体创作.播放和实时处理等方面都有很好的支持,而Li ...
- nginx基本运维及常用配置
nginx基本运维及常用配置 ========================================================== 基本运维 nginx 的启动 nginx -c /p ...
- 统一流控服务开源:基于.Net Core的流控服务
先前有一篇博文,梳理了流控服务的场景.业界做法和常用算法 统一流控服务开源-1:场景&业界做法&算法篇 最近完成了流控服务的开发,并在生产系统进行了大半年的验证,稳定可靠.今天整理一下 ...
- 利用python自动生成verilog模块例化模板
一.前言 初入职场,一直忙着熟悉工作,就没什么时间更新博客.今天受“利奇马”的影响,只好宅在家中,写写技术文章.芯片设计规模日益庞大,编写脚本成了芯片开发人员必要的软技能.模块端口动不动就几十上百个, ...
- python基础--列表,元组
list1 = [1,2,3,4,5,6]list2 = ['wang','cong']# 1对列表中的元素取值(通过索引)print(list1[3]) # 4print(list2[1]) # c ...
- intellij idea与github整合管理代码
各位看官大家好,博主每每在公司学习新知识写代码时都需要通过U盘带回家来继续每天的学习,觉得这样实在麻烦,于是今天就整合了一下github来完成代码的管理. 开始之前我们需要准备三样东西:1.intel ...
- WebSocket和HTTP协议的区别
HTTP: 1,无状态协议. 2,短连接.(Ajax轮询方式或Long poll方式实现“持久连接”状态) 2,被动型. 客户端请求->服务器端响应.服务端不能主动联系客户端,只能有客户端发 ...
- ES解决bootstrap checks failed, memory locking requested for elasticsearch process but memory is not locked问题
问题描述: ERROR: [1] bootstrap checks failed[1]: memory locking requested for elasticsearch process but ...
- 常用的python内置方法
all ( ) 循环参数,参数全为真就返回Ture any() 只要有一个 ...