分析:正向加边,反向加边,然后两遍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的更多相关文章

  1. 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 / ...

  2. [POJ] 1511 Invitation Cards

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 18198   Accepted: 596 ...

  3. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...

  4. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

  5. POJ 1511 Invitation Cards (spfa的邻接表)

    Invitation Cards Time Limit : 16000/8000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) ...

  6. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  7. Poj 1511 Invitation Cards(spfa)

    Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...

  8. (简单) POJ 1511 Invitation Cards,SPFA。

    Description In the age of television, not many people attend theater performances. Antique Comedians ...

  9. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

随机推荐

  1. yii2 用gii生成后台模块 view path描述

    view path 格式: @backend/views/refund , 注意@和/

  2. 让c像python一样可以在命令行写代码并且编译

    在你亲爱的.bashrc/.zshrc中添加 ###C###go_libs="-lm"go_flags="-g -Wall -include allheads.h -O3 ...

  3. Java知识总结--数据库

    1 薪水排序后薪水排名在第3-5的员工 1)select * from(select ename,sal,rownum rn from (select ename,sal from emp_44 wh ...

  4. 工作踩坑记录:JavaScript跳转被缓存

    起因:业务想要一个固定二维码来每周扫码跳转到不同的页面上去,我用JS写了个跳转,却发现被缓存了,虽然被具体被缓存多久不清楚,但是被缓存了很不爽,不符合业务实时更改这个二维码跳转页面的需求. 经过:既然 ...

  5. Python守护进程(多线程开发)

    本段代码主要作用是httpsqs队列的消费端守护进程,从httpsqs中取出数据,放入mongodb #!/usr/bin/python import sys,time,json,logging im ...

  6. E8.Net工作流平台之中国特色

     特色之一领导排名有先后 领导排名是有潜规则的,不论是在企业通讯录中,还是企业员工目录中,不管在流程执行过程中,还是存档数据中,当前领导的排名一定要按潜规则展示,不能随便罗列.E8.Net工作流解决了 ...

  7. 【JPA】表达条件查询的关键字

    1.通过解析方法名创建查询 框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find.findBy.read.readBy.get.getBy,然后对剩下部分进行解析.并且如果方法的最后一 ...

  8. 【welcome-file-list】让默认页生效

    <welcome-file-list> <welcome-file>404.html</welcome-file> <welcome-file>/vie ...

  9. MVC 4 网页版发送 邮件的配置问题

    有时项目要用到邮箱验证就要发送邮件传统的解决方案: public void SendResetPasswordEmail(string email) { MailAddress from = new ...

  10. iphone下overflow失效问题的解决方法

    overflow-y: auto; -webkit-overflow-scrolling:touch; /*加上这个让浏览器支持touch和自动滚动这样界面就可以滚动了*/