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 ...
随机推荐
- 读书计划——javascript dom 编程艺术(一)
用脑图把基础体系再捋清楚一边.
- pycharm3.x 注册码
PyCharm 3.0 注册码 PyCharm3 序列号 License Key 用户名:yueting3527 注册码: ===== LICENSE BEGIN ===== 93347-120420 ...
- codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre
C. Lieges of Legendre 题意:给n,m表示有n个为2的倍数,m个为3的倍数:问这n+m个数不重复时的最大值 最小为多少? 数据:(0 ≤ n, m ≤ 1 000 000, n + ...
- 2016032701 - ubuntu安装jdk
参考地址:http://jingyan.baidu.com/article/d621e8da0e92052865913f32.html 1.首先需要去oracle官网去下载jdk1.8,我本人下载的是 ...
- 关于博客名“大话济公”的说明
其实本来没打算起这个名字的,换了几个名字都被占用了(无语啊...).最近呢,我在研究<济公传>,对于济公的传说比较喜欢,尤其是这个任务,诙谐幽默,同时有时时刻刻在帮助有困难的群众,虽然是个 ...
- Oracle目录结构及创建新数据库
oracle目录结构 当需要创建新的数据仓库时我可以用 Database Configuration Assistant(数据库配置助手) admin 存放创建的不同数据库 cfgtoollogs c ...
- JavaScript decodeURI() 和 encodeURI() 函数
定义和用法 decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码. 语法 decodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串 ...
- 《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构成的模块跑起来后才出现内存崩溃,是很让人痛苦的.因为崩溃的位置在时间和空间上,通常是在距真正的错误源一段距离之后才 ...
- spring framework 各版本源码下载地址
现在spring的源码下载地址真是不好找,这次终于找到了.记录一下,以帮助需要的朋友. https://github.com/spring-projects/spring-framework/tags ...
- jquery mobile event
jquery.js $(document).on("mobileinit", function() { // }); jquery.mobile.js $(document).re ...