POJ 1511 Invitation Cards dij
分析:正向加边,反向加边,然后两遍dij
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=1e6+;
const int INF=0x3f3f3f3f;
struct Edge{
int u,v,next;
LL w;
bool operator<(const Edge &e)const{
return w>e.w;
}
}edge[N],o[N];
int head[N],tot,n,m;
LL d[N],tmp[N];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
priority_queue<Edge>q;
bool vis[N];
void dij(int s){
for(int i=;i<=n;++i)d[i]=-,vis[i]=;
d[s]=,q.push(Edge{,s,,});
while(!q.empty()){
while(!q.empty()&&vis[q.top().v])q.pop();
if(q.empty())break;
int u=q.top().v;
q.pop();
vis[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]&&(d[v]==-||d[v]>d[u]+edge[i].w)){
d[v]=d[u]+edge[i].w;
q.push(Edge{,v,d[v],});
}
}
}
// return d[t];
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head)),tot=;
for(int i=;i<=m;++i){
scanf("%d%d%I64d",&o[i].u,&o[i].v,&o[i].w);
add(o[i].u,o[i].v,o[i].w);
}
dij();
for(int i=;i<=n;++i)tmp[i]=d[i];
memset(head,-,sizeof(head)),tot=;
for(int i=;i<=m;++i){
add(o[i].v,o[i].u,o[i].w);
}
dij();
LL ans=;
for(int i=;i<=n;++i)
ans+=tmp[i]+d[i];
printf("%I64d\n",ans);
}
return ;
}
POJ 1511 Invitation Cards dij的更多相关文章
- 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
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 18198 Accepted: 596 ...
- 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 ...
- 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 ...
随机推荐
- 关于php的认识和介绍
php的介绍: 什么是php? <1>:php是一个编程语言 <2>:php是处理php编程语言的一个软件.php语言必须运行在php软件上. 为什么要学习php? php可以 ...
- PHP权限分配思路
常见四种方式1.用户+组+角色+权限2.用户+组+权限3.用户+角色+权限(最多用)4.用户+权限以第三种为例:权限:用户操作的具体事件:如curd角色:指一类用户拥有的权限,如超级管理员,管理员,普 ...
- 关于H5 storage 的一些注意事项以及用法
在我们使用H5 storage之前,先了解一下storage的介绍吧: Storage模块管理应用本地数据存储,用于应用数据的保存和读取,应用本地数据localStorage,sessionStora ...
- 使用Yeoman搭建 AngularJS 应用 (2) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/index.html 使用Yeoman搭建简单的应用 今天将会搭建一个简单的网页程序.你将可以添加,删除,拖拽和保存. 浏览Yeoman Y ...
- 随机产生字母a--z, A-Z 的任意组合
VERSION 1.0 引自: http://www.coderanch.com/t/134491/Security/generating-secure-tokens package demo; ...
- MongoDB实战指南(六):MongoDB复制集之复制集概述
1. 复制集概述 数据库总是会遇到各种失败的场景,如网络连接断开.断电等,尽管journaling日志功能也提供了数据恢复的功能,但journaling通常是针对单个节点来说的,只能保证单节点数据的一 ...
- HDU 5044 TREE
题意:一棵树上两种操作,操作1,改变u到v的每一点的值增加k,操作2,改变u到v每一条边值增加k.最后结束时问,每一点和每一条边的值. 初始时,点和边的值都为0. 分析: 很显然要用树链剖分,将点和边 ...
- NGUI所见即所得之UIAtlasMaker , UIAtlas (2)
本文的重点就是要将NGUI把多张图片打成一个图集(Atlas)的原理和过程研究下,学习下Unity提供的api和NGUI写的功能以及设计思想. (原文链接) 其它链接:NGUI所见即所得之UIRoot ...
- Cppcheck 用法(上篇)
http://blog.csdn.net/u011012932/article/details/52778149
- Autodesk 2014全套密钥
AUTODESK 2014通用安装序列号: 666-69696969 667-98989898 400-45454545 066-66666666 AUTODESK 2014全系列产品密钥 001F1 ...