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 ...
随机推荐
- 用大白话告诉你 :Java 后端到底是在做什么?
阅读本文大概需要 6 分钟. 作者:黄小斜 新手程序员通常会走入一个误区,就是认为学习了一门语言,就可以称为是某某语言工程师了.但事实上真的是这样吗?其实并非如此. 今天我们就来聊一聊,Java 开发 ...
- 用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name=&qu ...
- 学习Qt的一点小感想
作为一名电子信息工程的学生,嵌入式似乎是不二的选择,然后我便学习了一下在嵌入式广泛应用的QT软件,刚开始就是学学控件,觉得还是简单,也觉得比较新颖,可是到了做一些具体的小东西就会发现学的东西远远不够, ...
- DedeCMS V5.7 SP2前台文件上传漏洞(CVE-2018-20129)
DedeCMS V5.7 SP2前台文件上传漏洞(CVE-2018-20129) 一.漏洞描述 织梦内容管理系统(Dedecms)是一款PHP开源网站管理系统.Dedecms V5.7 SP2版本中的 ...
- Linux fuser工具使用方法介绍
引言 fuser是linux中较常用的工具,"fuser"——从其名称我们可以看出该工具的用途:查询给定文件或目录的用户或进程信息. 除查询文件相关信息之外,使用fuser还能向进 ...
- SAP-批量删除生产订单
1.SE38运行:PPARCHP1 2.先用COOIS导出订单,已经CLSD,没有删除的
- Tomcat源码分析 (八)----- HTTP请求处理过程(一)
终于进行到Connector的分析阶段了,这也是Tomcat里面最复杂的一块功能了.Connector中文名为连接器,既然是连接器,它肯定会连接某些东西,连接些什么呢? Connector用于接受请求 ...
- OpenResty 社区王院生:APISIX 的高性能实践
2019 年 7 月 6 日,OpenResty 社区联合又拍云,举办 OpenResty × Open Talk 全国巡回沙龙·上海站,OpenResty 软件基金会联合创始人王院生在活动上做了&l ...
- Oracle 12c Adoption Discussion — Summary
Morning (@9:30) Oracle 12c Overview & Features for Developers Oracle Database In-Memory Deep Div ...
- CSS布局:元素水平垂直居中
CSS布局:元素水平垂直居中 本文将依次介绍在不同条件下实现水平垂直居中的多种方法 水平垂直居中是在写网页时经常会用到的需求,在上两篇博客中,分别介绍了水平居中和垂直居中的方法.本文的水平垂直居中就是 ...